/
AfterPaymentSaveObserver.php
136 lines (112 loc) · 4.01 KB
/
AfterPaymentSaveObserver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Vault\Observer;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Vault\Api\Data\PaymentTokenInterface;
use Magento\Vault\Api\PaymentTokenManagementInterface;
use Magento\Vault\Model\Ui\VaultConfigProvider;
/**
* Order payment after save observer for storing payment vault record in db
*/
class AfterPaymentSaveObserver implements ObserverInterface
{
const PAYMENT_OBJECT_DATA_KEY = 'payment';
/**
* @var PaymentTokenManagementInterface
*/
protected $paymentTokenManagement;
/**
* @var EncryptorInterface
*/
protected $encryptor;
/**
* @param PaymentTokenManagementInterface $paymentTokenManagement
* @param EncryptorInterface $encryptor
*/
public function __construct(
PaymentTokenManagementInterface $paymentTokenManagement,
EncryptorInterface $encryptor
) {
$this->paymentTokenManagement = $paymentTokenManagement;
$this->encryptor = $encryptor;
}
/**
* Create payment vault record
*
* @param Observer $observer
* @return $this
*/
public function execute(Observer $observer)
{
/** @var OrderPaymentInterface $payment */
$payment = $observer->getDataByKey(self::PAYMENT_OBJECT_DATA_KEY);
$extensionAttributes = $payment->getExtensionAttributes();
$paymentToken = $this->getPaymentToken($extensionAttributes);
if ($paymentToken === null) {
return $this;
}
if ($paymentToken->getEntityId() !== null) {
$this->paymentTokenManagement->addLinkToOrderPayment(
$paymentToken->getEntityId(),
$payment->getEntityId()
);
return $this;
}
$order = $payment->getOrder();
$paymentToken->setCustomerId($order->getCustomerId());
$paymentToken->setIsActive(true);
$paymentToken->setPaymentMethodCode($payment->getMethod());
$additionalInformation = $payment->getAdditionalInformation();
if (isset($additionalInformation[VaultConfigProvider::IS_ACTIVE_CODE])) {
$paymentToken->setIsVisible(
(bool) (int) $additionalInformation[VaultConfigProvider::IS_ACTIVE_CODE]
);
}
$paymentToken->setPublicHash($this->generatePublicHash($paymentToken));
$this->paymentTokenManagement->saveTokenWithPaymentLink($paymentToken, $payment);
$extensionAttributes->setVaultPaymentToken($paymentToken);
return $this;
}
/**
* Generate vault payment public hash
*
* @param PaymentTokenInterface $paymentToken
* @return string
*/
protected function generatePublicHash(PaymentTokenInterface $paymentToken)
{
$hashKey = $paymentToken->getGatewayToken();
if ($paymentToken->getCustomerId()) {
$hashKey = $paymentToken->getCustomerId();
}
$hashKey .= $paymentToken->getPaymentMethodCode()
. $paymentToken->getType()
. $paymentToken->getTokenDetails();
return $this->encryptor->getHash($hashKey);
}
/**
* Reads Payment token from Order Payment
*
* @param OrderPaymentExtensionInterface | null $extensionAttributes
* @return PaymentTokenInterface | null
*/
protected function getPaymentToken(OrderPaymentExtensionInterface $extensionAttributes = null)
{
if (null === $extensionAttributes) {
return null;
}
/** @var PaymentTokenInterface $paymentToken */
$paymentToken = $extensionAttributes->getVaultPaymentToken();
if (null === $paymentToken || empty($paymentToken->getGatewayToken())) {
return null;
}
return $paymentToken;
}
}