Completed
Push — master ( 624537...1570a8 )
by Leith
02:20
created

PurchaseRequest::getConsumerLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Omnipay\Redsys\Message;
4
5
use Omnipay\Common\Message\AbstractRequest;
6
7
/**
8
 * Redsys Purchase Request
9
 */
10
class PurchaseRequest extends AbstractRequest
11
{
12
    /** @var string */
13
    protected $liveEndpoint = 'https://sis.redsys.es/sis/realizarPago';
14
    /** @var string */
15
    protected $testEndpoint = 'https://sis-t.redsys.es:25443/sis/realizarPago';
16
    /** @var array */
17
    protected static $consumerLanguages = array(
18
        'es' => '001', // Spanish
19
        'en' => '002', // English
20
        'ca' => '003', // Catalan - same as Valencian (010)
21
        'fr' => '004', // French
22
        'de' => '005', // German
23
        'nl' => '006', // Dutch
24
        'it' => '007', // Italian
25
        'sv' => '008', // Swedish
26
        'pt' => '009', // Portuguese
27
        'pl' => '011', // Polish
28
        'gl' => '012', // Galician
29
        'eu' => '013', // Basque
30
    );
31
32 2
    public function getCardholder()
33
    {
34 2
        return $this->getParameter('cardholder');
35
    }
36
37 4
    public function setCardholder($value)
38
    {
39 4
        return $this->setParameter('cardholder', $value);
40
    }
41
42 3
    public function getConsumerLanguage()
43
    {
44 3
        return $this->getParameter('consumerLanguage');
45
    }
46
47
    /**
48
     * Set the language presented to the consumer
49
     *
50
     * @param string|int Either the ISO 639-1 code to be converted, or the gateway's own numeric language code
51
     */
52 4
    public function setConsumerLanguage($value)
53
    {
54 4
        if (is_int($value)) {
55 1
            if ($value < 0 || $value > 13) {
56 1
                $value = 1;
57 1
            }
58 1
            $value = str_pad($value, 3, '0', STR_PAD_LEFT);
59 4
        } elseif (!is_numeric($value)) {
60 4
            $value = isset(self::$consumerLanguages[$value]) ? self::$consumerLanguages[$value] : '001';
61 4
        }
62
63 4
        return $this->setParameter('consumerLanguage', $value);
64
    }
65
66 6
    public function getHmacKey()
67
    {
68 6
        return $this->getParameter('hmacKey');
69
    }
70
71 12
    public function setHmacKey($value)
72
    {
73 12
        return $this->setParameter('hmacKey', $value);
74
    }
75
76 2
    public function getMerchantData()
77
    {
78 2
        return $this->getParameter('merchantData');
79
    }
80
81 4
    public function setMerchantData($value)
82
    {
83 4
        return $this->setParameter('merchantData', $value);
84
    }
85
86 4
    public function getMerchantId()
87
    {
88 4
        return $this->getParameter('merchantId');
89
    }
90
91 12
    public function setMerchantId($value)
92
    {
93 12
        return $this->setParameter('merchantId', $value);
94
    }
95
96 4
    public function getMerchantName()
97
    {
98 4
        return $this->getParameter('merchantName');
99
    }
100
101 12
    public function setMerchantName($value)
102
    {
103 12
        return $this->setParameter('merchantName', $value);
104
    }
105
106 4
    public function getTerminalId()
107
    {
108 4
        return $this->getParameter('terminalId');
109
    }
110
111 12
    public function setTerminalId($value)
112
    {
113 12
        return $this->setParameter('terminalId', $value);
114
    }
115
116
    /**
117
     * Override the abstract method to add requirement that it must start with 4 numeric characters
118
     *
119
     * @param string|int $value The transaction ID (merchant order) to set for the transaction
120
     */
121 8
    public function setTransactionId($value)
122
    {
123 8
        $start = substr($value, 0, 4);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
124 8
        $numerics = 0;
125 8
        foreach (str_split($start) as $char) {
126 8
            if (is_numeric($char)) {
127 8
                $numerics++;
128 8
            } else {
129 8
                break;
130
            }
131 8
        }
132 8
        $value = str_pad(substr($start, 0, $numerics), 4, 0, STR_PAD_LEFT).substr($value, $numerics);
133
134 8
        parent::setTransactionId($value);
135 8
    }
136
137 2
    public function getData()
138
    {
139 2
        $this->validate('merchantId', 'terminalId', 'amount', 'currency');
140
141
        return array(
142
            // mandatory fields
143 2
            'Ds_Merchant_MerchantCode'       => $this->getMerchantId(),
144 2
            'Ds_Merchant_Terminal'           => $this->getTerminalId(),
145 2
            'Ds_Merchant_TransactionType'    => '0',                          // Authorisation
146 2
            'Ds_Merchant_Amount'             => $this->getAmountInteger(),
147 2
            'Ds_Merchant_Currency'           => $this->getCurrencyNumeric(),  // uses ISO-4217 codes
148 2
            'Ds_Merchant_Order'              => $this->getTransactionId(),
149 2
            'Ds_Merchant_MerchantUrl'        => $this->getNotifyUrl(),
150
            // optional fields
151 2
            'Ds_Merchant_ProductDescription' => $this->getDescription(),
152 2
            'Ds_Merchant_Cardholder'         => $this->getCardholder(),
153 2
            'Ds_Merchant_UrlOK'              => $this->getReturnUrl(),
154 2
            'Ds_Merchant_UrlKO'              => $this->getReturnUrl(),
155 2
            'Ds_Merchant_MerchantName'       => $this->getMerchantName(),
156 2
            'Ds_Merchant_ConsumerLanguage'   => $this->getConsumerLanguage(),
157 2
            'Ds_Merchant_MerchantData'       => $this->getMerchantData(),
158 2
        );
159
    }
160
161 1
    public function sendData($data)
162
    {
163 1
        return $this->response = new PurchaseResponse($this, $data);
164
    }
165
166 2
    public function getEndpoint()
167
    {
168 2
        return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
169
    }
170
}
171