Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VaultDetailsHandler.php
Go to the documentation of this file.
1 <?php
7 
8 use Braintree\Transaction;
13 use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
14 use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
17 
22 {
26  private $paymentTokenFactory;
27 
31  private $paymentExtensionFactory;
32 
36  private $subjectReader;
37 
41  private $dateTimeFactory;
42 
49  public function __construct(
50  PaymentTokenFactoryInterface $paymentTokenFactory,
51  OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory,
52  SubjectReader $subjectReader,
53  DateTimeFactory $dateTimeFactory
54  ) {
55  $this->paymentTokenFactory = $paymentTokenFactory;
56  $this->paymentExtensionFactory = $paymentExtensionFactory;
57  $this->subjectReader = $subjectReader;
58  $this->dateTimeFactory = $dateTimeFactory;
59  }
60 
64  public function handle(array $handlingSubject, array $response)
65  {
66  $paymentDO = $this->subjectReader->readPayment($handlingSubject);
67  $transaction = $this->subjectReader->readTransaction($response);
68  $payment = $paymentDO->getPayment();
69 
70  // add vault payment token entity to extension attributes
71  $paymentToken = $this->getVaultPaymentToken($transaction);
72  if ($paymentToken !== null) {
73  $extensionAttributes = $this->getExtensionAttributes($payment);
74  $extensionAttributes->setVaultPaymentToken($paymentToken);
75  }
76  }
77 
84  private function getVaultPaymentToken(Transaction $transaction)
85  {
86  // Check token existing in gateway response
87  $token = $transaction->paypalDetails->token;
88  if (empty($token)) {
89  return null;
90  }
91 
93  $paymentToken = $this->paymentTokenFactory->create(PaymentTokenFactoryInterface::TOKEN_TYPE_ACCOUNT);
94  $paymentToken->setGatewayToken($token);
95  $paymentToken->setExpiresAt($this->getExpirationDate());
96  $details = json_encode([
97  'payerEmail' => $transaction->paypalDetails->payerEmail
98  ]);
99  $paymentToken->setTokenDetails($details);
100 
101  return $paymentToken;
102  }
103 
107  private function getExpirationDate()
108  {
109  $expDate = $this->dateTimeFactory->create('now', new \DateTimeZone('UTC'));
110  $expDate->add(new \DateInterval('P1Y'));
111  return $expDate->format('Y-m-d 00:00:00');
112  }
113 
119  private function getExtensionAttributes(InfoInterface $payment)
120  {
121  $extensionAttributes = $payment->getExtensionAttributes();
122  if ($extensionAttributes === null) {
123  $extensionAttributes = $this->paymentExtensionFactory->create();
124  $payment->setExtensionAttributes($extensionAttributes);
125  }
126  return $extensionAttributes;
127  }
128 }
$transaction
$response
Definition: 404.php:11
$details
Definition: vault.phtml:10
$payment
Definition: order.php:17
$extensionAttributes
Definition: payment.php:22
__construct(PaymentTokenFactoryInterface $paymentTokenFactory, OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory, SubjectReader $subjectReader, DateTimeFactory $dateTimeFactory)
$paymentExtensionFactory
Definition: payment.php:21