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

CreateOrderPaymentWalletClient   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 43
c 2
b 0
f 0
dl 0
loc 110
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A placeRequest() 0 53 3
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
23
/**
24
 * Class Create Order Payment Wallet Client - create order for payment by Wallet.
25
 *
26
 * @SuppressWarnings(PHPCPD)
27
 */
28
class CreateOrderPaymentWalletClient implements ClientInterface
29
{
30
    /**
31
     * Result Code - Block name.
32
     */
33
    public const RESULT_CODE = 'RESULT_CODE';
34
35
    /**
36
     * External Order Id - Block name.
37
     */
38
    public const EXT_ORD_ID = 'EXT_ORD_ID';
39
40
    /**
41
     * @var Logger
42
     */
43
    private $logger;
44
45
    /**
46
     * @var ZendClientFactory
47
     */
48
    private $httpClientFactory;
49
50
    /**
51
     * @var Config
52
     */
53
    private $config;
54
55
    /**
56
     * @var Json
57
     */
58
    private $json;
59
60
    /**
61
     * @param Logger            $logger
62
     * @param ZendClientFactory $httpClientFactory
63
     * @param Config            $config
64
     * @param Json              $json
65
     */
66
    public function __construct(
67
        Logger $logger,
68
        ZendClientFactory $httpClientFactory,
69
        Config $config,
70
        Json $json
71
    ) {
72
        $this->config = $config;
73
        $this->httpClientFactory = $httpClientFactory;
74
        $this->logger = $logger;
75
        $this->json = $json;
76
    }
77
78
    /**
79
     * Places request to gateway.
80
     *
81
     * @param TransferInterface $transferObject
82
     *
83
     * @return array
84
     */
85
    public function placeRequest(TransferInterface $transferObject)
86
    {
87
        /** @var ZendClient $client */
88
        $client = $this->httpClientFactory->create();
89
        $request = $transferObject->getBody();
90
        $url = $this->config->getApiUrl();
91
        $apiBearer = $this->config->getMerchantGatewayOauth();
92
93
        try {
94
            $client->setUri($url.'/v1/payments/qrcode');
95
            $client->setConfig(['maxredirects' => 0, 'timeout' => 45000]);
96
            $client->setHeaders('Authorization', 'Bearer '.$apiBearer);
97
            $client->setRawData($this->json->serialize($request), 'application/json');
98
            $client->setMethod(ZendClient::POST);
99
100
            $responseBody = $client->request()->getBody();
101
            $data = $this->json->unserialize($responseBody);
102
            $response = array_merge(
103
                [
104
                    self::RESULT_CODE  => 0,
105
                ],
106
                $data
107
            );
108
            if (isset($data['payment_id'])) {
109
                $response = array_merge(
110
                    [
111
                        self::RESULT_CODE => 1,
112
                        self::EXT_ORD_ID  => $data['payment_id'],
113
                    ],
114
                    $data
115
                );
116
            }
117
            $this->logger->debug(
118
                [
119
                    'url'      => $url.'v1/payments/qrcode',
120
                    'request'  => $this->json->serialize($transferObject->getBody()),
121
                    'response' => $responseBody,
122
                ]
123
            );
124
        } catch (InvalidArgumentException $e) {
125
            $this->logger->debug(
126
                [
127
                    'url'       => $url.'v1/payments/qrcode',
128
                    'request'   => $this->json->serialize($transferObject->getBody()),
129
                    'response'  => $responseBody,
130
                    'error'     => $e->getMessage(),
131
                ]
132
            );
133
            // phpcs:ignore Magento2.Exceptions.DirectThrow
134
            throw new Exception('Invalid JSON was returned by the gateway');
135
        }
136
137
        return $response;
138
    }
139
}
140