|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright © Getnet. All rights reserved. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Bruno Elisei <[email protected]> |
|
6
|
|
|
* See LICENSE for license details. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Getnet\PaymentMagento\Gateway\Response; |
|
10
|
|
|
|
|
11
|
|
|
use Getnet\PaymentMagento\Gateway\Config\ConfigCc; |
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
use Magento\Framework\ObjectManagerInterface; |
|
14
|
|
|
use Magento\Framework\Serialize\Serializer\Json; |
|
15
|
|
|
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface; |
|
16
|
|
|
use Magento\Payment\Gateway\Response\HandlerInterface; |
|
17
|
|
|
use Magento\Payment\Model\InfoInterface; |
|
18
|
|
|
use Magento\Sales\Api\Data\OrderPaymentExtensionInterface; |
|
|
|
|
|
|
19
|
|
|
use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory; |
|
|
|
|
|
|
20
|
|
|
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface; |
|
21
|
|
|
use Magento\Vault\Api\Data\PaymentTokenInterface; |
|
22
|
|
|
use Magento\Vault\Api\Data\PaymentTokenInterfaceFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
26
|
|
|
*/ |
|
27
|
|
|
class VaultDetailsHandler implements HandlerInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Response Pay Credit - Block name. |
|
31
|
|
|
*/ |
|
32
|
|
|
public const CREDIT = 'credit'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Response Brand - Block name. |
|
36
|
|
|
*/ |
|
37
|
|
|
public const BRAND = 'brand'; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var PaymentTokenInterfaceFactory |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $paymentTokenFactory; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var OrderPaymentExtensionInterfaceFactory |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $payExtensionFactory; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var ObjectManagerInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
private $objectManager; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var Json |
|
55
|
|
|
*/ |
|
56
|
|
|
private $json; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var ConfigCc |
|
60
|
|
|
*/ |
|
61
|
|
|
private $configCc; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Json $json |
|
65
|
|
|
* @param ObjectManagerInterface $objectManager |
|
66
|
|
|
* @param OrderPaymentExtensionInterfaceFactory $payExtensionFactory |
|
67
|
|
|
* @param ConfigCc $configCc |
|
68
|
|
|
* @param PaymentTokenFactoryInterface $paymentTokenFactory |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __construct( |
|
71
|
|
|
Json $json, |
|
72
|
|
|
ObjectManagerInterface $objectManager, |
|
73
|
|
|
OrderPaymentExtensionInterfaceFactory $payExtensionFactory, |
|
74
|
|
|
ConfigCc $configCc, |
|
75
|
|
|
PaymentTokenFactoryInterface $paymentTokenFactory = null |
|
76
|
|
|
) { |
|
77
|
|
|
if ($paymentTokenFactory === null) { |
|
78
|
|
|
$paymentTokenFactory = $objectManager->get(PaymentTokenFactoryInterface::class); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->objectManager = $objectManager; |
|
82
|
|
|
$this->payExtensionFactory = $payExtensionFactory; |
|
83
|
|
|
$this->paymentTokenFactory = $paymentTokenFactory; |
|
|
|
|
|
|
84
|
|
|
$this->json = $json; |
|
85
|
|
|
$this->configCc = $configCc; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Handle. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $handlingSubject |
|
92
|
|
|
* @param array $response |
|
93
|
|
|
* |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
public function handle(array $handlingSubject, array $response) |
|
97
|
|
|
{ |
|
98
|
|
|
if (!isset($handlingSubject['payment']) |
|
99
|
|
|
|| !$handlingSubject['payment'] instanceof PaymentDataObjectInterface |
|
100
|
|
|
) { |
|
101
|
|
|
throw new InvalidArgumentException('Payment data object should be provided'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$paymentDO = $handlingSubject['payment']; |
|
105
|
|
|
|
|
106
|
|
|
$payment = $paymentDO->getPayment(); |
|
107
|
|
|
$paymentToken = $this->getVaultPaymentToken($payment, $response); |
|
108
|
|
|
if (null !== $paymentToken) { |
|
109
|
|
|
$extensionAttributes = $this->getExtensionAttributes($payment); |
|
110
|
|
|
$extensionAttributes->setVaultPaymentToken($paymentToken); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get vault payment token entity. |
|
116
|
|
|
* |
|
117
|
|
|
* @param InfoInterface $payment |
|
118
|
|
|
* @param array $response |
|
119
|
|
|
* |
|
120
|
|
|
* @return PaymentTokenInterface|null |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function getVaultPaymentToken($payment, $response) |
|
123
|
|
|
{ |
|
124
|
|
|
$ccNumberToken = $payment->getAdditionalInformation('cc_public_id'); |
|
125
|
|
|
$ccLast4 = $payment->getAdditionalInformation('cc_number'); |
|
126
|
|
|
$ccType = $payment->getAdditionalInformation('cc_type'); |
|
127
|
|
|
$ccExpMonth = $payment->getAdditionalInformation('cc_exp_month'); |
|
128
|
|
|
|
|
129
|
|
|
$ccExpYear = $payment->getAdditionalInformation('cc_exp_year'); |
|
130
|
|
|
if (empty($ccNumberToken)) { |
|
131
|
|
|
return null; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$ccType = $this->mapperCcType($ccType); |
|
135
|
|
|
if (!$ccType) { |
|
136
|
|
|
$ccType = $response[self::CREDIT][self::BRAND]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$ccLast4 = preg_replace('/[^0-9]/', '', $ccLast4); |
|
140
|
|
|
$paymentToken = $this->paymentTokenFactory->create(); |
|
141
|
|
|
$paymentToken->setGatewayToken($ccNumberToken); |
|
142
|
|
|
$paymentToken->setExpiresAt(strtotime('+1 year')); |
|
143
|
|
|
$paymentToken->setType(PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD); |
|
144
|
|
|
// phpcs:ignore Generic.Files.LineLength |
|
145
|
|
|
$details = ['cc_last4' => $ccLast4, 'cc_exp_year' => $ccExpYear, 'cc_exp_month' => $ccExpMonth, 'cc_type' => $ccType]; |
|
146
|
|
|
$paymentToken->setTokenDetails($this->json->serialize($details)); |
|
147
|
|
|
|
|
148
|
|
|
return $paymentToken; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get payment extension attributes. |
|
153
|
|
|
* |
|
154
|
|
|
* @param InfoInterface $payment |
|
155
|
|
|
* |
|
156
|
|
|
* @return OrderPaymentExtensionInterface |
|
157
|
|
|
*/ |
|
158
|
|
|
private function getExtensionAttributes(InfoInterface $payment): OrderPaymentExtensionInterface |
|
159
|
|
|
{ |
|
160
|
|
|
$extensionAttributes = $payment->getExtensionAttributes(); |
|
161
|
|
|
if (null === $extensionAttributes) { |
|
162
|
|
|
$extensionAttributes = $this->payExtensionFactory->create(); |
|
163
|
|
|
$payment->setExtensionAttributes($extensionAttributes); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $extensionAttributes; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get Type Cc by response payment. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $type |
|
173
|
|
|
* |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
public function mapperCcType($type) |
|
177
|
|
|
{ |
|
178
|
|
|
$type = ucfirst($type); |
|
179
|
|
|
$mapper = $this->configCc->getCcTypesMapper(); |
|
180
|
|
|
|
|
181
|
|
|
return $mapper[$type]; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths