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

CreateOrderPaymentGetpayClient   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A placeRequest() 0 54 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 Getpay Client - create order for payment by Pix.
25
 *
26
 * @SuppressWarnings(PHPCPD)
27
 */
28
class CreateOrderPaymentGetpayClient 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
    protected $logger;
44
45
    /**
46
     * @var ZendClientFactory
47
     */
48
    protected $httpClientFactory;
49
50
    /**
51
     * @var Config
52
     */
53
    protected $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/payment-links');
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
            if (isset($data['link_id'])) {
103
                $response = array_merge(
104
                    [
105
                        self::RESULT_CODE => 1,
106
                        self::EXT_ORD_ID  => $data['link_id'],
107
                    ],
108
                    $data
109
                );
110
            } else {
111
                $response = array_merge(
112
                    [
113
                        self::RESULT_CODE  => 0,
114
                    ],
115
                    $data
116
                );
117
            }
118
            $this->logger->debug(
119
                [
120
                    'url'      => $url.'v1/payment-links',
121
                    'request'  => $this->json->serialize($transferObject->getBody()),
122
                    'response' => $responseBody,
123
                ]
124
            );
125
        } catch (InvalidArgumentException $e) {
126
            $this->logger->debug(
127
                [
128
                    'url'       => $url.'v1/payment-links',
129
                    'request'   => $this->json->serialize($transferObject->getBody()),
130
                    'response'  => $responseBody,
131
                    'error'     => $e->getMessage(),
132
                ]
133
            );
134
            // phpcs:ignore Magento2.Exceptions.DirectThrow
135
            throw new Exception('Invalid JSON was returned by the gateway');
136
        }
137
138
        return $response;
139
    }
140
}
141