|
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\Http\Client; |
|
12
|
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
|
14
|
|
|
use Magento\Framework\HTTP\ZendClient; |
|
|
|
|
|
|
15
|
|
|
use Magento\Framework\HTTP\ZendClientFactory; |
|
|
|
|
|
|
16
|
|
|
use Magento\Framework\Serialize\Serializer\Json; |
|
|
|
|
|
|
17
|
|
|
use Magento\Payment\Gateway\Http\ClientInterface; |
|
|
|
|
|
|
18
|
|
|
use Magento\Payment\Gateway\Http\TransferInterface; |
|
|
|
|
|
|
19
|
|
|
use Magento\Payment\Model\Method\Logger; |
|
|
|
|
|
|
20
|
|
|
use Moip\Magento2\Gateway\Config\Config; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class CreateOrderAuthorizeClient - Returns authorization for order creation. |
|
24
|
|
|
*/ |
|
25
|
|
|
class CreateOrderAuthorizeClient implements ClientInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var LoggerInterface |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
|
|
private $logger; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ZendClientFactory |
|
34
|
|
|
*/ |
|
35
|
|
|
private $httpClientFactory; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var Config |
|
39
|
|
|
*/ |
|
40
|
|
|
private $config; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Json |
|
44
|
|
|
*/ |
|
45
|
|
|
private $json; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Logger $logger |
|
49
|
|
|
* @param ZendClientFactory $httpClientFactory |
|
50
|
|
|
* @param Config $config |
|
51
|
|
|
* @param Json $json |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct( |
|
54
|
|
|
Logger $logger, |
|
55
|
|
|
ZendClientFactory $httpClientFactory, |
|
56
|
|
|
Config $config, |
|
57
|
|
|
Json $json |
|
58
|
|
|
) { |
|
59
|
|
|
$this->config = $config; |
|
60
|
|
|
$this->httpClientFactory = $httpClientFactory; |
|
61
|
|
|
$this->logger = $logger; |
|
62
|
|
|
$this->json = $json; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Places request to gateway. |
|
67
|
|
|
* |
|
68
|
|
|
* @param TransferInterface $transferObject |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function placeRequest(TransferInterface $transferObject) |
|
73
|
|
|
{ |
|
74
|
|
|
$client = $this->httpClientFactory->create(); |
|
75
|
|
|
$request = $transferObject->getBody(); |
|
76
|
|
|
$url = $this->config->getApiUrl(); |
|
77
|
|
|
$apiBearer = $this->config->getMerchantGatewayOauth(); |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
|
|
$client->setUri($url.'orders'); |
|
81
|
|
|
$client->setConfig(['maxredirects' => 0, 'timeout' => 120]); |
|
82
|
|
|
$client->setHeaders('Authorization', 'Bearer '.$apiBearer); |
|
83
|
|
|
$client->setRawData($this->json->serialize($request), 'application/json'); |
|
84
|
|
|
$client->setMethod(ZendClient::POST); |
|
85
|
|
|
|
|
86
|
|
|
$responseBody = $client->request()->getBody(); |
|
87
|
|
|
$data = $this->json->unserialize($responseBody); |
|
88
|
|
|
if (isset($data['id'])) { |
|
89
|
|
|
$response = array_merge( |
|
90
|
|
|
[ |
|
91
|
|
|
'RESULT_CODE' => 1, |
|
92
|
|
|
'EXT_ORD_ID' => $data['id'], |
|
93
|
|
|
], |
|
94
|
|
|
$data |
|
95
|
|
|
); |
|
96
|
|
|
} else { |
|
97
|
|
|
$response = array_merge( |
|
98
|
|
|
[ |
|
99
|
|
|
'RESULT_CODE' => 0, |
|
100
|
|
|
], |
|
101
|
|
|
$data |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
$this->logger->debug( |
|
105
|
|
|
[ |
|
106
|
|
|
'url' => $url.'orders', |
|
107
|
|
|
'request' => $this->json->serialize($transferObject->getBody()), |
|
108
|
|
|
'response' => $responseBody, |
|
109
|
|
|
] |
|
110
|
|
|
); |
|
111
|
|
|
} catch (InvalidArgumentException $e) { |
|
112
|
|
|
$this->logger->debug( |
|
113
|
|
|
[ |
|
114
|
|
|
'exception' => $e->getMessage(), |
|
115
|
|
|
'url' => $url.'orders', |
|
116
|
|
|
'request' => $this->json->serialize($transferObject->getBody()), |
|
117
|
|
|
'response' => $responseBody, |
|
118
|
|
|
] |
|
119
|
|
|
); |
|
120
|
|
|
// phpcs:ignore Magento2.Exceptions.DirectThrow |
|
121
|
|
|
throw new \Exception('Invalid JSON was returned by the gateway'); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $response; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
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