We will be off from 27/1 (Monday) to 31/1 (Friday) (GMT +7) for our Tet Holiday (Lunar New Year) in our country

Commit 5aa57ee7 authored by attilak's avatar attilak

Fix vault storing for updated expiration date

parent 83a35dd9
...@@ -26,6 +26,10 @@ namespace Adyen\Payment\Gateway\Response; ...@@ -26,6 +26,10 @@ namespace Adyen\Payment\Gateway\Response;
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 Adyen\Payment\Api\Data\OrderPaymentInterface;
use Magento\Payment\Gateway\Data\PaymentDataObject;
use Magento\Vault\Api\PaymentTokenRepositoryInterface;
class VaultDetailsHandler implements HandlerInterface class VaultDetailsHandler implements HandlerInterface
{ {
...@@ -45,6 +49,16 @@ class VaultDetailsHandler implements HandlerInterface ...@@ -45,6 +49,16 @@ class VaultDetailsHandler implements HandlerInterface
*/ */
private $adyenHelper; private $adyenHelper;
/**
* @var PaymentTokenManagement
*/
private $paymentTokenManagement;
/**
* @var
*/
private $paymentTokenRepository;
/** /**
* VaultDetailsHandler constructor. * VaultDetailsHandler constructor.
* *
...@@ -55,11 +69,15 @@ class VaultDetailsHandler implements HandlerInterface ...@@ -55,11 +69,15 @@ class VaultDetailsHandler implements HandlerInterface
public function __construct( public function __construct(
PaymentTokenFactoryInterface $paymentTokenFactory, PaymentTokenFactoryInterface $paymentTokenFactory,
\Adyen\Payment\Logger\AdyenLogger $adyenLogger, \Adyen\Payment\Logger\AdyenLogger $adyenLogger,
\Adyen\Payment\Helper\Data $adyenHelper \Adyen\Payment\Helper\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,14 +85,14 @@ class VaultDetailsHandler implements HandlerInterface ...@@ -67,14 +85,14 @@ 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 @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);
...@@ -87,9 +105,10 @@ class VaultDetailsHandler implements HandlerInterface ...@@ -87,9 +105,10 @@ 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; $paymentToken = null;
...@@ -136,16 +155,29 @@ class VaultDetailsHandler implements HandlerInterface ...@@ -136,16 +155,29 @@ class VaultDetailsHandler implements HandlerInterface
$cardType = $additionalData['paymentMethod']; $cardType = $additionalData['paymentMethod'];
try { try {
// Check if paymentToken exists already
$paymentToken = $this->paymentTokenManagement->getByGatewayToken($token, $payment->getMethodInstance()->getCode(), $payment->getOrder()->getCustomerId());
$paymentTokenSaveRequired = false;
// 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
); );
$paymentToken->setGatewayToken($token);
if (strpos($cardType, "paywithgoogle") !== false && !empty($additionalData['paymentMethodVariant'])) { if (strpos($cardType, "paywithgoogle") !== false && !empty($additionalData['paymentMethodVariant'])) {
$cardType = $additionalData['paymentMethodVariant']; $cardType = $additionalData['paymentMethodVariant'];
$paymentToken->setIsVisible(false); $paymentToken->setIsVisible(false);
} }
$paymentToken->setGatewayToken($token); } else {
$paymentTokenSaveRequired = true;
}
$paymentToken->setExpiresAt($this->getExpirationDate($expirationDate)); $paymentToken->setExpiresAt($this->getExpirationDate($expirationDate));
$details = [ $details = [
...@@ -155,6 +187,11 @@ class VaultDetailsHandler implements HandlerInterface ...@@ -155,6 +187,11 @@ class VaultDetailsHandler implements HandlerInterface
]; ];
$paymentToken->setTokenDetails(json_encode($details)); $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) { } catch (\Exception $e) {
$this->adyenLogger->error(print_r($e, true)); $this->adyenLogger->error(print_r($e, true));
} }
......
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