We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

CheckoutResponseValidator.php 9.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php
/**
 *                       ######
 *                       ######
 * ############    ####( ######  #####. ######  ############   ############
 * #############  #####( ######  #####. ######  #############  #############
 *        ######  #####( ######  #####. ######  #####  ######  #####  ######
 * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
 * ###### ######  #####( ######  #####. ######  #####          #####  ######
 * #############  #############  #############  #############  #####  ######
 *  ############   ############  #############   ############  #####  ######
 *                                      ######
 *                               #############
 *                               ############
 *
 * Adyen Payment module (https://www.adyen.com/)
 *
 * Copyright (c) 2015 Adyen BV (https://www.adyen.com/)
 * See LICENSE.txt for license details.
 *
 * Author: Adyen <magento@adyen.com>
 */

namespace Adyen\Payment\Gateway\Validator;

use Magento\Payment\Gateway\Validator\AbstractValidator;

class CheckoutResponseValidator extends AbstractValidator
{
30 31 32 33
    /**
     * @var \Adyen\Payment\Logger\AdyenLogger
     */
    private $adyenLogger;
34

35 36 37
    /**
     * @var \Adyen\Payment\Helper\Data
     */
38
    private $adyenHelper;
39

40 41 42 43 44 45 46 47
    /**
     * GeneralResponseValidator constructor.
     *
     * @param \Magento\Payment\Gateway\Validator\ResultInterfaceFactory $resultFactory
     * @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
     */
    public function __construct(
        \Magento\Payment\Gateway\Validator\ResultInterfaceFactory $resultFactory,
48 49
        \Adyen\Payment\Logger\AdyenLogger $adyenLogger,
        \Adyen\Payment\Helper\Data $adyenHelper
50 51
    ) {
        $this->adyenLogger = $adyenLogger;
52
        $this->adyenHelper = $adyenHelper;
53 54
        parent::__construct($resultFactory);
    }
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

