<commit_before><?php

/**
 * Adyen Payment Module
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL> so we can send you a copy immediately.
 *
 * @category	Adyen
 * @package	Adyen_Payment
 * @copyright	Copyright (c) 2011 Adyen (http://www.adyen.com)
 * @license	http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
/**
 * @category   Payment Gateway
 * @package    Adyen_Payment
 * @author     Adyen
 * @property   <NAME>.V
 * @copyright  Copyright (c) 2014 Adyen BV (http://www.adyen.com)
 */
abstract class Adyen_Payment_Model_Adyen_Abstract extends Mage_Payment_Model_Method_Abstract {

    /**
     * Zend_Log debug level
     * @var unknown_type
     */
    const DEBUG_LEVEL = 7;

    protected $_isGateway = false;
    protected $_canAuthorize = true;
    protected $_canCapture = true;
    protected $_canCapturePartial = true;
    protected $_canRefund = true;
    protected $_canVoid = true;
    protected $_canUseInternal = true;
    protected $_canUseCheckout = true;
    protected $_canUseForMultishipping = false;
    protected $_canRefundInvoicePartial = true;

    /**
     * TODO: whether a captured transaction may be voided by this gateway
     * This may happen when amount is captured, but not settled
     * @var bool
     */
    protected $_canCancelInvoice = true;

    /**
     * Magento Order Object
     * @var unknown_type
     */
    protected $_order;

    /**
     * Module identifiers
     */
    protected $_code = 'adyen_abstract';
    protected $_paymentMethod = 'abstract';

    /**
     * Internal objects and arrays for SOAP communication
     */
    protected $_service = NULL;
    protected $_accountData = NULL;

    /**
     * Payment Modification Request
     * @var unknown_type
     */
    protected $_paymentRequest = NULL;
    protected $_optionalData = NULL;
    protected $_testModificationUrl = 'https://pal-test.adyen.com/pal/adapter/httppost';
    protected $_liveModificationUrl = 'https://pal-live.adyen.com/pal/adapter/httppost';

    /**
     * @param Varien_Object $payment
     * @param unknown_type $amount
     */
    public function refund(Varien_Object $payment, $amount) {
        $this->writeLog('refund fx called');

        $order = $payment->getOrder();
        $pspReference = Mage::getModel('adyen/event')->getOriginalPspReference($order->getIncrementId());

        // if amound is a full refund send a refund/cancelled request so if it is not captured yet it will cancel the order
        $grandTotal = $order->getGrandTotal();

        if($grandTotal == $amount) {
            $order->getPayment()->getMethodInstance()->SendCancelOrRefund($payment, null, $pspReference);
        } else {
            $order->getPayment()->getMethodInstance()->sendRefundRequest($payment, $amount, $pspReference);
        }

        return $this;
    }

    /**
     * In the backend it means Authorize only
     * @param Varien_Object $payment
     * @param unknown_type $amount
     */
    public function authorize(Varien_Object $payment, $amount) {
    	parent::authorize($payment, $amount);
        $payment->setLastTransId($this->getTransactionId())->setIsTransactionPending(true);
        if ($this->getCode() == 'adyen_boleto' || $this->getCode() == 'adyen_cc' || $this->getCode() == 'adyen_oneclick' || $this->getCode() == 'adyen_elv' || $this->getCode() == 'adyen_sepa') {
            $_authorizeResponse = $this->_processRequest($payment, $amount, "authorise");
        }
        return $this;
    }

    /**
     * In backend it means Authorize && Capture
     * @param $payment
     * @param $amount
     */
    public function capture(Varien_Object $payment, $amount) {
        parent::capture($payment, $amount);
        $payment->setStatus(self::STATUS_APPROVED)
                ->setTransactionId($this->getTransactionId())
                ->setIsTransactionClosed(0);
        return $this;
    }
	
	public function authorise3d(Varien_Object $payment, $amount) {
        $authorizeResponse = $this->_processRequest($payment, $amount, "authorise3d");
		$responseCode = $authorizeResponse->paymentResult->resultCode;
		return $responseCode;
	}

