Passed
Push — main ( 6b22cc...840532 )
by Bruno
09:36 queued 04:25
created

GetpayFetchTransactionInfoClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 10
rs 10
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
Bug introduced by
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
     * @var string
38
     */
39
    public const GETNET_ORDER_ID = 'order_id';
40
41
    /**
42
     * Order State - Block Name.
43
     */
44
    public const ORDER_STATE = 'state';
45
46
    /**
47
     * @const string
48
     */
49
    public const RESPONSE_STATUS = 'status';
50
51
    /**
52
     * @const string
53
     */
54
    public const RESPONSE_EXPIRED = 'EXPIRED';
55
56
    /**
57
     * @const string
58
     */
59
    public const RESPONSE_SOLD_OUT = 'SOLD_OUT';
60
61
    /**
62
     * @const string
63
     */
64
    public const RESPONSE_SUCCESSFUL_ORDERS = 'successful_orders';
65
66
    /**
67
     * @var Logger
68
     */
69
    private $logger;
70
71
    /**
72
     * @var ZendClientFactory
73
     */
74
    private $httpClientFactory;
75
76
    /**
77
     * @var Config
78
     */
79
    private $config;
80
81
    /**
82
     * @var Json
83
     */
84
    private $json;
85
86
    /**
87
     * @param Logger            $logger
88
     * @param ZendClientFactory $httpClientFactory
89
     * @param Config            $config
90
     * @param Json              $json
91
     */
92
    public function __construct(
93
        Logger $logger,
94
        ZendClientFactory $httpClientFactory,
95
        Config $config,
96
        Json $json
97
    ) {
98
        $this->config = $config;
99
        $this->httpClientFactory = $httpClientFactory;
100
        $this->logger = $logger;
101
        $this->json = $json;
102
    }
103
104
    /**
105
     * Places request to gateway.
106
     *
107
     * @param TransferInterface $transferObject
108
     *
109
     * @return array
110
     */
111
    public function placeRequest(TransferInterface $transferObject)
112
    {
113
        /** @var ZendClient $client */
114
        $client = $this->httpClientFactory->create();
115
        $request = $transferObject->getBody();
116
        $url = $this->config->getApiUrl();
117
        $apiBearer = $this->config->getMerchantGatewayOauth();
118
        $getnetOrderId = $request[self::GETNET_ORDER_ID];
119
        $responseBody = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $responseBody is dead and can be removed.
Loading history...
120
        $response = ['RESULT_CODE' => 0];
121
122
        if ($request[self::ORDER_STATE] !== Order::STATE_NEW) {
123
            // phpcs:ignore Magento2.Exceptions.DirectThrow
124
            throw new InvalidArgumentException('Payment is not New.');
125
        }
126
127
        try {
128
            $client->setUri($url.'v1/payment-links/'.$getnetOrderId);
129
            $client->setConfig(['maxredirects' => 0, 'timeout' => 45000]);
130
            $client->setHeaders('Authorization', 'Bearer '.$apiBearer);
131
            $client->setMethod(ZendClient::GET);
132
133
            $responseBody = $client->request()->getBody();
134
            $data = $this->json->unserialize($responseBody);
135
136
            if ($data[self::RESPONSE_STATUS] === self::RESPONSE_EXPIRED ||
137
                $data[self::RESPONSE_STATUS] === self::RESPONSE_SOLD_OUT) {
138
                $response = array_merge(
139
                    [
140
                        'RESULT_CODE'       => 1,
141
                        'GETNET_ORDER_ID'   => $getnetOrderId,
142
                        'STATUS'            => $data[self::RESPONSE_SUCCESSFUL_ORDERS],
143
                    ],
144
                    $data
145
                );
146
            }
147
        } catch (InvalidArgumentException $e) {
148
            // phpcs:ignore Magento2.Exceptions.DirectThrow
149
            throw new Exception('Invalid JSON was returned by the gateway');
150
        }
151
        $this->logger->debug(
152
            [
153
                'url'      => $url.'v1/payment-links/'.$getnetOrderId,
154
                'response' => $responseBody,
155
            ]
156
        );
157
158
        return $response;
159
    }
160
}
161