Passed
Branch [email protected] (aae97d)
by Bruno
10:40
created

WalletFetchTransactionInfoClient::placeRequest()   A

Complexity

Conditions 5
Paths 11

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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