CreateOrderPaymentBoletoClient   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 126
rs 10
wmc 4

2 Methods

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