Issues (139)

Gateway/Http/Client/RefundClient.php (1 issue)

Labels
Severity
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
declare(strict_types=1);
10
11
namespace Getnet\PaymentMagento\Gateway\Http\Client;
12
13
use Exception;
14
use Getnet\PaymentMagento\Gateway\Config\Config;
15
use InvalidArgumentException;
16
use Magento\Framework\HTTP\ZendClient;
17
use Magento\Framework\HTTP\ZendClientFactory;
0 ignored issues
show
The type Magento\Framework\HTTP\ZendClientFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Magento\Framework\Serialize\Serializer\Json;
19
use Magento\Payment\Gateway\Http\ClientInterface;
20
use Magento\Payment\Gateway\Http\TransferInterface;
21
use Magento\Payment\Model\Method\Logger;
22
23
/**
24
 * Class Refund Client - Returns refund data.
25
 *
26
 * @SuppressWarnings(PHPCPD)
27
 */
28
class RefundClient implements ClientInterface
29
{
30
    /**
31
     * Result Code - Block name.
32
     */
33
    public const RESULT_CODE = 'RESULT_CODE';
34
35
    /**
36
     * Store Id - Block name.
37
     */
38
    public const STORE_ID = 'store_id';
39
40
    /**
41
     * Response Pay Cancel Request Id - Block name.
42
     */
43
    public const RESPONSE_CANCEL_REQUEST_ID = 'cancel_request_id';
44
45
    /**
46
     * Response Pay Status - Block Name.
47
     */
48
    public const RESPONSE_STATUS = 'status';
49
50
    /**
51
     * Response Pay Status Denied - Value.
52
     */
53
    public const RESPONSE_STATUS_DENIED = 'DENIED';
54
55
    /**
56
     * @var Logger
57
     */
58
    protected $logger;
59
60
    /**
61
     * @var ZendClientFactory
62
     */
63
    protected $httpClientFactory;
64
65
    /**
66
     * @var Config
67
     */
68
    protected $config;
69
70
    /**
71
     * @var Json
72
     */
73
    protected $json;
74
75
    /**
76
     * @param Logger            $logger
77
     * @param ZendClientFactory $httpClientFactory
78
     * @param Config            $config
79
     * @param Json              $json
80
     */
81
    public function __construct(
82
        Logger $logger,
83
        ZendClientFactory $httpClientFactory,
84
        Config $config,
85
        Json $json
86
    ) {
87
        $this->config = $config;
88
        $this->httpClientFactory = $httpClientFactory;
89
        $this->logger = $logger;
90
        $this->json = $json;
91
    }
92
93
    /**
94
     * Places request to gateway.
95
     *
96
     * @param TransferInterface $transferObject
97
     *
98
     * @return array
99
     */
100
    public function placeRequest(TransferInterface $transferObject)
101
    {
102
        /** @var ZendClient $client */
103
        $client = $this->httpClientFactory->create();
104
        $request = $transferObject->getBody();
105
        $storeId = $request[self::STORE_ID];
106
        $url = $this->config->getApiUrl($storeId);
107
        $apiBearer = $this->config->getMerchantGatewayOauth($storeId);
108
        unset($request[self::STORE_ID]);
109
110
        try {
111
            $client->setUri($url.'/v1/payments/cancel/request');
112
            $client->setConfig(['maxredirects' => 0, 'timeout' => 45000]);
113
            $client->setHeaders(
114
                [
115
                    'Authorization'               => 'Bearer '.$apiBearer,
116
                    'x-transaction-channel-entry' => 'MG',
117
                ]
118
            );
119
            $client->setRawData($this->json->serialize($request), 'application/json');
120
            $client->setMethod(ZendClient::POST);
121
122
            $responseBody = $client->request()->getBody();
123
            $data = $this->json->unserialize($responseBody);
124
            $response = array_merge(
125
                [
126
                    self::RESULT_CODE  => 0,
127
                ],
128
                $data
129
            );
130
            if (isset($data[self::RESPONSE_CANCEL_REQUEST_ID])) {
131
                $response = array_merge(
132
                    [
133
                        self::RESULT_CODE                 => 1,
134
                        self::RESPONSE_CANCEL_REQUEST_ID  => $data[self::RESPONSE_CANCEL_REQUEST_ID],
135
                    ],
136
                    $data
137
                );
138
                if ($data[self::RESPONSE_STATUS] === self::RESPONSE_STATUS_DENIED) {
139
                    $response = array_merge(
140
                        [
141
                            self::RESULT_CODE                 => 0,
142
                            self::RESPONSE_CANCEL_REQUEST_ID  => $data[self::RESPONSE_CANCEL_REQUEST_ID],
143
                        ],
144
                        $data
145
                    );
146
                }
147
            }
148
            $this->logger->debug(
149
                [
150
                    'url'      => $url.'v1/payments/cancel/request',
151
                    'request'  => $this->json->serialize($transferObject->getBody()),
152
                    'response' => $this->json->serialize($response),
153
                ]
154
            );
155
        } catch (InvalidArgumentException $e) {
156
            $this->logger->debug(
157
                [
158
                    'oauth'     => $apiBearer,
159
                    'url'       => $url.'v1/payments/cancel/request',
160
                    'request'   => $this->json->serialize($transferObject->getBody()),
161
                    'response'  => $this->json->serialize($response),
162
                    'error'     => $e->getMessage(),
163
                ]
164
            );
165
            // phpcs:ignore Magento2.Exceptions.DirectThrow
166
            throw new Exception('Invalid JSON was returned by the gateway');
167
        }
168
169
        return $response;
170
    }
171
}
172