<commit_before><?php
/**
 * PaytrailController class file.
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright &copy; Nord Software 2014
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @package nordsoftware.yii-paytrail.controllers
 */

class PaytrailController extends PaymentController
{
    /**
     * @var string
     */
    public $managerId = 'payment';

    /**
     * @return array
     */
    public function filters()
    {
        return array(
            'accessControl',
            'validateSuccessRequest + success, notify',
            'validateFailureRequest + failure',
        );
    }

    /**
     * @param CFilterChain $filterChain
     * @throws CHttpException
     * @throws CException
     */
    public function filterValidateSuccessRequest(CFilterChain $filterChain)
    {
        $gateway = $this->createGateway();
        $data = implode('|', array($_GET['ORDER_NUMBER'], $_GET['TIMESTAMP'], $_GET['PAID'], $_GET['METHOD'], $gateway->apiSecret));
        if (!$this->validateAuthCode($_GET['RETURN_AUTHCODE'], $data)) {
            throw new CException('Invalid authentication code.');
        }
        $filterChain->run();
    }

    /**
     * @param CFilterChain $filterChain
     * @throws CHttpException
     * @throws CException
     */
    public function filterValidateFailureRequest(CFilterChain $filterChain)
    {
        $gateway = $this->createGateway();
        $data = implode('&', array($_GET['ORDER_NUMBER'], $_GET['TIMESTAMP'], $gateway->apiSecret));
        if (!$this->validateAuthCode($_GET['RETURN_AUTHCODE'], $data)) {
            throw new CException('Invalid authentication code.');
        }
        $filterChain->run();
    }

    /**
     * @return PaytrailGateway
     */
    protected function createGateway()
    {
        return $this->getPaymentManager()->createGateway('paytrail');
    }

    /**
     * @return bool
     */
    protected function validateAuthCode($code, $data)
    {
        return $code !== strtoupper(md5($data));
    }

    /**
     * @return array access control rules.
     */
    public function accessRules()
    {
        return array(
            array('allow', 'users' => array('@')),
            array('deny'),
        );
    }

    /**
     * Invoked by Paytrail after a successful payment.
     * @param string $ORDER_NUMBER
     * @param string $TIMESTAMP
     * @param string $PAID
     * @param string $METHOD
     * @param string $RETURN_AUTHCODE
     */
    public function actionSuccess($ORDER_NUMBER, $TIMESTAMP, $PAID, $METHOD, $RETURN_AUTHCODE)
    {
        $manager = $this->getPaymentManager();
        $transaction = $this->loadTransaction($ORDER_NUMBER);
        $manager->changeTransactionStatus(PaymentTransaction::STATUS_SUCCEEDED, $transaction);
        $this->redirect($manager->successUrl);
    }

    /**
     * Invoked by Paytrail after a failed payment.
     * @param string $ORDER_NUMBER
     * @param string $TIMESTAMP
     * @param string $RETURN_AUTHCODE
     */
    public function actionFailure($ORDER_NUMBER, $TIMESTAMP, $RETURN_AUTHCODE)
    {
        $manager = $this->getPaymentManager();
        $transaction = $this->loadTransaction($ORDER_NUMBER);
        $manager->changeTransactionStatus(PaymentTransaction::STATUS_CANCELLED, $transaction);
        $this->redirect($manager->failureUrl);
    }

    /**
     * Invoked by Paytrail when a payment has been confirmed.
     * @param string $ORDER_NUMBER
     * @param string $TIMESTAMP
     * @param string $PAID
     * @param string $METHOD
     * @param string $RETURN_AUTHCODE
     */<commit_msg>Fix a few bugs in the paytrail controller<commit_after><?php
/**
 * PaytrailController class file.
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright &copy; Nord Software 2014
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @package nordsoftware.yii-paytrail.controllers
 */

class PaytrailController extends PaymentController
{
    /**
     * @var string
     */
    public $managerId = 'payment';

    /**
     * @return array
     */
    public function filters()
    {
        return array(
            'accessControl',
            'validateSuccessRequest + success, notify',
            'validateFailureRequest + failure',
        );
    }

    /**
     * @param CFilterChain $filterChain
     * @throws CHttpException
     * @throws CException
     */
    public function filterValidateSuccessRequest(CFilterChain $filterChain)
    {
        $gateway = $this->createGateway();
        $data = implode('|', array($_GET['ORDER_NUMBER'], $_GET['TIMESTAMP'], $_GET['PAID'], $_GET['METHOD'], $gateway->apiSecret));
        if (!$this->validateAuthCode($_GET['RETURN_AUTHCODE'], $data)) {
            throw new CException('Invalid authentication code.');
        }
        $filterChain->run();
    }

    /**
     * @param CFilterChain $filterChain
     * @throws CHttpException
     * @throws CException
     */
    public function filterValidateFailureRequest(CFilterChain $filterChain)
    {
        $gateway = $this->createGateway();
        $data = implode('|', array($_GET['ORDER_NUMBER'], $_GET['TIMESTAMP'], $gateway->apiSecret));
        if (!$this->validateAuthCode($_GET['RETURN_AUTHCODE'], $data)) {
            throw new CException('Invalid authentication code.');
        }
        $filterChain->run();
    }

    /**
     * @return PaytrailGateway
     */
    protected function createGateway()
    {
        return $this->getPaymentManager()->createGateway('paytrail');
    }

    /**
     * @return bool
     */
    protected function validateAuthCode($code, $data)
    {
        return $code === strtoupper(md5($data));
    }

    /**
     * @return array access control rules.
     */
    public function accessRules()
    {
        return array(
            array('allow', 'users' => array('@')),
            array('deny'),
        );
    }

    /**
     * Invoked by Paytrail after a successful payment.
     * @param string $ORDER_NUMBER
     * @param string $TIMESTAMP
     * @param string $PAID
     * @param string $METHOD
     * @param string $RETURN_AUTHCODE
     */
    public function actionSuccess($ORDER_NUMBER, $TIMESTAMP, $PAID, $METHOD, $RETURN_AUTHCODE)
    {
        $manager = $this->getPaymentManager();
        $transaction = $this->loadTransaction($ORDER_NUMBER);
        $manager->changeTransactionStatus(PaymentTransaction::STATUS_SUCCEEDED, $transaction);
        $this->redirect($manager->successUrl);
    }

    /**
     * Invoked by Paytrail after a failed payment.
     * @param string $ORDER_NUMBER
     * @param string $TIMESTAMP
     * @param string $RETURN_AUTHCODE
     */
    public function actionFailure($ORDER_NUMBER, $TIMESTAMP, $RETURN_AUTHCODE)
    {
        $manager = $this->getPaymentManager();
        $transaction = $this->loadTransaction($ORDER_NUMBER);
        $manager->changeTransactionStatus(PaymentTransaction::STATUS_FAILED, $transaction);
        $this->redirect($manager->failureUrl);
    }

    /**
     * Invoked by Paytrail when a payment has been confirmed.
     * @param string $ORDER_NUMBER
     * @param string $TIMESTAMP
     * @param string $PAID
     * @param string $METHOD
     * @param string $RETURN_AUTHCODE
     */
{'fraction_non_alphanumeric': 0.1343262998207144, 'fraction_numerical': 0.0013791201213625708, 'mean_word_length': 2.6774847870182557, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '227823', 'n_tokens_mistral': 2025, 'n_tokens_neox': 2021, 'n_words': 439}