    public function sendCaptureRequest(Varien_Object $payment, $amount, $pspReference) {
        if (empty($pspReference)) {
            $this->writeLog('oops empty pspReference');
            return $this;
        }
        $this->writeLog("sendCaptureRequest pspReference : $pspReference amount: $amount");
        return $this->_processRequest($payment, $amount, "capture", $pspReference);
    }

    public function sendRefundRequest(Varien_Object $payment, $amount, $pspReference) {
        if (empty($pspReference)) {
            $this->writeLog('oops empty pspReference');
            return $this;
        }
        $this->writeLog("sendRefundRequest pspReference : $pspReference amount: $amount");
        return $this->_processRequest($payment, $amount, "refund", $pspReference);
    }

    public function SendCancelOrRefund(Varien_Object $payment, $pspReference) {
        if (empty($pspReference)) {
            $this->writeLog('oops empty pspReference');
            return $this;
        }
        $this->writeLog("sendCancelOrRefundRequest pspReference : $pspReference");
        return $this->_processRequest($payment, null, "cancel_or_refund", $pspReference);
    }

    /**
     * Process the request here
     * @param Varien_Object $payment
     * @param unknown_type $amount
     * @param unknown_type $request
     * @param unknown_type $responseData
     */
    protected function _processRequest(Varien_Object $payment, $amount, $request, $pspReference = null) {
        $this->_initOrder();
        $this->_initService();
        $merchantAccount = trim($this->_getConfigData('merchantAccount'));
        $recurringType = $this->_getConfigData('recurringtypes', 'adyen_abstract');
        $enableMoto = (int) $this->_getConfigData('enable_moto', 'adyen_cc');
        $modificationResult = Mage::getModel('adyen/adyen_data_modificationResult');
        $requestData = Mage::getModel('adyen/adyen_data_modificationRequest')
                ->create($payment, $amount, $this->_order, $merchantAccount, $pspReference);

        switch ($request) {
            case "authorise":<commit_msg>Fixes #25  most of the part was already committed this was missing. With the modification request for CANCEL_OR_REFUND amount must be empty.<commit_after><?php

/**
 * Adyen Payment Module
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to <EMAIL> so we can send you a copy immediately.
 *
 * @category	Adyen
 * @package	Adyen_Payment
 * @copyright	Copyright (c) 2011 Adyen (http://www.adyen.com)
 * @license	http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
/**
 * @category   Payment Gateway
 * @package    Adyen_Payment
 * @author     Adyen
 * @property   Adyen B.V
 * @copyright  Copyright (c) 2014 Adyen BV (http://www.adyen.com)
 */
abstract class Adyen_Payment_Model_Adyen_Abstract extends Mage_Payment_Model_Method_Abstract {

    /**
     * Zend_Log debug level
     * @var unknown_type
     */
    const DEBUG_LEVEL = 7;

    protected $_isGateway = false;
    protected $_canAuthorize = true;
    protected $_canCapture = true;
    protected $_canCapturePartial = true;
    protected $_canRefund = true;
    protected $_canVoid = true;
    protected $_canUseInternal = true;
    protected $_canUseCheckout = true;
    protected $_canUseForMultishipping = false;
    protected $_canRefundInvoicePartial = true;

    /**
     * TODO: whether a captured transaction may be voided by this gateway
     * This may happen when amount is captured, but not settled
     * @var bool
     */
    protected $_canCancelInvoice = true;

    /**
     * Magento Order Object
     * @var unknown_type
     */
    protected $_order;

    /**
     * Module identifiers
     */
    protected $_code = 'adyen_abstract';
    protected $_paymentMethod = 'abstract';

    /**
     * Internal objects and arrays for SOAP communication
     */
    protected $_service = NULL;
    protected $_accountData = NULL;

    /**
     * Payment Modification Request
     * @var unknown_type
     */
    protected $_paymentRequest = NULL;
    protected $_optionalData = NULL;
    protected $_testModificationUrl = 'https://pal-test.adyen.com/pal/adapter/httppost';
    protected $_liveModificationUrl = 'https://pal-live.adyen.com/pal/adapter/httppost';

    /**
     * @param Varien_Object $payment
     * @param unknown_type $amount
     */
    public function refund(Varien_Object $payment, $amount) {
        $this->writeLog('refund fx called');

        $order = $payment->getOrder();
        $pspReference = Mage::getModel('adyen/event')->getOriginalPspReference($order->getIncrementId());

        // if amound is a full refund send a refund/cancelled request so if it is not captured yet it will cancel the order
        $grandTotal = $order->getGrandTotal();

        if($grandTotal == $amount) {
            $order->getPayment()->getMethodInstance()->SendCancelOrRefund($payment, null, $pspReference);
        } else {
            $order->getPayment()->getMethodInstance()->sendRefundRequest($payment, $amount, $pspReference);
        }

        return $this;
    }

    /**
     * In the backend it means Authorize only
     * @param Varien_Object $payment
     * @param unknown_type $amount
     */
    public function authorize(Varien_Object $payment, $amount) {
    	parent::authorize($payment, $amount);
        $payment->setLastTransId($this->getTransactionId())->setIsTransactionPending(true);
        if ($this->getCode() == 'adyen_boleto' || $this->getCode() == 'adyen_cc' || $this->getCode() == 'adyen_oneclick' || $this->getCode() == 'adyen_elv' || $this->getCode() == 'adyen_sepa') {
            $_authorizeResponse = $this->_processRequest($payment, $amount, "authorise");
        }
        return $this;
    }

    /**
     * In backend it means Authorize && Capture
     * @param $payment
     * @param $amount
     */
    public function capture(Varien_Object $payment, $amount) {
        parent::capture($payment, $amount);
        $payment->setStatus(self::STATUS_APPROVED)
                ->setTransactionId($this->getTransactionId())
                ->setIsTransactionClosed(0);
        return $this;
    }
	
	public function authorise3d(Varien_Object $payment, $amount) {
        $authorizeResponse = $this->_processRequest($payment, $amount, "authorise3d");
		$responseCode = $authorizeResponse->paymentResult->resultCode;
		return $responseCode;
	}

    public function sendCaptureRequest(Varien_Object $payment, $amount, $pspReference) {
        if (empty($pspReference)) {
            $this->writeLog('oops empty pspReference');
            return $this;
        }
        $this->writeLog("sendCaptureRequest pspReference : $pspReference amount: $amount");
        return $this->_processRequest($payment, $amount, "capture", $pspReference);
    }

    public function sendRefundRequest(Varien_Object $payment, $amount, $pspReference) {
        if (empty($pspReference)) {
            $this->writeLog('oops empty pspReference');
            return $this;
        }
        $this->writeLog("sendRefundRequest pspReference : $pspReference amount: $amount");
        return $this->_processRequest($payment, $amount, "refund", $pspReference);
    }

    public function SendCancelOrRefund(Varien_Object $payment, $amount, $pspReference) {
        if (empty($pspReference)) {
            $this->writeLog('oops empty pspReference');
            return $this;
        }
        $this->writeLog("sendCancelOrRefundRequest pspReference : $pspReference");
        return $this->_processRequest($payment, null, "cancel_or_refund", $pspReference);
    }

    /**
     * Process the request here
     * @param Varien_Object $payment
     * @param unknown_type $amount
     * @param unknown_type $request
     * @param unknown_type $responseData
     */
    protected function _processRequest(Varien_Object $payment, $amount, $request, $pspReference = null) {
        $this->_initOrder();
        $this->_initService();
        $merchantAccount = trim($this->_getConfigData('merchantAccount'));
        $recurringType = $this->_getConfigData('recurringtypes', 'adyen_abstract');
        $enableMoto = (int) $this->_getConfigData('enable_moto', 'adyen_cc');
        $modificationResult = Mage::getModel('adyen/adyen_data_modificationResult');
        $requestData = Mage::getModel('adyen/adyen_data_modificationRequest')
                ->create($payment, $amount, $this->_order, $merchantAccount, $pspReference);

        switch ($request) {
            case "authorise":
{'fraction_non_alphanumeric': 0.12311952698151336, 'fraction_numerical': 0.0031434772846343838, 'mean_word_length': 3.1253473294226612, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 92, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6644197', 'n_tokens_mistral': 3914, 'n_tokens_neox': 3822, 'n_words': 942}