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

Commit 1f0bd282 authored by Attila Kiss's avatar Attila Kiss Committed by GitHub

Merge pull request #718 from Adyen/PW-2383

[PW-2383] Fix vault storing for updated expiration date
parents 5842e362 66c2bdd3
...@@ -23,43 +23,79 @@ ...@@ -23,43 +23,79 @@
namespace Adyen\Payment\Gateway\Response; namespace Adyen\Payment\Gateway\Response;
use Adyen\Payment\Helper\Data;
use Adyen\Payment\Logger\AdyenLogger;
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface; use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
use Magento\Payment\Gateway\Response\HandlerInterface; use Magento\Payment\Gateway\Response\HandlerInterface;
use Magento\Payment\Model\InfoInterface; use Magento\Payment\Model\InfoInterface;
use Magento\Vault\Model\PaymentTokenManagement;
use Magento\Payment\Gateway\Data\PaymentDataObject;
use Magento\Vault\Api\PaymentTokenRepositoryInterface;
class VaultDetailsHandler implements HandlerInterface class VaultDetailsHandler implements HandlerInterface
{ {
const RECURRING_DETAIL_REFERENCE = 'recurring.recurringDetailReference';
const CARD_SUMMARY = 'cardSummary';
const EXPIRY_DATE = 'expiryDate';
const PAYMENT_METHOD = 'paymentMethod';
const ADDITIONAL_DATA_ERRORS = array(
self::RECURRING_DETAIL_REFERENCE => 'Missing Token in Result please enable in ' .
'Settings -> API URLs and Response menu in the Adyen Customer Area Recurring details setting',
self::CARD_SUMMARY => 'Missing cardSummary in Result please login to the adyen portal ' .
'and go to Settings -> API URLs and Response and enable the Card summary property',
self::EXPIRY_DATE => 'Missing expiryDate in Result please login to the adyen portal and go to ' .
'Settings -> API URLs and Response and enable the Expiry date property',
self::PAYMENT_METHOD => 'Missing paymentMethod in Result please login to the adyen portal and go to ' .
'Settings -> API URLs and Response and enable the Variant property'
);
/** /**
* @var PaymentTokenInterfaceFactory * @var PaymentTokenFactoryInterface
*/ */
protected $paymentTokenFactory; protected $paymentTokenFactory;
/** /**
* @var \Adyen\Payment\Logger\AdyenLogger * @var AdyenLogger
*/ */
private $adyenLogger; private $adyenLogger;
/** /**
* @var \Adyen\Payment\Helper\Data * @var Data
*/ */
private $adyenHelper; private $adyenHelper;
/**
* @var PaymentTokenManagement
*/
private $paymentTokenManagement;
/**
* @var
*/
private $paymentTokenRepository;
/** /**
* VaultDetailsHandler constructor. * VaultDetailsHandler constructor.
* *
* @param PaymentTokenFactoryInterface $paymentTokenFactory * @param PaymentTokenFactoryInterface $paymentTokenFactory
* @param \Adyen\Payment\Logger\AdyenLogger $adyenLogger * @param AdyenLogger $adyenLogger
* @param \Adyen\Payment\Helper\Data $adyenHelper * @param Data $adyenHelper
* @param PaymentTokenManagement $paymentTokenManagement
* @param PaymentTokenRepositoryInterface $paymentTokenRepository
*/ */
public function __construct( public function __construct(
PaymentTokenFactoryInterface $paymentTokenFactory, PaymentTokenFactoryInterface $paymentTokenFactory,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger, AdyenLogger $adyenLogger,
\Adyen\Payment\Helper\Data $adyenHelper Data $adyenHelper,
PaymentTokenManagement $paymentTokenManagement,
PaymentTokenRepositoryInterface $paymentTokenRepository
) { ) {
$this->adyenLogger = $adyenLogger; $this->adyenLogger = $adyenLogger;
$this->adyenHelper = $adyenHelper; $this->adyenHelper = $adyenHelper;
$this->paymentTokenFactory = $paymentTokenFactory; $this->paymentTokenFactory = $paymentTokenFactory;
$this->paymentTokenManagement = $paymentTokenManagement;
$this->paymentTokenRepository = $paymentTokenRepository;
} }
/** /**
...@@ -67,18 +103,23 @@ class VaultDetailsHandler implements HandlerInterface ...@@ -67,18 +103,23 @@ class VaultDetailsHandler implements HandlerInterface
*/ */
public function handle(array $handlingSubject, array $response) public function handle(array $handlingSubject, array $response)
{ {
$payment = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($handlingSubject); /** @var PaymentDataObject $orderPayment */
$orderPayment = \Magento\Payment\Gateway\Helper\SubjectReader::readPayment($handlingSubject);
/** @var \Adyen\Payment\Api\Data\OrderPaymentInterface $payment */ $payment = $orderPayment->getPayment();
$payment = $payment->getPayment();
if ($this->adyenHelper->isCreditCardVaultEnabled($payment->getOrder()->getStoreId())) { if ($this->adyenHelper->isCreditCardVaultEnabled($payment->getOrder()->getStoreId())) {
// add vault payment token entity to extension attributes // add vault payment token entity to extension attributes
$paymentToken = $this->getVaultPaymentToken($response); $paymentToken = $this->getVaultPaymentToken($response, $payment);
if (null !== $paymentToken) { if (null !== $paymentToken) {
$extensionAttributes = $this->getExtensionAttributes($payment); $extensionAttributes = $this->getExtensionAttributes($payment);
$extensionAttributes->setVaultPaymentToken($paymentToken); $extensionAttributes->setVaultPaymentToken($paymentToken);
} else {
$this->adyenLogger->error(
sprintf('Failure trying to save credit card token in vault for order %s',
$payment->getOrder()->getIncrementId())
);
} }
} }
} }
...@@ -87,77 +128,69 @@ class VaultDetailsHandler implements HandlerInterface ...@@ -87,77 +128,69 @@ class VaultDetailsHandler implements HandlerInterface
* Get vault payment token entity * Get vault payment token entity
* *
* @param array $response * @param array $response
* @param $payment
* @return PaymentTokenInterface|null * @return PaymentTokenInterface|null
*/ */
private function getVaultPaymentToken(array $response) private function getVaultPaymentToken(array $response, $payment)
{ {
$paymentToken = null;
if (!empty($response['additionalData'])) {
$additionalData = $response['additionalData'];
if (empty($response['additionalData'])) {
return null;
}
if (empty($additionalData['recurring.recurringDetailReference'])) { $additionalData = $response['additionalData'];
$this->adyenLogger->error(
'Missing Token in Result please enable in ' .
'Settings -> API URLs and Response menu in the Adyen Customer Area Recurring details setting'
);
return null;
}
$token = $additionalData['recurring.recurringDetailReference'];
$paymentToken = null;
if (empty($additionalData['cardSummary'])) { foreach (self::ADDITIONAL_DATA_ERRORS as $key => $errorMsg) {
$this->adyenLogger->error( if (empty($additionalData[$key])) {
'Missing cardSummary in Result please login to the adyen portal ' . $this->adyenLogger->error($errorMsg);
'and go to Settings -> API URLs and Response and enable the Card summary property'
);
return null; return null;
} }
$cardSummary = $additionalData['cardSummary']; }
if (empty($additionalData['expiryDate'])) { try {
$this->adyenLogger->error(
'Missing expiryDate in Result please login to the adyen portal and go to ' .
'Settings -> API URLs and Response and enable the Expiry date property'
);
return null;
}
$expirationDate = $additionalData['expiryDate'];
if (empty($additionalData['paymentMethod'])) { // Check if paymentToken exists already
$this->adyenLogger->error( $paymentToken = $this->paymentTokenManagement->getByGatewayToken($additionalData[self::RECURRING_DETAIL_REFERENCE],
'Missing paymentMethod in Result please login to the adyen portal and go to ' . $payment->getMethodInstance()->getCode(), $payment->getOrder()->getCustomerId());
'Settings -> API URLs and Response and enable the Variant property'
);
return null;
}
$cardType = $additionalData['paymentMethod']; $paymentTokenSaveRequired = false;
try { // In case the payment token does not exist, create it based on the additionalData
if (is_null($paymentToken)) {
/** @var PaymentTokenInterface $paymentToken */ /** @var PaymentTokenInterface $paymentToken */
$paymentToken = $this->paymentTokenFactory->create( $paymentToken = $this->paymentTokenFactory->create(
PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD
); );
if (strpos($cardType, "paywithgoogle") !== false && !empty($additionalData['paymentMethodVariant'])) { $paymentToken->setGatewayToken($additionalData[self::RECURRING_DETAIL_REFERENCE]);
$cardType = $additionalData['paymentMethodVariant'];
if (strpos($additionalData[self::PAYMENT_METHOD], "paywithgoogle") !== false
&& !empty($additionalData['paymentMethodVariant'])) {
$additionalData[self::PAYMENT_METHOD] = $additionalData['paymentMethodVariant'];
$paymentToken->setIsVisible(false); $paymentToken->setIsVisible(false);
} }
$paymentToken->setGatewayToken($token); } else {
$paymentToken->setExpiresAt($this->getExpirationDate($expirationDate)); $paymentTokenSaveRequired = true;
}
$details = [
'type' => $cardType, $paymentToken->setExpiresAt($this->getExpirationDate($additionalData[self::EXPIRY_DATE]));
'maskedCC' => $cardSummary,
'expirationDate' => $expirationDate $details = [
]; 'type' => $additionalData[self::PAYMENT_METHOD],
'maskedCC' => $additionalData[self::CARD_SUMMARY],
$paymentToken->setTokenDetails(json_encode($details)); 'expirationDate' => $additionalData[self::EXPIRY_DATE]
} catch (\Exception $e) { ];
$this->adyenLogger->error(print_r($e, true));
$paymentToken->setTokenDetails(json_encode($details));
// If the token is updated, it needs to be saved to keep the changes
if ($paymentTokenSaveRequired) {
$this->paymentTokenRepository->save($paymentToken);
} }
} catch (\Exception $e) {
$this->adyenLogger->error(print_r($e, true));
} }
return $paymentToken; return $paymentToken;
......
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