VaultDetailsHandler.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Gateway\Response\PayPal;
  7. use Braintree\Transaction;
  8. use Magento\Braintree\Gateway\SubjectReader;
  9. use Magento\Framework\Intl\DateTimeFactory;
  10. use Magento\Payment\Gateway\Response\HandlerInterface;
  11. use Magento\Payment\Model\InfoInterface;
  12. use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
  13. use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
  14. use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
  15. use Magento\Vault\Api\Data\PaymentTokenInterface;
  16. /**
  17. * Vault Details Handler
  18. */
  19. class VaultDetailsHandler implements HandlerInterface
  20. {
  21. /**
  22. * @var PaymentTokenFactoryInterface
  23. */
  24. private $paymentTokenFactory;
  25. /**
  26. * @var OrderPaymentExtensionInterfaceFactory
  27. */
  28. private $paymentExtensionFactory;
  29. /**
  30. * @var SubjectReader
  31. */
  32. private $subjectReader;
  33. /**
  34. * @var DateTimeFactory
  35. */
  36. private $dateTimeFactory;
  37. /**
  38. * @param PaymentTokenFactoryInterface $paymentTokenFactory
  39. * @param OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory
  40. * @param SubjectReader $subjectReader
  41. * @param DateTimeFactory $dateTimeFactory
  42. */
  43. public function __construct(
  44. PaymentTokenFactoryInterface $paymentTokenFactory,
  45. OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory,
  46. SubjectReader $subjectReader,
  47. DateTimeFactory $dateTimeFactory
  48. ) {
  49. $this->paymentTokenFactory = $paymentTokenFactory;
  50. $this->paymentExtensionFactory = $paymentExtensionFactory;
  51. $this->subjectReader = $subjectReader;
  52. $this->dateTimeFactory = $dateTimeFactory;
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. public function handle(array $handlingSubject, array $response)
  58. {
  59. $paymentDO = $this->subjectReader->readPayment($handlingSubject);
  60. $transaction = $this->subjectReader->readTransaction($response);
  61. $payment = $paymentDO->getPayment();
  62. // add vault payment token entity to extension attributes
  63. $paymentToken = $this->getVaultPaymentToken($transaction);
  64. if ($paymentToken !== null) {
  65. $extensionAttributes = $this->getExtensionAttributes($payment);
  66. $extensionAttributes->setVaultPaymentToken($paymentToken);
  67. }
  68. }
  69. /**
  70. * Get vault payment token entity
  71. *
  72. * @param \Braintree\Transaction $transaction
  73. * @return PaymentTokenInterface|null
  74. */
  75. private function getVaultPaymentToken(Transaction $transaction)
  76. {
  77. // Check token existing in gateway response
  78. $token = $transaction->paypalDetails->token;
  79. if (empty($token)) {
  80. return null;
  81. }
  82. /** @var PaymentTokenInterface $paymentToken */
  83. $paymentToken = $this->paymentTokenFactory->create(PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT);
  84. $paymentToken->setGatewayToken($token);
  85. $paymentToken->setExpiresAt($this->getExpirationDate());
  86. $details = json_encode([
  87. 'payerEmail' => $transaction->paypalDetails->payerEmail
  88. ]);
  89. $paymentToken->setTokenDetails($details);
  90. return $paymentToken;
  91. }
  92. /**
  93. * @return string
  94. */
  95. private function getExpirationDate()
  96. {
  97. $expDate = $this->dateTimeFactory->create('now', new \DateTimeZone('UTC'));
  98. $expDate->add(new \DateInterval('P1Y'));
  99. return $expDate->format('Y-m-d 00:00:00');
  100. }
  101. /**
  102. * Get payment extension attributes
  103. * @param InfoInterface $payment
  104. * @return OrderPaymentExtensionInterface
  105. */
  106. private function getExtensionAttributes(InfoInterface $payment)
  107. {
  108. $extensionAttributes = $payment->getExtensionAttributes();
  109. if ($extensionAttributes === null) {
  110. $extensionAttributes = $this->paymentExtensionFactory->create();
  111. $payment->setExtensionAttributes($extensionAttributes);
  112. }
  113. return $extensionAttributes;
  114. }
  115. }