Passed
Push — master ( 6f877c...669a46 )
by Pablo
12:02 queued 09:29
created

RefundResponse::getCardCountry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Omnipay\Redsys\Message;
4
5
use AvaiBookSports\Component\RedsysMessages\Exception\CatalogNotFoundException;
6
use AvaiBookSports\Component\RedsysMessages\Factory;
7
use AvaiBookSports\Component\RedsysMessages\Loader\CatalogLoader;
8
use Http\Discovery\MessageFactoryDiscovery;
9
use Omnipay\Common\Exception\InvalidResponseException;
10
use Omnipay\Common\Http\Exception\RequestException;
11
use Omnipay\Common\Message\AbstractResponse;
12
use Omnipay\Common\Message\RequestInterface;
13
14
/**
15
 * Redsys Purchase Response.
16
 */
17
class RefundResponse extends AbstractResponse
18
{
19
    /** @var string */
20
    protected $returnSignature;
21
22
    /** @var bool */
23
    protected $usingUpcaseResponse = false;
24
25
    /** @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...
26
    protected $redsysMessages;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param RequestInterface $request the initiating request
32
     * @param mixed            $data
33
     *
34
     * @throws InvalidResponseException If resopnse format is incorrect, data is missing, or signature does not match
35
     */
36 9
    public function __construct(RequestInterface $request, $data)
37
    {
38 9
        parent::__construct($request, $data);
39
40 9
        $security = new Security();
41
42
        try {
43 9
            $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...
44 9
        } catch (CatalogNotFoundException $e) {
45 1
            $this->redsysMessages = (new Factory(new CatalogLoader()))->createCatalogByLanguage('en');
46
        }
47
48 9
        if (!isset($data['CODIGO'])) {
49
            throw new InvalidResponseException('Invalid response from payment gateway (no data)');
50
        }
51
52 9
        if (!isset($data['OPERACION'])) {
53 3
            if ('0' == $data['CODIGO']) {
54
                throw new InvalidResponseException('Invalid response from payment gateway (no data)');
55
            }
56 3
        }
57
58
        // Exceeder API rate limit
59 9
        if ('SIS0295' == $data['CODIGO'] || '9295' == $data['CODIGO']) {
60 1
            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

60
            throw new RequestException('Too many requests. "'.$data['CODIGO'].'"', MessageFactoryDiscovery::find()->createRequest('POST', $this->getRequest()->/** @scrutinizer ignore-call */ getEndpoint(), ['SOAPAction' => 'trataPeticion']));
Loading history...
61
        }
62
63 8
        if (isset($data['OPERACION']['DS_ORDER'])) {
64 1
            $this->usingUpcaseResponse = true;
65 1
        }
66
67 8
        if (!empty($data['OPERACION'])) {
68 6
            if (!empty($data['OPERACION']['Ds_CardNumber'])) {
69
                $signature_keys = [
70 1
                    'Ds_Amount',
71 1
                    'Ds_Order',
72 1
                    'Ds_MerchantCode',
73 1
                    'Ds_Currency',
74 1
                    'Ds_Response',
75 1
                    'Ds_CardNumber',
76 1
                    'Ds_TransactionType',
77 1
                    'Ds_SecurePayment',
78 1
                ];
79 1
            } else {
80
                $signature_keys = [
81 5
                    'Ds_Amount',
82 5
                    'Ds_Order',
83 5
                    'Ds_MerchantCode',
84 5
                    'Ds_Currency',
85 5
                    'Ds_Response',
86 5
                    'Ds_TransactionType',
87 5
                    'Ds_SecurePayment',
88 5
                ];
89
            }
90
91 6
            $signature_data = '';
92 6
            foreach ($signature_keys as $key) {
93 6
                $value = $this->getKey($key);
94 6
                if (null === $value) {
95
                    throw new InvalidResponseException('Invalid response from payment gateway (missing data)');
96
                }
97 6
                $signature_data .= $value;
98 6
            }
99
100 6
            $this->returnSignature = $security->createSignature(
101 6
                $signature_data,
102 6
                $this->getKey('Ds_Order'),
103 6
                $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

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