AuthorizeRequest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 32
c 1
b 0
f 1
dl 0
loc 53
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 22 1
A sendData() 0 24 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Omnipay\Redsys\Message;
6
7
class AuthorizeRequest extends AbstractRequest
8
{
9
    use RedirectionTrait;
10
11
    public function getData()
12
    {
13
        $this->validate('merchantId', 'terminalId', 'amount', 'currency');
14
15
        return [
16
            // mandatory fields
17
            'Ds_Merchant_MerchantCode' => $this->getMerchantId(),
18
            'Ds_Merchant_Terminal' => $this->getTerminalId(),
19
            'Ds_Merchant_TransactionType' => '1',                          // Authorisation
20
            'Ds_Merchant_Amount' => $this->getAmountInteger(),
21
            'Ds_Merchant_Currency' => $this->getCurrencyNumeric(),  // uses ISO-4217 codes
22
            'Ds_Merchant_Order' => $this->getTransactionId(),
23
            'Ds_Merchant_MerchantUrl' => $this->getNotifyUrl(),
24
            // optional fields
25
            'Ds_Merchant_ProductDescription' => $this->getDescription(),
26
            'Ds_Merchant_MerchantDescriptor' => mb_substr($this->getDescription(), 0, 25),
27
            'Ds_Merchant_Cardholder' => $this->getCardholder(),
28
            'Ds_Merchant_UrlOK' => $this->getReturnUrl(),
29
            'Ds_Merchant_UrlKO' => $this->getReturnUrl(),
30
            'Ds_Merchant_MerchantName' => $this->getMerchantName(),
31
            'Ds_Merchant_ConsumerLanguage' => $this->getConsumerLanguage(),
32
            'Ds_Merchant_MerchantData' => $this->getMerchantData(),
33
        ];
34
    }
35
36
    public function sendData($data)
37
    {
38
        // Avoid sending null parameters, as Redsys will read the keyword null as a string "null"
39
        foreach ($data as $dataKey => $dataValue) {
40
            if (null === $dataValue) {
41
                unset($data[$dataKey]);
42
            }
43
        }
44
45
        $security = new Security();
46
47
        $encoded_data = $security->encodeMerchantParameters($data);
48
49
        $response_data = [
50
            'Ds_SignatureVersion' => Security::VERSION,
51
            'Ds_MerchantParameters' => $encoded_data,
52
            'Ds_Signature' => $security->createSignature(
53
                $encoded_data,
54
                $data['Ds_Merchant_Order'],
55
                $this->getHmacKey()
56
            ),
57
        ];
58
59
        return $this->response = new PurchaseResponse($this, $response_data);
60
    }
61
}
62