PurchaseRequest::getData()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

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