|
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
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Moip\Magento2\Gateway\Command; |
|
12
|
|
|
|
|
13
|
|
|
use Magento\Payment\Gateway\Command\CommandException; |
|
|
|
|
|
|
14
|
|
|
use Magento\Payment\Gateway\Command\CommandPoolInterface; |
|
|
|
|
|
|
15
|
|
|
use Magento\Payment\Gateway\CommandInterface; |
|
|
|
|
|
|
16
|
|
|
use Magento\Payment\Gateway\ErrorMapper\ErrorMessageMapperInterface; |
|
|
|
|
|
|
17
|
|
|
use Magento\Payment\Gateway\Http\ClientInterface; |
|
|
|
|
|
|
18
|
|
|
use Magento\Payment\Gateway\Http\TransferFactoryInterface; |
|
|
|
|
|
|
19
|
|
|
use Magento\Payment\Gateway\Request\BuilderInterface; |
|
|
|
|
|
|
20
|
|
|
use Magento\Payment\Gateway\Response\HandlerInterface; |
|
|
|
|
|
|
21
|
|
|
use Magento\Payment\Gateway\Validator\ResultInterface; |
|
|
|
|
|
|
22
|
|
|
use Magento\Payment\Gateway\Validator\ValidatorInterface; |
|
|
|
|
|
|
23
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class AuthorizePaymentCommand - Executes the payment creation command with the order creation call. |
|
27
|
|
|
*/ |
|
28
|
|
|
class AuthorizePaymentCommand implements CommandInterface |
|
29
|
|
|
{ |
|
30
|
|
|
private const CREATE_ORDER_MOIP = 'create_order_moip'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var CommandPoolInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $commandPool; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var BuilderInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $requestBuilder; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var TransferFactoryInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
private $transferFactory; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var ClientInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
private $client; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var LoggerInterface |
|
54
|
|
|
*/ |
|
55
|
|
|
private $logger; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var HandlerInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
private $handler; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var ValidatorInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
private $validator; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var ErrorMessageMapperInterface |
|
69
|
|
|
*/ |
|
70
|
|
|
private $errorMessageMapper; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param CommandPoolInterface $commandPool |
|
74
|
|
|
* @param BuilderInterface $requestBuilder |
|
75
|
|
|
* @param TransferFactoryInterface $transferFactory |
|
76
|
|
|
* @param ClientInterface $client |
|
77
|
|
|
* @param LoggerInterface $logger |
|
78
|
|
|
* @param HandlerInterface $handler |
|
79
|
|
|
* @param ValidatorInterface $validator |
|
80
|
|
|
* @param ErrorMessageMapperInterface|null $errorMessageMapper |
|
81
|
|
|
*/ |
|
82
|
|
|
public function __construct( |
|
83
|
|
|
CommandPoolInterface $commandPool, |
|
84
|
|
|
BuilderInterface $requestBuilder, |
|
85
|
|
|
TransferFactoryInterface $transferFactory, |
|
86
|
|
|
ClientInterface $client, |
|
87
|
|
|
LoggerInterface $logger, |
|
88
|
|
|
HandlerInterface $handler = null, |
|
89
|
|
|
ValidatorInterface $validator = null, |
|
90
|
|
|
ErrorMessageMapperInterface $errorMessageMapper = null |
|
91
|
|
|
) { |
|
92
|
|
|
$this->commandPool = $commandPool; |
|
93
|
|
|
$this->requestBuilder = $requestBuilder; |
|
94
|
|
|
$this->transferFactory = $transferFactory; |
|
95
|
|
|
$this->client = $client; |
|
96
|
|
|
$this->handler = $handler; |
|
97
|
|
|
$this->validator = $validator; |
|
98
|
|
|
$this->logger = $logger; |
|
99
|
|
|
$this->errorMessageMapper = $errorMessageMapper; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Execute. |
|
104
|
|
|
* |
|
105
|
|
|
* @param array $commandSubject |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
public function execute(array $commandSubject) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->createOrderMoip($commandSubject); |
|
112
|
|
|
$transferO = $this->transferFactory->create( |
|
113
|
|
|
$this->requestBuilder->build($commandSubject) |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$response = $this->client->placeRequest($transferO); |
|
117
|
|
|
if ($this->validator !== null) { |
|
118
|
|
|
$result = $this->validator->validate( |
|
119
|
|
|
array_merge($commandSubject, ['response' => $response]) |
|
120
|
|
|
); |
|
121
|
|
|
if (!$result->isValid()) { |
|
122
|
|
|
$this->processErrors($result); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ($this->handler) { |
|
127
|
|
|
$this->handler->handle( |
|
128
|
|
|
$commandSubject, |
|
129
|
|
|
$response |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Process Errors. |
|
136
|
|
|
* |
|
137
|
|
|
* @param ResultInterface $result |
|
138
|
|
|
*/ |
|
139
|
|
|
private function processErrors(ResultInterface $result) |
|
140
|
|
|
{ |
|
141
|
|
|
$messages = []; |
|
142
|
|
|
$errorsSource = array_merge($result->getErrorCodes(), $result->getFailsDescription()); |
|
143
|
|
|
foreach ($errorsSource as $errorCodeOrMessage) { |
|
144
|
|
|
$errorCodeOrMessage = (string) $errorCodeOrMessage; |
|
145
|
|
|
if ($this->errorMessageMapper !== null) { |
|
146
|
|
|
$mapped = (string) $this->errorMessageMapper->getMessage($errorCodeOrMessage); |
|
147
|
|
|
if (!empty($mapped)) { |
|
148
|
|
|
$messages[] = $mapped; |
|
149
|
|
|
$errorCodeOrMessage = $mapped; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
$this->logger->critical('Payment Error: '.$errorCodeOrMessage); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
throw new CommandException( |
|
156
|
|
|
!empty($messages) |
|
157
|
|
|
? __(implode(PHP_EOL, $messages)) |
|
|
|
|
|
|
158
|
|
|
: __('Transaction has been declined. Please try again later.') |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Create Order. |
|
164
|
|
|
* |
|
165
|
|
|
* @param array $commandSubject |
|
166
|
|
|
* |
|
167
|
|
|
* @return bool |
|
168
|
|
|
*/ |
|
169
|
|
|
private function createOrderMoip(array $commandSubject): bool |
|
170
|
|
|
{ |
|
171
|
|
|
$response = $this->commandPool->get(self::CREATE_ORDER_MOIP) |
|
|
|
|
|
|
172
|
|
|
->execute($commandSubject); |
|
173
|
|
|
|
|
174
|
|
|
return true; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
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