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 10.6 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

    /**
     * @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 = [];
69
        $resultCode = $response['resultCode'];
70
        // validate result
71 72 73
        if (!empty($resultCode)) {
            $payment->setAdditionalInformation('resultCode', $resultCode);
            switch ($resultCode) {
74
                case "IdentifyShopper":
75
                    $payment->setAdditionalInformation('threeDSType', $resultCode);
76 77 78 79
                    $payment->setAdditionalInformation(
                        'threeDS2Token',
                        $response['authentication']['threeds2.fingerprintToken']
                    );
80
                    $payment->setAdditionalInformation('adyenPaymentData', $response['paymentData']);
81 82
                    break;
                case "ChallengeShopper":
83
                    $payment->setAdditionalInformation('threeDSType', $resultCode);
84 85 86 87
                    $payment->setAdditionalInformation(
                        'threeDS2Token',
                        $response['authentication']['threeds2.challengeToken']
                    );
88
                    $payment->setAdditionalInformation('adyenPaymentData', $response['paymentData']);
89
                    break;
90 91 92 93 94 95 96 97 98 99
                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'])) {
100 101
                        //Multibanco resultCode has changed after checkout v49 and
                        // comprafacil.entity is not received anymore
102 103 104 105 106 107 108
                        foreach ($response['additionalData'] as $key => $value) {
                            if (strpos($key, 'comprafacil') === 0) {
                                $payment->setAdditionalInformation($key, $value);
                            }
                        }
                    }

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

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
                    $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
152 153 154 155 156
                    if (
                        isset($response['redirect']['data']['PaReq']) &&
                        isset($response['redirect']['data']['MD']) &&
                        isset($response['redirect']['data']['TermUrl'])
                    ) {
157 158
                        $paReq = null;
                        $md = null;
159
                        $termUrl = null;
160 161 162 163 164 165 166 167 168 169 170

                        $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'];
                        }

171 172 173 174 175
                        if (!empty($response['redirect']['data']['TermUrl'])) {
                            $termUrl = $response['redirect']['data']['TermUrl'];
                        }

                        if ($paReq && $md && $termUrl && $redirectUrl && $paymentData && $redirectMethod) {
176 177 178 179
                            $payment->setAdditionalInformation('redirectUrl', $redirectUrl);
                            $payment->setAdditionalInformation('redirectMethod', $redirectMethod);
                            $payment->setAdditionalInformation('paRequest', $paReq);
                            $payment->setAdditionalInformation('md', $md);
180
                            $payment->setAdditionalInformation('termUrl', $termUrl);
181 182 183 184 185 186 187
                            $payment->setAdditionalInformation('paymentData', $paymentData);
                        } else {
                            $isValid = false;
                            $errorMsg = __('3D secure is not valid.');
                            $this->adyenLogger->error($errorMsg);
                            $errorMessages[] = $errorMsg;
                        }
188 189
                        // otherwise it is an alternative payment method which only requires the
                        // redirect url to be present
190 191 192 193
                    } else {
                        // Flag to show we are in the checkoutAPM flow
                        $payment->setAdditionalInformation('checkoutAPM', true);

194 195 196
                        if (!empty($response['details'])) {
                            $payment->setAdditionalInformation('details', $response['details']);
                        }
197 198 199 200 201 202 203
                        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.');
204
                            $this->adyenLogger->error($errorMsg);
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                            $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.');
221 222 223 224 225

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

226 227 228 229 230
            throw new \Magento\Framework\Exception\LocalizedException(__($errorMsg));
        }

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