Completed
Pull Request — master (#5)
by Michael
05:16
created

PurchaseRequest::setShippingAddress2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 8
    public function getConsumerLanguage()
43
    {
44 8
        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 7
    public function setConsumerLanguage($value)
53
    {
54 7
        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 7
        } elseif (!is_numeric($value)) {
60 7
            $value = isset(self::$consumerLanguages[$value]) ? self::$consumerLanguages[$value] : '001';
61 7
        }
62
63 7
        return $this->setParameter('consumerLanguage', $value);
64
    }
65
66 15
    public function getHmacKey()
67
    {
68 15
        return $this->getParameter('hmacKey');
69
    }
70
71 23
    public function setHmacKey($value)
72
    {
73 23
        return $this->setParameter('hmacKey', $value);
74
    }
75
76 7
    public function getMerchantData()
77
    {
78 7
        return $this->getParameter('merchantData');
79
    }
80
81 7
    public function setMerchantData($value)
82
    {
83 7
        return $this->setParameter('merchantData', $value);
84
    }
85
86 11
    public function getMerchantId()
87
    {
88 11
        return $this->getParameter('merchantId');
89
    }
90
91 23
    public function setMerchantId($value)
92
    {
93 23
        return $this->setParameter('merchantId', $value);
94
    }
95
96 11
    public function getMerchantName()
97
    {
98 11
        return $this->getParameter('merchantName');
99
    }
100
101 23
    public function setMerchantName($value)
102
    {
103 23
        return $this->setParameter('merchantName', $value);
104
    }
105
106 11
    public function getTerminalId()
107
    {
108 11
        return $this->getParameter('terminalId');
109
    }
110
111 23
    public function setTerminalId($value)
112
    {
113 23
        return $this->setParameter('terminalId', $value);
114
    }
115
116
    /**
117
     * Get the shippingCountry field
118
     *
119
     * @return int The shipping country as an ISO3166 numeric code, e.g. 840 for USA.
120
     */
121 15
    public function getShippingCountry()
122
    {
123 15
        return $this->getParameter('shippingCountry');
124 15
    }
125 15
126 15
    /**
127 15
     * Set the shippingCountry field
128 15
     *
129 15
     * @param int $value The shipping country as an ISO3166 numeric code, e.g. 840 for USA.
130
     * @return self
131 15
     */
132 15
    public function setShippingCountry($value)
133
    {
134 15
        return $this->setParameter('shippingCountry', $value);
135 15
    }
136
137 2
    /**
138
     * Get the shippingCity field
139 2
     *
140
     * @return string
141
     */
142
    public function getShippingCity()
143 2
    {
144 2
        return $this->getParameter('shippingCity');
145 2
    }
146 2
147 2
    /**
148 2
     * Set the shippingCity field
149 2
     *
150
     * @param string $value
151 2
     * @return self
152 2
     */
153 2
    public function setShippingCity($value)
154 2
    {
155 2
        return $this->setParameter('shippingCity', $value);
156 2
    }
157 2
158 2
    /**
159
     * Get the shippingState field
160
     *
161 1
     * @return string
162
     */
163 1
    public function getShippingState()
164
    {
165 1
        return $this->getParameter('shippingState');
166
    }
167
168 1
    /**
169 1
     * Set the shippingState field
170 1
     *
171 1
     * @param string $value
172 1
     * @return self
173 1
     */
174 1
    public function setShippingState($value)
175 1
    {
176
        return $this->setParameter('shippingState', $value);
177 1
    }
178
179
    /**
180 7
     * Get the shippingAddress3 field
181
     *
182 7
     * @return string
183
     */
184
    public function getShippingAddress3()
185 1
    {
186
        return $this->getParameter('shippingAddress3');
187
    }
188
189
    /**
190
     * Set the shippingAddress3 field
191
     *
192
     * @param string $value
193
     * @return self
194
     */
195
    public function setShippingAddress3($value)
196
    {
197
        return $this->setParameter('shippingAddress3', $value);
198
    }
199
200
    /**
201
     * Get the shippingAddress2 field
202
     *
203
     * @return string
204
     */
205
    public function getShippingAddress2()
206
    {
207
        return $this->getParameter('shippingAddress2');
208
    }
209
210
    /**
211
     * Set the shippingAddress2 field
212
     *
213
     * @param string $value
214
     * @return self
215
     */
216
    public function setShippingAddress2($value)
217
    {
218
        return $this->setParameter('shippingAddress2', $value);
219
    }
220
221
    /**
222
     * Get the shippingAddress1 field
223
     *
224
     * @return string
225
     */
226
    public function getShippingAddress1()
227
    {
228
        return $this->getParameter('shippingAddress1');
229
    }
230
231
    /**
232
     * Set the shippingAddress1 field
233
     *
234
     * @param string $value
235
     * @return self
236
     */
237
    public function setShippingAddress1($value)
238
    {
239
        return $this->setParameter('shippingAddress1', $value);
240
    }
241
242
    /**
243
     * Get the shippingPostcode field
244
     *
245
     * @return string
246
     */
247
    public function getShippingPostcode()
248
    {
249
        return $this->getParameter('shippingPostcode');
250
    }
251
252
    /**
253
     * Set the shippingPostcode field
254
     *
255
     * @param string $value
256
     * @return self
257
     */
258
    public function setShippingPostcode($value)
259
    {
260
        return $this->setParameter('shippingPostcode', $value);
261
    }
262
263
    /**
264
     * Get the email field
265
     *
266
     * @return string
267
     */
268
    public function getEmail()
269
    {
270
        return $this->getParameter('email');
271
    }
272
273
    /**
274
     * Set the email field
275
     *
276
     * @param string $value
277
     * @return self
278
     */
279
    public function setEmail($value)
280
    {
281
        return $this->setParameter('email', $value);
282
    }
283
284
    /**
285
     * Override the abstract method to add requirement that it must start with 4 numeric characters
286
     *
287
     * @param string|int $value The transaction ID (merchant order) to set for the transaction
288
     */
289
    public function setTransactionId($value)
290
    {
291
        $start = substr($value, 0, 4);
292
        $numerics = 0;
293
        foreach (str_split($start) as $char) {
294
            if (is_numeric($char)) {
295
                $numerics++;
296
            } else {
297
                break;
298
            }
299
        }
300
        $value = str_pad(substr($start, 0, $numerics), 4, 0, STR_PAD_LEFT).substr($value, $numerics);
301
302
        parent::setTransactionId($value);
303
    }
304
305
    public function getData()
306
    {
307
        $this->validate('merchantId', 'terminalId', 'amount', 'currency');
308
309
        return array(
310
            // mandatory fields
311
            'Ds_Merchant_MerchantCode'       => $this->getMerchantId(),
312
            'Ds_Merchant_Terminal'           => $this->getTerminalId(),
313
            'Ds_Merchant_TransactionType'    => '0',                          // Authorisation
314
            'Ds_Merchant_Amount'             => $this->getAmountInteger(),
315
            'Ds_Merchant_Currency'           => $this->getCurrencyNumeric(),  // uses ISO-4217 codes
316
            'Ds_Merchant_Order'              => $this->getTransactionId(),
317
            'Ds_Merchant_MerchantUrl'        => $this->getNotifyUrl(),
318
            // optional fields
319
            'Ds_Merchant_ProductDescription' => $this->getDescription(),
320
            'Ds_Merchant_Cardholder'         => $this->getCardholder(),
321
            'Ds_Merchant_UrlOK'              => $this->getReturnUrl(),
322
            'Ds_Merchant_UrlKO'              => $this->getReturnUrl(),
323
            'Ds_Merchant_MerchantName'       => $this->getMerchantName(),
324
            'Ds_Merchant_ConsumerLanguage'   => $this->getConsumerLanguage(),
325
            'Ds_Merchant_MerchantData'       => $this->getMerchantData(),
326
            // 3DS EMV fields
327
            'Ds_Merchant_Emv3Ds' => [
328
                  'shipAddrCountry'  => $this->getShippingCountry(),
329
                  'shipAddrCity'     => $this->getShippingCity(),
330
                  'shipAddrState'    => $this->getShippingState(),
331
                  'shipAddrLine3'    => $this->getShippingAddress3(),
332
                  'shipAddrLine2'    => $this->getShippingAddress2(),
333
                  'shipAddrLine1'    => $this->getShippingAddress1(),
334
                  'shipAddrPostCode' => $this->getShippingPostcode(),
335
                  'cardholderName'   => $this->getCardholder(),
336
                  'email'            => $this->getEmail(),
337
                  // 'mobilePhone'      => [
338
                  //     'cc'         => '123',
339
                  //     'subscriber' => '123456789'
340
                  // ],
341
            ],
342
        );
343
    }
344
345
    public function sendData($data)
346
    {
347
        $security = new Security;
348
349
        $encoded_data = $security->encodeMerchantParameters($data);
350
351
        $response_data = array(
352
            'Ds_SignatureVersion'   => Security::VERSION,
353
            'Ds_MerchantParameters' => $encoded_data,
354
            'Ds_Signature'          => $security->createSignature(
355
                $encoded_data,
356
                $data['Ds_Merchant_Order'],
357
                $this->getHmacKey()
358
            ),
359
        );
360
361
        return $this->response = new PurchaseResponse($this, $response_data);
362
    }
363
364
    public function getEndpoint()
365
    {
366
        return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
367
    }
368
}
369