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;
15 use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
16 use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
19 
25 {
30 
35 
39  protected $subjectReader;
40 
44  protected $config;
45 
49  private $serializer;
50 
61  public function __construct(
63  OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory,
66  Json $serializer = null
67  ) {
68  $this->paymentTokenFactory = $paymentTokenFactory;
69  $this->paymentExtensionFactory = $paymentExtensionFactory;
70  $this->config = $config;
71  $this->subjectReader = $subjectReader;
72  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
73  }
74 
78  public function handle(array $handlingSubject, array $response)
79  {
80  $paymentDO = $this->subjectReader->readPayment($handlingSubject);
81  $transaction = $this->subjectReader->readTransaction($response);
82  $payment = $paymentDO->getPayment();
83 
84  // add vault payment token entity to extension attributes
85  $paymentToken = $this->getVaultPaymentToken($transaction);
86  if (null !== $paymentToken) {
87  $extensionAttributes = $this->getExtensionAttributes($payment);
88  $extensionAttributes->setVaultPaymentToken($paymentToken);
89  }
90  }
91 
98  protected function getVaultPaymentToken(Transaction $transaction)
99  {
100  // Check token existing in gateway response
101  $token = $transaction->creditCardDetails->token;
102  if (empty($token)) {
103  return null;
104  }
105 
107  $paymentToken = $this->paymentTokenFactory->create(PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD);
108  $paymentToken->setGatewayToken($token);
109  $paymentToken->setExpiresAt($this->getExpirationDate($transaction));
110 
111  $paymentToken->setTokenDetails($this->convertDetailsToJSON([
112  'type' => $this->getCreditCardType($transaction->creditCardDetails->cardType),
113  'maskedCC' => $transaction->creditCardDetails->last4,
114  'expirationDate' => $transaction->creditCardDetails->expirationDate
115  ]));
116 
117  return $paymentToken;
118  }
119 
124  private function getExpirationDate(Transaction $transaction)
125  {
126  $expDate = new \DateTime(
127  $transaction->creditCardDetails->expirationYear
128  . '-'
129  . $transaction->creditCardDetails->expirationMonth
130  . '-'
131  . '01'
132  . ' '
133  . '00:00:00',
134  new \DateTimeZone('UTC')
135  );
136  $expDate->add(new \DateInterval('P1M'));
137  return $expDate->format('Y-m-d 00:00:00');
138  }
139 
145  private function convertDetailsToJSON($details)
146  {
147  $json = $this->serializer->serialize($details);
148  return $json ? $json : '{}';
149  }
150 
157  private function getCreditCardType($type)
158  {
159  $replaced = str_replace(' ', '-', strtolower($type));
160  $mapper = $this->config->getCcTypesMapper();
161 
162  return $mapper[$replaced];
163  }
164 
170  private function getExtensionAttributes(InfoInterface $payment)
171  {
172  $extensionAttributes = $payment->getExtensionAttributes();
173  if (null === $extensionAttributes) {
174  $extensionAttributes = $this->paymentExtensionFactory->create();
175  $payment->setExtensionAttributes($extensionAttributes);
176  }
177  return $extensionAttributes;
178  }
179 }
$transaction
__construct(PaymentTokenFactoryInterface $paymentTokenFactory, OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory, Config $config, SubjectReader $subjectReader, Json $serializer=null)
$response
Definition: 404.php:11
handle(array $handlingSubject, array $response)
$details
Definition: vault.phtml:10
$payment
Definition: order.php:17
$type
Definition: item.phtml:13
$extensionAttributes
Definition: payment.php:22