WalletFetchTransactionInfoClient::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
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 WalletFetchTransactionInfoClient - create authorization for fetch.
26
 *
27
 * @SuppressWarnings(PHPCPD)
28
 */
29
class WalletFetchTransactionInfoClient 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
     * 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_DENIED = 'DENIED';
55
56
    /**
57
     * @const string
58
     */
59
    public const RESPONSE_APPROVED = 'APPROVED';
60
61
    /**
62
     * @var Logger
63
     */
64
    protected $logger;
65
66
    /**
67
     * @var ZendClientFactory
68
     */
69
    protected $httpClientFactory;
70
71
    /**
72
     * @var Config
73
     */
74
    protected $config;
75
76
    /**
77
     * @var Json
78
     */
79
    protected $json;
80
81
    /**
82
     * @param Logger            $logger
83
     * @param ZendClientFactory $httpClientFactory
84
     * @param Config            $config
85
     * @param Json              $json
86
     */
87
    public function __construct(
88
        Logger $logger,
89
        ZendClientFactory $httpClientFactory,
90
        Config $config,
91
        Json $json
92
    ) {
93
        $this->config = $config;
94
        $this->httpClientFactory = $httpClientFactory;
95
        $this->logger = $logger;
96
        $this->json = $json;
97
    }
98
99
    /**
100
     * Places request to gateway.
101
     *
102
     * @param TransferInterface $transferObject
103
     *
104
     * @return array
105
     */
106
    public function placeRequest(TransferInterface $transferObject)
107
    {
108
        /** @var ZendClient $client */
109
        $client = $this->httpClientFactory->create();
110
        $request = $transferObject->getBody();
111
        $url = $this->config->getApiUrl();
0 ignored issues
show
Unused Code introduced by
The assignment to $url is dead and can be removed.
Loading history...
112
        $storeId = $request[self::STORE_ID];
113
        $url = $this->config->getApiUrl($storeId);
114
        $apiBearer = $this->config->getMerchantGatewayOauth($storeId);
115
        unset($request[self::STORE_ID]);
116
        $response = ['RESULT_CODE' => 0];
117
118
        if ($request[self::ORDER_STATE] !== Order::STATE_NEW) {
119
            // phpcs:ignore Magento2.Exceptions.DirectThrow
120
            throw new InvalidArgumentException('Payment is not New.');
121
        }
122
123
        try {
124
            $client->setUri($url.'v1/payments/qrcode/'.$getnetPaymentId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $getnetPaymentId seems to be never defined.
Loading history...
125
            $client->setConfig(['maxredirects' => 0, 'timeout' => 45000]);
126
            $client->setHeaders(
127
                [
128
                    'Authorization'               => 'Bearer '.$apiBearer,
129
                    'x-transaction-channel-entry' => 'MG',
130
                ]
131
            );
132
            $client->setMethod(ZendClient::GET);
133
134
            $responseBody = $client->request()->getBody();
135
            $data = $this->json->unserialize($responseBody);
136
            $status = $data[self::RESPONSE_STATUS];
137
            if ($status === self::RESPONSE_DENIED || $status === self::RESPONSE_APPROVED) {
138
                $response = array_merge(
139
                    [
140
                        'RESULT_CODE'       => 1,
141
                        'GETNET_ORDER_ID'   => $getnetPaymentId,
142
                        'STATUS'            => $status,
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/payments/qrcode/'.$getnetPaymentId,
154
                'response' => $responseBody,
155
            ]
156
        );
157
158
        return $response;
159
    }
160
}
161