Issues (139)

Http/Client/CreateOrderPaymentWalletClient.php (1 issue)

Labels
Severity
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
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
     * 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/qrcode');
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
                    'url'      => $url.'v1/payments/qrcode',
132
                    'request'  => $this->json->serialize($transferObject->getBody()),
133
                    'response' => $responseBody,
134
                ]
135
            );
136
        } catch (InvalidArgumentException $e) {
137
            $this->logger->debug(
138
                [
139
                    'url'       => $url.'v1/payments/qrcode',
140
                    'request'   => $this->json->serialize($transferObject->getBody()),
141
                    'response'  => $responseBody,
142
                    'error'     => $e->getMessage(),
143
                ]
144
            );
145
            // phpcs:ignore Magento2.Exceptions.DirectThrow
146
            throw new Exception('Invalid JSON was returned by the gateway');
147
        }
148
149
        return $response;
150
    }
151
}
152