Commit f7238c3a authored by Rik ter Beek's avatar Rik ter Beek Committed by GitHub

Merge pull request #294 from ho-nl-fork/feature/vault

Vault support for 3D secure payments
parents 46bc4c90 b9f677ea
......@@ -23,6 +23,13 @@
namespace Adyen\Payment\Controller\Process;
use Magento\Payment\Model\InfoInterface;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
use Magento\Sales\Model\ResourceModel\Order\Payment as OrderPaymentResource;
class Validate3d extends \Magento\Framework\App\Action\Action
{
/**
......@@ -55,6 +62,21 @@ class Validate3d extends \Magento\Framework\App\Action\Action
*/
protected $_orderRepository;
/**
* @var PaymentTokenFactoryInterface
*/
private $paymentTokenFactory;
/**
* @var OrderPaymentExtensionInterfaceFactory
*/
private $paymentExtensionFactory;
/**
* @var OrderPaymentResource
*/
private $orderPaymentResource;
/**
* Validate3d constructor.
*
......@@ -62,19 +84,29 @@ class Validate3d extends \Magento\Framework\App\Action\Action
* @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger
* @param \Adyen\Payment\Helper\Data $adyenHelper
* @param \Adyen\Payment\Model\Api\PaymentRequest $paymentRequest
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
* @param PaymentTokenFactoryInterface $paymentTokenFactory
* @param OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory
* @param OrderPaymentResource $orderPaymentResource
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Adyen\Payment\Helper\Data $adyenHelper,
\Adyen\Payment\Model\Api\PaymentRequest $paymentRequest,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
PaymentTokenFactoryInterface $paymentTokenFactory,
OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory,
OrderPaymentResource $orderPaymentResource
) {
parent::__construct($context);
$this->_adyenLogger = $adyenLogger;
$this->_adyenHelper = $adyenHelper;
$this->_paymentRequest = $paymentRequest;
$this->_orderRepository = $orderRepository;
$this->paymentTokenFactory = $paymentTokenFactory;
$this->paymentExtensionFactory = $paymentExtensionFactory;
$this->orderPaymentResource = $orderPaymentResource;
}
/**
......@@ -120,21 +152,53 @@ class Validate3d extends \Magento\Framework\App\Action\Action
// );
// old fashion way:
$result = $this->_authorise3d($order->getPayment());
$resultCode = $result['resultCode'];
} catch (\Exception $e) {
$this->_adyenLogger->addAdyenResult("Process 3D secure payment was refused");
$result = 'Refused';
$resultCode = 'Refused';
}
$this->_adyenLogger->addAdyenResult("Process 3D secure payment result is: " . $result);
$this->_adyenLogger->addAdyenResult("Process 3D secure payment result is: " . $resultCode);
// check if authorise3d was successful
if ($result == 'Authorised') {
if ($resultCode == 'Authorised') {
$order->addStatusHistoryComment(__('3D-secure validation was successful'))->save();
// set back to false so when pressed back button on the success page it will reactivate 3D secure
$order->getPayment()->setAdditionalInformation('3dActive', '');
$order->getPayment()->setAdditionalInformation('3dSuccess', true);
$this->_orderRepository->save($order);
try {
$additionalData = $result['additionalData'];
$token = $additionalData['recurring.recurringDetailReference'];
$expirationDate = $additionalData['expiryDate'];
$cardType = $additionalData['paymentMethod'];
$cardSummary = $additionalData['cardSummary'];
/** @var PaymentTokenInterface $paymentToken */
$paymentToken = $this->paymentTokenFactory->create(PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD);
$paymentToken->setGatewayToken($token);
$paymentToken->setExpiresAt($this->getExpirationDate($expirationDate));
$details = [
'type' => $cardType,
'maskedCC' => $cardSummary,
'expirationDate' => $expirationDate
];
$paymentToken->setTokenDetails(json_encode($details));
$extensionAttributes = $this->getExtensionAttributes($order->getPayment());
$extensionAttributes->setVaultPaymentToken($paymentToken);
$orderPayment = $order->getPayment()->setExtensionAttributes($extensionAttributes);
$add = unserialize($orderPayment->getAdditionalData());
$add['force_save'] = true;
$orderPayment->setAdditionalData(serialize($add));
$this->orderPaymentResource->save($orderPayment);
} catch(\Exception $e) {
$this->_adyenLogger->error((string)$e->getMessage());
}
$this->_redirect('checkout/onepage/success', ['_query' => ['utm_nooverride' => '1']]);
} else {
$order->addStatusHistoryComment(__('3D-secure validation was unsuccessful.'))->save();
......@@ -169,6 +233,48 @@ class Validate3d extends \Magento\Framework\App\Action\Action
}
}
/**
* Get payment extension attributes
* @param InfoInterface $payment
* @return OrderPaymentExtensionInterface
*/
private function getExtensionAttributes(InfoInterface $payment)
{
$extensionAttributes = $payment->getExtensionAttributes();
if (null === $extensionAttributes) {
$extensionAttributes = $this->paymentExtensionFactory->create();
$payment->setExtensionAttributes($extensionAttributes);
}
return $extensionAttributes;
}
/**
* @param $expirationDate
* @return string
*/
private function getExpirationDate($expirationDate)
{
$expirationDate = explode('/', $expirationDate);
//add leading zero to month
$month = sprintf("%02d", $expirationDate[0]);
$expDate = new \DateTime(
$expirationDate[1]
. '-'
. $month
. '-'
. '01'
. ' '
. '00:00:00',
new \DateTimeZone('UTC')
);
// add one month
$expDate->add(new \DateInterval('P1M'));
return $expDate->format('Y-m-d 00:00:00');
}
/**
* Called by validate3d controller when cc payment has 3D secure
*
......@@ -183,8 +289,7 @@ class Validate3d extends \Magento\Framework\App\Action\Action
} catch(\Exception $e) {
throw $e;
}
$responseCode = $response['resultCode'];
return $responseCode;
return $response;
}
/**
......
......@@ -16,7 +16,7 @@
"require": {
"adyen/php-api-library": "*",
"magento/framework": ">=100.1.0",
"magento/module-vault": "100.1.*|100.2.*"
"magento/module-vault": "101.0.*"
},
"autoload": {
"files": [
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment