Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PaymentTokenAssigner.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Vault\Observer;
7 
15 
17 {
21  private $paymentTokenManagement;
22 
27  public function __construct(
28  PaymentTokenManagementInterface $paymentTokenManagement
29  ) {
30  $this->paymentTokenManagement = $paymentTokenManagement;
31  }
32 
37  public function execute(Observer $observer)
38  {
39  $dataObject = $this->readDataArgument($observer);
40 
41  $additionalData = $dataObject->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
42 
43  if (!is_array($additionalData) || !isset($additionalData[PaymentTokenInterface::PUBLIC_HASH])) {
44  return;
45  }
46 
47  $tokenPublicHash = $additionalData[PaymentTokenInterface::PUBLIC_HASH];
48 
49  if ($tokenPublicHash === null) {
50  return;
51  }
52 
54  $paymentModel = $this->readPaymentModelArgument($observer);
55  if (!$paymentModel instanceof Payment) {
56  return;
57  }
58 
59  $quote = $paymentModel->getQuote();
60  $customerId = (int) $quote->getCustomer()->getId();
61 
62  $paymentToken = $this->paymentTokenManagement->getByPublicHash($tokenPublicHash, $customerId);
63  if ($paymentToken === null) {
64  return;
65  }
66 
67  $paymentModel->setAdditionalInformation(
68  [
70  PaymentTokenInterface::PUBLIC_HASH => $tokenPublicHash
71  ]
72  );
73  }
74 }
$quote
__construct(PaymentTokenManagementInterface $paymentTokenManagement)