Issues (139)

Http/Client/GetpayFetchTransactionInfoClient.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
use Magento\Sales\Model\Order;
23
24
/**
25
 * Class GetpayFetchTransactionInfoClient - create authorization for fetch.
26
 *
27
 * @SuppressWarnings(PHPCPD)
28
 */
29
class GetpayFetchTransactionInfoClient implements ClientInterface
30
{
31
    /**
32
     * @var string
33
     */
34
    public const GETNET_PAYMENT_ID = 'payment_id';
35
36
    /**
37
     * Store Id - Block name.
38
     */
39
    public const STORE_ID = 'store_id';
40
41
    /**
42
     * @var string
43
     */
44
    public const GETNET_ORDER_ID = 'order_id';
45
46
    /**
47
     * Order State - Block Name.
48
     */
49
    public const ORDER_STATE = 'state';
50
51
    /**
52
     * @const string
53
     */
54
    public const RESPONSE_STATUS = 'status';
55
56
    /**
57
     * @const string
58
     */
59
    public const RESPONSE_EXPIRED = 'EXPIRED';
60
61
    /**
62
     * @const string
63
     */
64
    public const RESPONSE_SOLD_OUT = 'SOLD_OUT';
65
66
    /**
67
     * @const string
68
     */
69
    public const RESPONSE_SUCCESSFUL_ORDERS = 'successful_orders';
70
71
    /**
72
     * @var Logger
73
     */
74
    protected $logger;
75
76
    /**
77
     * @var ZendClientFactory
78
     */
79
    protected $httpClientFactory;
80
81
    /**
82
     * @var Config
83
     */
84
    protected $config;
85
86
    /**
87
     * @var Json
88
     */
89
    protected $json;
90
91
    /**
92
     * @param Logger            $logger
93
     * @param ZendClientFactory $httpClientFactory
94
     * @param Config            $config
95
     * @param Json              $json
96
     */
97
    public function __construct(
98
        Logger $logger,
99
        ZendClientFactory $httpClientFactory,
100
        Config $config,
101
        Json $json
102
    ) {
103
        $this->config = $config;
104
        $this->httpClientFactory = $httpClientFactory;
105
        $this->logger = $logger;
106
        $this->json = $json;
107
    }
108
109
    /**
110
     * Places request to gateway.
111
     *
112
     * @param TransferInterface $transferObject
113
     *
114
     * @return array
115
     */
116
    public function placeRequest(TransferInterface $transferObject)
117
    {
118
        /** @var ZendClient $client */
119
        $client = $this->httpClientFactory->create();
120
        $request = $transferObject->getBody();
121
        $storeId = $request[self::STORE_ID];
122
        $url = $this->config->getApiUrl($storeId);
123
        $apiBearer = $this->config->getMerchantGatewayOauth($storeId);
124
        unset($request[self::STORE_ID]);
125
        $getnetOrderId = $request[self::GETNET_ORDER_ID];
126
        $response = ['RESULT_CODE' => 0];
127
128
        if ($request[self::ORDER_STATE] !== Order::STATE_NEW) {
129
            // phpcs:ignore Magento2.Exceptions.DirectThrow
130
            throw new InvalidArgumentException('Payment is not New.');
131
        }
132
133
        try {
134
            $client->setUri($url.'v1/payment-links/'.$getnetOrderId);
135
            $client->setConfig(['maxredirects' => 0, 'timeout' => 45000]);
136
            $client->setHeaders(
137
                [
138
                    'Authorization'               => 'Bearer '.$apiBearer,
139
                    'x-transaction-channel-entry' => 'MG',
140
                ]
141
            );
142
            $client->setMethod(ZendClient::GET);
143
144
            $responseBody = $client->request()->getBody();
145
            $data = $this->json->unserialize($responseBody);
146
147
            if ($data[self::RESPONSE_STATUS] === self::RESPONSE_EXPIRED ||
148
                $data[self::RESPONSE_STATUS] === self::RESPONSE_SOLD_OUT) {
149
                $response = array_merge(
150
                    [
151
                        'RESULT_CODE'       => 1,
152
                        'GETNET_ORDER_ID'   => $getnetOrderId,
153
                        'STATUS'            => $data[self::RESPONSE_SUCCESSFUL_ORDERS],
154
                    ],
155
                    $data
156
                );
157
            }
158
        } catch (InvalidArgumentException $e) {
159
            // phpcs:ignore Magento2.Exceptions.DirectThrow
160
            throw new Exception('Invalid JSON was returned by the gateway');
161
        }
162
        $this->logger->debug(
163
            [
164
                'url'      => $url.'v1/payment-links/'.$getnetOrderId,
165
                'response' => $responseBody,
166
            ]
167
        );
168
169
        return $response;
170
    }
171
}
172