Issues (570)

Gateway/Http/Client/RefundClient.php (7 issues)

Labels
Severity
1
<?php
2
/**
3
 * Copyright © Wirecard Brasil. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Moip\Magento2\Gateway\Http\Client;
12
13
use InvalidArgumentException;
14
use Magento\Framework\HTTP\ZendClient;
0 ignored issues
show
The type Magento\Framework\HTTP\ZendClient 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...
15
use Magento\Framework\HTTP\ZendClientFactory;
0 ignored issues
show
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...
16
use Magento\Framework\Serialize\Serializer\Json;
0 ignored issues
show
The type Magento\Framework\Serialize\Serializer\Json 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...
17
use Magento\Payment\Gateway\Http\ClientInterface;
0 ignored issues
show
The type Magento\Payment\Gateway\Http\ClientInterface 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\Payment\Gateway\Http\TransferInterface;
0 ignored issues
show
The type Magento\Payment\Gateway\Http\TransferInterface 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...
19
use Magento\Payment\Model\Method\Logger;
0 ignored issues
show
The type Magento\Payment\Model\Method\Logger 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...
20
use Moip\Magento2\Gateway\Config\Config;
21
22
/**
23
 * Class RefundClient - Returns refund query.
24
 */
25
class RefundClient implements ClientInterface
26
{
27
    const MOIP_ORDER_ID = 'moip_order_id';
28
29
    /**
30
     * @var LoggerInterface
0 ignored issues
show
The type Moip\Magento2\Gateway\Http\Client\LoggerInterface 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...
31
     */
32
    private $logger;
33
34
    /**
35
     * @var ZendClientFactory
36
     */
37
    private $httpClientFactory;
38
39
    /**
40
     * @var Config
41
     */
42
    private $config;
43
44
    /**
45
     * @var Json
46
     */
47
    private $json;
48
49
    /**
50
     * @param Logger            $logger
51
     * @param ZendClientFactory $httpClientFactory
52
     * @param Config            $config
53
     * @param Json              $json
54
     */
55
    public function __construct(
56
        Logger $logger,
57
        ZendClientFactory $httpClientFactory,
58
        Config $config,
59
        Json $json
60
    ) {
61
        $this->config = $config;
62
        $this->httpClientFactory = $httpClientFactory;
63
        $this->logger = $logger;
64
        $this->json = $json;
65
    }
66
67
    /**
68
     * Places request to gateway.
69
     *
70
     * @param TransferInterface $transferObject
71
     *
72
     * @return array
73
     */
74
    public function placeRequest(TransferInterface $transferObject)
75
    {
76
        $client = $this->httpClientFactory->create();
77
        $request = $transferObject->getBody();
78
79
        $url = $this->config->getApiUrl();
80
        $apiBearer = $this->config->getMerchantGatewayOauth();
81
        $orderMoip = $request[self::MOIP_ORDER_ID];
82
83
        try {
84
            $client->setUri($url.'orders/'.$orderMoip.'/refunds');
85
            $client->setConfig(['maxredirects' => 0, 'timeout' => 120]);
86
            $client->setHeaders('Authorization', 'Bearer '.$apiBearer);
87
            $client->setRawData($this->json->serialize($request['send']), 'application/json');
88
            $client->setMethod(ZendClient::POST);
89
90
            $responseBody = $client->request()->getBody();
91
92
            $data = $this->json->unserialize($responseBody);
93
94
            if (isset($data['id'])) {
95
                $response = array_merge(
96
                    [
97
                        'RESULT_CODE'   => 1,
98
                        'STATUS'        => $data['status'],
99
                        'REFUND_ID'     => $data['id'],
100
                    ],
101
                    $data
102
                );
103
            } else {
104
                $response = array_merge(
105
                    [
106
                        'RESULT_CODE' => 0,
107
                    ],
108
                    $data
109
                );
110
            }
111
            $this->logger->debug(
112
                [
113
                    'url'      => $url.'orders/'.$orderMoip.'/refunds',
114
                    'request'  => $this->json->serialize($request['send']),
115
                    'response' => $responseBody,
116
                ]
117
            );
118
        } catch (InvalidArgumentException $e) {
119
            $this->logger->debug(
120
                [
121
                    'url'      => $url.'orders/'.$orderMoip.'/refunds',
122
                    'request'  => $this->json->serialize($request['send']),
123
                    'response' => $responseBody,
124
                ]
125
            );
126
            // phpcs:ignore Magento2.Exceptions.DirectThrow
127
            throw new \Exception('Invalid JSON was returned by the gateway');
128
        }
129
130
        return $response;
131
    }
132
}
133