|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright © Wirecard Brasil. All rights reserved. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Bruno Elisei <[email protected]> |
|
6
|
|
|
* See COPYING.txt for license details. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Moip\Magento2\Gateway\Response; |
|
10
|
|
|
|
|
11
|
|
|
use Magento\Framework\ObjectManagerInterface; |
|
12
|
|
|
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface; |
|
13
|
|
|
use Magento\Payment\Gateway\Response\HandlerInterface; |
|
14
|
|
|
use Magento\Payment\Model\InfoInterface; |
|
15
|
|
|
use Magento\Sales\Api\Data\OrderPaymentExtensionInterface; |
|
16
|
|
|
use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory; |
|
17
|
|
|
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface; |
|
18
|
|
|
use Magento\Vault\Api\Data\PaymentTokenInterface; |
|
19
|
|
|
use Magento\Vault\Api\Data\PaymentTokenInterfaceFactory; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
|
23
|
|
|
*/ |
|
24
|
|
|
class VaultDetailsHandler implements HandlerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var PaymentTokenInterfaceFactory |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $paymentTokenFactory; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var OrderPaymentExtensionInterfaceFactory |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $paymentExtensionFactory; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var SubjectReader |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $subjectReader; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ObjectManagerInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $objectManager; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var Json |
|
48
|
|
|
*/ |
|
49
|
|
|
private $serializer; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* AccountPaymentTokenFactory constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param ObjectManagerInterface $objectManager |
|
55
|
|
|
* @param PaymentTokenFactoryInterface $paymentTokenFactory |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct( |
|
58
|
|
|
ObjectManagerInterface $objectManager, |
|
59
|
|
|
OrderPaymentExtensionInterfaceFactory $paymentExtensionFactory, |
|
60
|
|
|
PaymentTokenFactoryInterface $paymentTokenFactory = null |
|
61
|
|
|
) { |
|
62
|
|
|
if ($paymentTokenFactory === null) { |
|
63
|
|
|
$paymentTokenFactory = $objectManager->get(PaymentTokenFactoryInterface::class); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->objectManager = $objectManager; |
|
67
|
|
|
$this->paymentExtensionFactory = $paymentExtensionFactory; |
|
68
|
|
|
$this->paymentTokenFactory = $paymentTokenFactory; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritdoc |
|
73
|
|
|
*/ |
|
74
|
|
|
public function handle(array $handlingSubject, array $response) |
|
75
|
|
|
{ |
|
76
|
|
|
if (!isset($handlingSubject['payment']) |
|
77
|
|
|
|| !$handlingSubject['payment'] instanceof PaymentDataObjectInterface |
|
|
|
|
|
|
78
|
|
|
) { |
|
79
|
|
|
throw new \InvalidArgumentException('Payment data object should be provided'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$paymentDO = $handlingSubject['payment']; |
|
83
|
|
|
|
|
84
|
|
|
$payment = $paymentDO->getPayment(); |
|
85
|
|
|
|
|
86
|
|
|
if ($payment->getMethod() === 'moip_magento2_cc') { |
|
87
|
|
|
$paymentToken = $this->getVaultPaymentToken($response, $payment); |
|
88
|
|
|
if (null !== $paymentToken) { |
|
89
|
|
|
$extensionAttributes = $this->getExtensionAttributes($payment); |
|
90
|
|
|
$extensionAttributes->setVaultPaymentToken($paymentToken); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get vault payment token entity. |
|
97
|
|
|
* |
|
98
|
|
|
* @param $response |
|
99
|
|
|
* |
|
100
|
|
|
* @return PaymentTokenInterface|null |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function getVaultPaymentToken($response, $payment) |
|
103
|
|
|
{ |
|
104
|
|
|
$paymentAddtional = $response['fundingInstrument']; |
|
105
|
|
|
$ccExpYear = $payment->getAdditionalInformation('cc_exp_year'); |
|
106
|
|
|
$ccExpMonth = $payment->getAdditionalInformation('cc_exp_month'); |
|
107
|
|
|
$payment->unsAdditionalInformation('cc_exp_year'); |
|
108
|
|
|
$payment->unsAdditionalInformation('cc_exp_month'); |
|
109
|
|
|
|
|
110
|
|
|
if (isset($paymentAddtional['creditCard'])) { |
|
111
|
|
View Code Duplication |
if (isset($paymentAddtional['creditCard']['id'])) { |
|
|
|
|
|
|
112
|
|
|
$ccId = $paymentAddtional['creditCard']['id']; |
|
113
|
|
|
$ccType = $this->mapperCcType($paymentAddtional['creditCard']['brand']); |
|
114
|
|
|
$ccLast4 = $paymentAddtional['creditCard']['last4']; |
|
115
|
|
|
} |
|
116
|
|
|
} else { |
|
117
|
|
|
throw new \InvalidArgumentException('Payment data object should be provided'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (empty($ccId)) { |
|
121
|
|
|
return null; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$paymentToken = $this->paymentTokenFactory->create(); |
|
125
|
|
|
$paymentToken->setGatewayToken($ccId); |
|
126
|
|
|
$paymentToken->setExpiresAt(strtotime('+1 year')); |
|
127
|
|
|
$paymentToken->setType(PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD); |
|
128
|
|
|
// phpcs:ignore Generic.Files.LineLength |
|
129
|
|
|
$details = ['cc_last4' => $ccLast4, 'cc_exp_year' => $ccExpYear, 'cc_exp_month' => $ccExpMonth, 'cc_type' => $ccType]; |
|
|
|
|
|
|
130
|
|
|
$paymentToken->setTokenDetails(json_encode($details)); |
|
131
|
|
|
|
|
132
|
|
|
return $paymentToken; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get payment extension attributes. |
|
137
|
|
|
* |
|
138
|
|
|
* @param InfoInterface $payment |
|
139
|
|
|
* |
|
140
|
|
|
* @return OrderPaymentExtensionInterface |
|
141
|
|
|
*/ |
|
142
|
|
|
private function getExtensionAttributes(InfoInterface $payment): OrderPaymentExtensionInterface |
|
143
|
|
|
{ |
|
144
|
|
|
$extensionAttributes = $payment->getExtensionAttributes(); |
|
145
|
|
|
if (null === $extensionAttributes) { |
|
146
|
|
|
$extensionAttributes = $this->paymentExtensionFactory->create(); |
|
147
|
|
|
$payment->setExtensionAttributes($extensionAttributes); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $extensionAttributes; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Get Type Cc by response payment. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $type |
|
157
|
|
|
*/ |
|
158
|
|
View Code Duplication |
public function mapperCcType($type) |
|
|
|
|
|
|
159
|
|
|
{ |
|
160
|
|
|
if ($type === 'MASTERCARD') { |
|
161
|
|
|
return 'MC'; |
|
162
|
|
|
} elseif ($type === 'VISA') { |
|
163
|
|
|
return 'VI'; |
|
164
|
|
|
} elseif ($type === 'AMEX') { |
|
165
|
|
|
return 'AE'; |
|
166
|
|
|
} elseif ($type === 'DINERS') { |
|
167
|
|
|
return 'DN'; |
|
168
|
|
|
} elseif ($type === 'HIPERCARD') { |
|
169
|
|
|
return 'HC'; |
|
170
|
|
|
} elseif ($type === 'HIPER') { |
|
171
|
|
|
return 'HI'; |
|
172
|
|
|
} elseif ($type === 'ELO') { |
|
173
|
|
|
return 'ELO'; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.