Passed
Push — master ( 669a46...82dcb0 )
by
unknown
10:39 queued 48s
created

VoidAuthorizeResponse::isSuccessful()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 6
c 1
b 0
f 1
nc 5
nop 0
dl 0
loc 10
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Omnipay\Redsys\Message;
6
7
use AvaiBookSports\Component\RedsysMessages\Exception\CatalogNotFoundException;
8
use AvaiBookSports\Component\RedsysMessages\Factory;
9
use AvaiBookSports\Component\RedsysMessages\Loader\CatalogLoader;
10
use Http\Discovery\MessageFactoryDiscovery;
11
use Omnipay\Common\Exception\InvalidResponseException;
12
use Omnipay\Common\Http\Exception\RequestException;
13
use Omnipay\Common\Message\AbstractResponse;
14
use Omnipay\Common\Message\RequestInterface;
15
16
class VoidAuthorizeResponse extends AbstractResponse
17
{
18
    protected $returnSignature;
19
20
    /** @var bool */
21
    protected $usingUpcaseResponse = false;
22
23
    /** @var CatalogInterface */
0 ignored issues
show
Bug introduced by
The type Omnipay\Redsys\Message\CatalogInterface 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...
24
    protected $redsysMessages;
25
26
    public function __construct(RequestInterface $request, $data)
27
    {
28
        parent::__construct($request, $data);
29
30
        $security = new Security();
31
32
        try {
33
            $this->redsysMessages = (new Factory(new CatalogLoader()))->createCatalogByLanguage(array_key_exists('language', $this->request->getParameters()) ? $this->request->getParameters()['language'] : 'en');
0 ignored issues
show
Documentation Bug introduced by
It seems like new AvaiBookSports\Compo...s()['language'] : 'en') of type AvaiBookSports\Component...ssages\CatalogInterface is incompatible with the declared type Omnipay\Redsys\Message\CatalogInterface of property $redsysMessages.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
34
        } catch (CatalogNotFoundException $e) {
35
            $this->redsysMessages = (new Factory(new CatalogLoader()))->createCatalogByLanguage('en');
36
        }
37
38
        if (!isset($data['CODIGO'])) {
39
            throw new InvalidResponseException('Invalid response from payment gateway (no data)');
40
        }
41
42
        if (!isset($data['OPERACION'])) {
43
            if ('0' == $data['CODIGO']) {
44
                throw new InvalidResponseException('Invalid response from payment gateway (no data)');
45
            }
46
        }
47
48
        // Exceeder API rate limit
49
        if ('SIS0295' == $data['CODIGO'] || '9295' == $data['CODIGO']) {
50
            throw new RequestException('Too many requests. "'.$data['CODIGO'].'"', MessageFactoryDiscovery::find()->createRequest('POST', $this->getRequest()->getEndpoint(), ['SOAPAction' => 'trataPeticion']));
0 ignored issues
show
Bug introduced by
The method getEndpoint() does not exist on Omnipay\Common\Message\RequestInterface. It seems like you code against a sub-type of Omnipay\Common\Message\RequestInterface such as Omnipay\Redsys\Message\AbstractRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
            throw new RequestException('Too many requests. "'.$data['CODIGO'].'"', MessageFactoryDiscovery::find()->createRequest('POST', $this->getRequest()->/** @scrutinizer ignore-call */ getEndpoint(), ['SOAPAction' => 'trataPeticion']));
Loading history...
51
        }
52
53
        if (isset($data['OPERACION']['DS_ORDER'])) {
54
            $this->usingUpcaseResponse = true;
55
        }
56
57
        if (!empty($data['OPERACION'])) {
58
            if (!empty($data['OPERACION']['Ds_CardNumber'])) {
59
                $signature_keys = [
60
                    'Ds_Amount',
61
                    'Ds_Order',
62
                    'Ds_MerchantCode',
63
                    'Ds_Currency',
64
                    'Ds_Response',
65
                    'Ds_CardNumber',
66
                    'Ds_TransactionType',
67
                    'Ds_SecurePayment',
68
                ];
69
            } else {
70
                $signature_keys = [
71
                    'Ds_Amount',
72
                    'Ds_Order',
73
                    'Ds_MerchantCode',
74
                    'Ds_Currency',
75
                    'Ds_Response',
76
                    'Ds_TransactionType',
77
                    'Ds_SecurePayment',
78
                ];
79
            }
80
81
            $signature_data = '';
82
            foreach ($signature_keys as $key) {
83
                $value = $this->getKey($key);
84
                if (null === $value) {
85
                    throw new InvalidResponseException('Invalid response from payment gateway (missing data)');
86
                }
87
                $signature_data .= $value;
88
            }
89
90
            $this->returnSignature = $security->createSignature(
91
                $signature_data,
92
                $this->getKey('Ds_Order'),
93
                $this->request->getHmacKey()
0 ignored issues
show
Bug introduced by
The method getHmacKey() does not exist on Omnipay\Common\Message\RequestInterface. It seems like you code against a sub-type of Omnipay\Common\Message\RequestInterface such as Omnipay\Redsys\Message\AbstractRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
                $this->request->/** @scrutinizer ignore-call */ 
94
                                getHmacKey()
Loading history...
94
            );
95
96
            if ($this->returnSignature != $this->getKey('Ds_Signature')) {
97
                throw new InvalidResponseException('Invalid response from payment gateway (signature mismatch)');
98
            }
99
        }
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    public function isSuccessful()
106
    {
107
        $response_code = $this->getKey('Ds_Response');
108
109
        // check for field existence as well as value
110
        return isset($this->data['CODIGO'])
111
            && '0' == $this->data['CODIGO']
112
            && null !== $response_code
113
            && is_numeric($response_code)
114
            && 400 == $response_code;
115
    }
116
117
    /**
118
     * Helper method to get a specific response parameter if available.
119
     *
120
     * @param string $key The key to look up
121
     *
122
     * @return mixed|null
123
     */
124
    protected function getKey($key)
125
    {
126
        if ($this->usingUpcaseResponse) {
127
            $key = strtoupper($key);
128
        }
129
130
        return isset($this->data['OPERACION'][$key]) ? $this->data['OPERACION'][$key] : null;
131
    }
132
133
    /**
134
     * Get the authorisation code if available.
135
     *
136
     * @return string|null
137
     */
138
    public function getTransactionReference()
139
    {
140
        return $this->getKey('Ds_AuthorisationCode') ?? $this->request->getParameters()['transactionId'];
141
    }
142
143
    /**
144
     * Get the merchant message if available.
145
     *
146
     * @return string|null A response message from the payment gateway
147
     */
148
    public function getMessage()
149
    {
150
        $message = $this->redsysMessages->getDsResponseMessage($this->getCode());
151
152
        if (null === $message) {
153
            $message = $this->redsysMessages->getErrorMessage($this->getCode());
154
        }
155
156
        return $message;
157
    }
158
159
    /**
160
     * Get the merchant response code if available.
161
     *
162
     * @return string|null
163
     */
164
    public function getCode()
165
    {
166
        $code = $this->getKey('Ds_Response');
167
168
        if (null === $code) {
169
            $code = $this->data['CODIGO'];
170
        }
171
172
        return $code;
173
    }
174
175
    /**
176
     * Get the merchant data if available.
177
     *
178
     * @return string|null
179
     */
180
    public function getMerchantData()
181
    {
182
        return $this->getKey('Ds_MerchantData');
183
    }
184
185
    /**
186
     * Get the card country if available.
187
     *
188
     * @return string|null ISO 3166-1 (3-digit numeric) format, if supplied
189
     */
190
    public function getCardCountry()
191
    {
192
        return $this->getKey('Ds_Card_Country');
193
    }
194
}
195