    /**
     * @param array $validationSubject
     * @return \Magento\Payment\Gateway\Validator\ResultInterface
     */
    public function validate(array $validationSubject)
    {
        $response = \Magento\Payment\Gateway\Helper\SubjectReader::readResponse($validationSubject);
        $paymentDataObjectInterface = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($validationSubject);
        $payment = $paymentDataObjectInterface->getPayment();

        $payment->setAdditionalInformation('3dActive', false);
        $isValid = true;
        $errorMessages = [];
        // validate result
70
        if (!empty($response['resultCode'])) {
71
            switch ($response['resultCode']) {
72 73
                case "IdentifyShopper":
                    $payment->setAdditionalInformation('threeDSType', $response['resultCode']);
74 75 76 77
                    $payment->setAdditionalInformation(
                        'threeDS2Token',
                        $response['authentication']['threeds2.fingerprintToken']
                    );
78 79 80 81
                    $payment->setAdditionalInformation('threeDS2PaymentData', $response['paymentData']);
                    break;
                case "ChallengeShopper":
                    $payment->setAdditionalInformation('threeDSType', $response['resultCode']);
82 83 84 85
                    $payment->setAdditionalInformation(
                        'threeDS2Token',
                        $response['authentication']['threeds2.challengeToken']
                    );
86 87
                    $payment->setAdditionalInformation('threeDS2PaymentData', $response['paymentData']);
                    break;
88 89 90 91 92 93 94 95 96 97
                case "Authorised":
                case "Received":
                    // For banktransfers store all bankTransfer details
                    if (!empty($response['additionalData']['bankTransfer.owner'])) {
                        foreach ($response['additionalData'] as $key => $value) {
                            if (strpos($key, 'bankTransfer') === 0) {
                                $payment->setAdditionalInformation($key, $value);
                            }
                        }
                    } elseif (!empty($response['additionalData']['comprafacil.entity'])) {
98 99
                        //Multibanco resultCode has changed after checkout v49 and
                        // comprafacil.entity is not received anymore
100 101 102 103 104 105 106
                        foreach ($response['additionalData'] as $key => $value) {
                            if (strpos($key, 'comprafacil') === 0) {
                                $payment->setAdditionalInformation($key, $value);
                            }
                        }
                    }

107 108
                    // Save cc_type if available in the response
                    if (!empty($response['additionalData']['paymentMethod'])) {
109 110 111
                        $ccType = $this->adyenHelper->getMagentoCreditCartType(
                            $response['additionalData']['paymentMethod']
                        );
112 113 114
                        $payment->setAdditionalInformation('cc_type', $ccType);
                        $payment->setCcType($ccType);
                    }
115 116 117
                    $payment->setAdditionalInformation('pspReference', $response['pspReference']);
                    break;
                case "PresentToShopper":
118 119 120 121 122
                    if (!empty($response['action'])) {
                        $payment->setAdditionalInformation('action', $response['action']);
                    }
                    if (!empty($response['pspReference'])) {
                        $payment->setAdditionalInformation('pspReference', $response['pspReference']);
123 124 125
                    }
                    break;
                case "RedirectShopper":
126 127
                    $payment->setAdditionalInformation('threeDSType', $response['resultCode']);

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
                    $redirectUrl = null;
                    $paymentData = null;

                    if (!empty($response['redirect']['url'])) {
                        $redirectUrl = $response['redirect']['url'];
                    }

                    if (!empty($response['redirect']['method'])) {
                        $redirectMethod = $response['redirect']['method'];
                    }

                    if (!empty($response['paymentData'])) {
                        $paymentData = $response['paymentData'];
                    }

                    // If the redirect data is there then the payment is a card payment with 3d secure
                    if (isset($response['redirect']['data']['PaReq']) && isset($response['redirect']['data']['MD'])) {
                        $paReq = null;
                        $md = null;

                        $payment->setAdditionalInformation('3dActive', true);

                        if (!empty($response['redirect']['data']['PaReq'])) {
                            $paReq = $response['redirect']['data']['PaReq'];
                        }

                        if (!empty($response['redirect']['data']['MD'])) {
                            $md = $response['redirect']['data']['MD'];
                        }

                        if ($paReq && $md && $redirectUrl && $paymentData && $redirectMethod) {
                            $payment->setAdditionalInformation('redirectUrl', $redirectUrl);
                            $payment->setAdditionalInformation('redirectMethod', $redirectMethod);
                            $payment->setAdditionalInformation('paRequest', $paReq);
                            $payment->setAdditionalInformation('md', $md);
                            $payment->setAdditionalInformation('paymentData', $paymentData);
                        } else {
                            $isValid = false;
                            $errorMsg = __('3D secure is not valid.');
                            $this->adyenLogger->error($errorMsg);
                            $errorMessages[] = $errorMsg;
                        }
170 171
                        // otherwise it is an alternative payment method which only requires the
                        // redirect url to be present
172 173 174 175
                    } else {
                        // Flag to show we are in the checkoutAPM flow
                        $payment->setAdditionalInformation('checkoutAPM', true);

176 177 178
                        if (!empty($response['details'])) {
                            $payment->setAdditionalInformation('details', $response['details']);
                        }
179 180 181 182 183 184 185
                        if ($redirectUrl && $paymentData && $redirectMethod) {
                            $payment->setAdditionalInformation('redirectUrl', $redirectUrl);
                            $payment->setAdditionalInformation('redirectMethod', $redirectMethod);
                            $payment->setAdditionalInformation('paymentData', $paymentData);
                        } else {
                            $isValid = false;
                            $errorMsg = __('Payment method is not valid.');
186
                            $this->adyenLogger->error($errorMsg);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
                            $errorMessages[] = $errorMsg;
                        }
                    }
                    break;
                case "Refused":
                    $errorMsg = __('The payment is REFUSED.');
                    // this will result the specific error
                    throw new \Magento\Framework\Exception\LocalizedException(__($errorMsg));
                    break;
                default:
                    $errorMsg = __('Error with payment method please select different payment method.');
                    throw new \Magento\Framework\Exception\LocalizedException(__($errorMsg));
                    break;
            }
        } else {
            $errorMsg = __('Error with payment method please select different payment method.');
203 204 205 206 207

            if (!empty($response['error'])) {
                $this->adyenLogger->error($response['error']);
            }

208 209 210 211 212
            throw new \Magento\Framework\Exception\LocalizedException(__($errorMsg));
        }

        return $this->createResult($isValid, $errorMessages);
    }
213
}