123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Braintree\Gateway\Response\PayPal;
- use Braintree\Transaction;
- use Magento\Braintree\Gateway\SubjectReader;
- use Magento\Framework\Intl\DateTimeFactory;
- use Magento\Payment\Gateway\Response\HandlerInterface;
- use Magento\Payment\Model\InfoInterface;
- use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
- use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
- use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
- use Magento\Vault\Api\Data\PaymentTokenInterface;
- /**
- * Vault Details Handler
- */
- class VaultDetailsHandler implements HandlerInterface
- {
- /**
- * @var PaymentTokenFactoryInterface
- */
- private $paymentTokenFactory;
- /**
- * @var OrderPaymentExtensionInterfaceFactory
- */
- private $paymentExtensionFactory;
- /**
- * @var SubjectReader
- */
- private $subjectReader;
- /**
- * @var DateTimeFactory
- */
- private $dateTimeFactory;
- /**
- * @param PaymentTokenFactoryInterface $paymentTokenFactory
- * @param OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory
- * @param SubjectReader $subjectReader
- * @param DateTimeFactory $dateTimeFactory
- */
- public function __construct(
- PaymentTokenFactoryInterface $paymentTokenFactory,
- OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory,
- SubjectReader $subjectReader,
- DateTimeFactory $dateTimeFactory
- ) {
- $this->paymentTokenFactory = $paymentTokenFactory;
- $this->paymentExtensionFactory = $paymentExtensionFactory;
- $this->subjectReader = $subjectReader;
- $this->dateTimeFactory = $dateTimeFactory;
- }
- /**
- * @inheritdoc
- */
- public function handle(array $handlingSubject, array $response)
- {
- $paymentDO = $this->subjectReader->readPayment($handlingSubject);
- $transaction = $this->subjectReader->readTransaction($response);
- $payment = $paymentDO->getPayment();
- // add vault payment token entity to extension attributes
- $paymentToken = $this->getVaultPaymentToken($transaction);
- if ($paymentToken !== null) {
- $extensionAttributes = $this->getExtensionAttributes($payment);
- $extensionAttributes->setVaultPaymentToken($paymentToken);
- }
- }
- /**
- * Get vault payment token entity
- *
- * @param \Braintree\Transaction $transaction
- * @return PaymentTokenInterface|null
- */
- private function getVaultPaymentToken(Transaction $transaction)
- {
- // Check token existing in gateway response
- $token = $transaction->paypalDetails->token;
- if (empty($token)) {
- return null;
- }
- /** @var PaymentTokenInterface $paymentToken */
- $paymentToken = $this->paymentTokenFactory->create(PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT);
- $paymentToken->setGatewayToken($token);
- $paymentToken->setExpiresAt($this->getExpirationDate());
- $details = json_encode([
- 'payerEmail' => $transaction->paypalDetails->payerEmail
- ]);
- $paymentToken->setTokenDetails($details);
- return $paymentToken;
- }
- /**
- * @return string
- */
- private function getExpirationDate()
- {
- $expDate = $this->dateTimeFactory->create('now', new \DateTimeZone('UTC'));
- $expDate->add(new \DateInterval('P1Y'));
- return $expDate->format('Y-m-d 00:00:00');
- }
- /**
- * Get payment extension attributes
- * @param InfoInterface $payment
- * @return OrderPaymentExtensionInterface
- */
- private function getExtensionAttributes(InfoInterface $payment)
- {
- $extensionAttributes = $payment->getExtensionAttributes();
- if ($extensionAttributes === null) {
- $extensionAttributes = $this->paymentExtensionFactory->create();
- $payment->setExtensionAttributes($extensionAttributes);
- }
- return $extensionAttributes;
- }
- }
|