Issues (139)

Gateway/Http/Client/AcceptPaymentClient.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 Getnet\PaymentMagento\Gateway\Request\ExtPaymentIdRequest;
16
use InvalidArgumentException;
17
use Magento\Framework\HTTP\ZendClient;
18
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...
19
use Magento\Framework\Serialize\Serializer\Json;
20
use Magento\Payment\Gateway\Http\ClientInterface;
21
use Magento\Payment\Gateway\Http\TransferInterface;
22
use Magento\Payment\Model\Method\Logger;
23
24
/**
25
 * Class Accept Payment Client - Returns authorization to accept payment.
26
 *
27
 * @SuppressWarnings(PHPCPD)
28
 */
29
class AcceptPaymentClient implements ClientInterface
30
{
31
    /**
32
     * Result Code - Block name.
33
     */
34
    public const RESULT_CODE = 'RESULT_CODE';
35
36
    /**
37
     * Store Id - Block name.
38
     */
39
    public const STORE_ID = 'store_id';
40
41
    /**
42
     * Response Pay Status - Block Name.
43
     */
44
    public const RESPONSE_STATUS = 'status';
45
46
    /**
47
     * Response Pay Status Approved - Value.
48
     */
49
    public const RESPONSE_STATUS_CONFIRMED = 'CONFIRMED';
50
51
    /**
52
     * Response Pay Status Denied - Value.
53
     */
54
    public const RESPONSE_STATUS_ERROR = 'ERROR';
55
56
    /**
57
     * @var Logger
58
     */
59
    protected $logger;
60
61
    /**
62
     * @var ZendClientFactory
63
     */
64
    protected $httpClientFactory;
65
66
    /**
67
     * @var Config
68
     */
69
    protected $config;
70
71
    /**
72
     * @var Json
73
     */
74
    protected $json;
75
76
    /**
77
     * @param Logger            $logger
78
     * @param ZendClientFactory $httpClientFactory
79
     * @param Config            $config
80
     * @param Json              $json
81
     */
82
    public function __construct(
83
        Logger $logger,
84
        ZendClientFactory $httpClientFactory,
85
        Config $config,
86
        Json $json
87
    ) {
88
        $this->config = $config;
89
        $this->httpClientFactory = $httpClientFactory;
90
        $this->logger = $logger;
91
        $this->json = $json;
92
    }
93
94
    /**
95
     * Places request to gateway.
96
     *
97
     * @param TransferInterface $transferObject
98
     *
99
     * @return array
100
     */
101
    public function placeRequest(TransferInterface $transferObject)
102
    {
103
        /** @var ZendClient $client */
104
        $client = $this->httpClientFactory->create();
105
        $request = $transferObject->getBody();
106
        $storeId = $request[self::STORE_ID];
107
        $url = $this->config->getApiUrl($storeId);
108
        $apiBearer = $this->config->getMerchantGatewayOauth($storeId);
109
        $paymentId = $request[ExtPaymentIdRequest::GETNET_PAYMENT_ID];
110
        unset($request[ExtPaymentIdRequest::GETNET_PAYMENT_ID]);
111
        unset($request[self::STORE_ID]);
112
113
        try {
114
            $client->setUri($url.'/v1/payments/credit/'.$paymentId.'/confirm');
115
            $client->setConfig(['maxredirects' => 0, 'timeout' => 45000]);
116
            $client->setHeaders(
117
                [
118
                    'Authorization'               => 'Bearer '.$apiBearer,
119
                    'x-transaction-channel-entry' => 'MG',
120
                ]
121
            );
122
            $client->setRawData($this->json->serialize($request), 'application/json');
123
            $client->setMethod(ZendClient::POST);
124
125
            $responseBody = $client->request()->getBody();
126
            $data = $this->json->unserialize($responseBody);
127
            $response = array_merge(
128
                [
129
                    self::RESULT_CODE => 0,
130
                ],
131
                $data
132
            );
133
            if (isset($data[self::RESPONSE_STATUS]) &&
134
                $data[self::RESPONSE_STATUS] === self::RESPONSE_STATUS_CONFIRMED
135
            ) {
136
                $response = array_merge(
137
                    [
138
                        self::RESULT_CODE => 1,
139
                    ],
140
                    $data
141
                );
142
            }
143
            $this->logger->debug(
144
                [
145
                    'url'      => $url.'/v1/payments/credit/'.$paymentId.'/confirm',
146
                    'request'  => $this->json->serialize($transferObject->getBody()),
147
                    'response' => $this->json->serialize($response),
148
                ]
149
            );
150
        } catch (InvalidArgumentException $e) {
151
            $this->logger->debug(
152
                [
153
                    'exception' => $e->getMessage(),
154
                    'url'       => $url.'/v1/payments/credit/'.$paymentId.'/confirm',
155
                    'request'   => $this->json->serialize($transferObject->getBody()),
156
                    'response'  => $this->json->serialize($response),
157
                ]
158
            );
159
            // phpcs:ignore Magento2.Exceptions.DirectThrow
160
            throw new Exception('Invalid JSON was returned by the gateway');
161
        }
162
163
        return $response;
164
    }
165
}
166