Passed
Branch [email protected] (3810d0)
by Bruno
11:09
created

TwoCcRefundClient::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
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
23
/**
24
 * Class Two Cc Refund Client - Returns refund data.
25
 *
26
 * @SuppressWarnings(PHPCPD)
27
 */
28
class TwoCcRefundClient implements ClientInterface
29
{
30
    /**
31
     * Result Code - Block name.
32
     */
33
    public const RESULT_CODE = 'RESULT_CODE';
34
35
    /**
36
     * Response Payments- Block name.
37
     */
38
    public const RESPONSE_PAYMENTS = 'payments';
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
    protected $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/combined/cancel');
95
            $client->setConfig(['maxredirects' => 0, 'timeout' => 45000]);
96
            $client->setHeaders(
97
                [
98
                    'Authorization' => 'Bearer '.$apiBearer,
99
                ]
100
            );
101
            $client->setRawData($this->json->serialize($request), 'application/json');
102
            $client->setMethod(ZendClient::POST);
103
104
            $responseBody = $client->request()->getBody();
105
            $data = $this->json->unserialize($responseBody);
106
            $response = array_merge(
107
                [
108
                    self::RESULT_CODE  => 0,
109
                ],
110
                $data
111
            );
112
            if (isset($data[self::RESPONSE_PAYMENTS])) {
113
                $response = array_merge(
114
                    [
115
                        self::RESULT_CODE   => 1,
116
                    ],
117
                    $data
118
                );
119
            }
120
            $this->logger->debug(
121
                [
122
                    'url'      => $url.'v1/payments/combined/cancel',
123
                    'request'  => $this->json->serialize($transferObject->getBody()),
124
                    'response' => $this->json->serialize($response),
125
                ]
126
            );
127
        } catch (InvalidArgumentException $e) {
128
            $this->logger->debug(
129
                [
130
                    'oauth'     => $apiBearer,
131
                    'url'       => $url.'v1/payments/combined/cancel',
132
                    'request'   => $this->json->serialize($transferObject->getBody()),
133
                    'response'  => $this->json->serialize($response),
134
                    'error'     => $e->getMessage(),
135
                ]
136
            );
137
            // phpcs:ignore Magento2.Exceptions.DirectThrow
138
            throw new Exception('Invalid JSON was returned by the gateway');
139
        }
140
141
        return $response;
142
    }
143
}
144