PurchaseRequest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
eloc 32
c 4
b 0
f 2
dl 0
loc 53
rs 10
ccs 34
cts 34
cp 1
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 22 2
A sendData() 0 24 3
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