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 4.47 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
<?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
{
	/**
	 * @var \Adyen\Payment\Logger\AdyenLogger
	 */
	private $adyenLogger;

	/**
	 * GeneralResponseValidator constructor.
	 *
	 * @param \Magento\Payment\Gateway\Validator\ResultInterfaceFactory $resultFactory
	 * @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
	 */
	public function __construct(
		\Magento\Payment\Gateway\Validator\ResultInterfaceFactory $resultFactory,
		\Adyen\Payment\Logger\AdyenLogger $adyenLogger
	) {
		$this->adyenLogger = $adyenLogger;
		parent::__construct($resultFactory);
	}

	/**
	 * @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
		if ($response && isset($response['resultCode'])) {
			switch ($response['resultCode']) {
				case "Authorised":
					$payment->setAdditionalInformation('pspReference', $response['pspReference']);
					break;
				case "Received":
					$payment->setAdditionalInformation('pspReference', $response['pspReference']);
					// set additionalData
					if (isset($response['additionalData']) && is_array($response['additionalData'])) {

						$additionalData = $response['additionalData'];
						if (isset($additionalData['boletobancario.dueDate'])) {
							$payment->setAdditionalInformation(
								'dueDate',
								$additionalData['boletobancario.dueDate']
							);
						}

						if (isset($additionalData['boletobancario.expirationDate'])) {
							$payment->setAdditionalInformation(
								'expirationDate',
								$additionalData['boletobancario.expirationDate']
							);
						}

						if (isset($additionalData['boletobancario.url'])) {
							$payment->setAdditionalInformation(
								'url',
								$additionalData['boletobancario.url']
							);
						}
					}
					break;
				case "RedirectShopper":
					$payment->setAdditionalInformation('3dActive', true);

					$issuerUrl = $response['redirect']['url'];
					$paReq = $response['redirect']['data']['PaReq'];
					$md = $response['redirect']['data']['MD'];

					if (!empty($paReq) && !empty($md) && !empty($issuerUrl)) {
						$payment->setAdditionalInformation('issuerUrl', $issuerUrl);
						$payment->setAdditionalInformation('paRequest', $paReq);
						$payment->setAdditionalInformation('md', $md);
					} else {
						$isValid = false;
						$errorMsg = __('3D secure is not valid.');
						$this->adyenLogger->error($errorMsg);;
						$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.');
			throw new \Magento\Framework\Exception\LocalizedException(__($errorMsg));
		}

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