Completed
Push — master ( 77725b...f2a46e )
by Joachim
12:59
created

PaymentRequest::setCookieParts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace Loevgaard\AltaPay\Payload;
3
4
use Loevgaard\AltaPay\Payload\PaymentRequest\Config;
5
use Loevgaard\AltaPay\Payload\PaymentRequest\ConfigInterface;
6
use Loevgaard\AltaPay\Payload\PaymentRequest\CustomerInfo;
7
use Loevgaard\AltaPay\Payload\PaymentRequest\CustomerInfoInterface;
8
9
/**
10
 * @todo create assertions
11
 */
12
class PaymentRequest extends Payload implements PaymentRequestInterface
13
{
14
    const ACCOUNT_OFFER_REQUIRED = 'required';
15
    const ACCOUNT_OFFER_DISABLED = 'disabled';
16
17
    const PAYMENT_SOURCE_ECOMMERCE = 'eCommerce';
18
    const PAYMENT_SOURCE_MOBI = 'mobi';
19
    const PAYMENT_SOURCE_MOTO = 'moto';
20
    const PAYMENT_SOURCE_MAIL_ORDER = 'mail_order';
21
    const PAYMENT_SOURCE_TELEPHONE_ORDER = 'telephone_order';
22
23
    const SHIPPING_METHOD_LOW_COST = 'LowCost';
24
    const SHIPPING_METHOD_DESIGNATED_BY_CUSTOMER = 'DesignatedByCustomer';
25
    const SHIPPING_METHOD_INTERNATIONAL = 'International';
26
    const SHIPPING_METHOD_MILITARY = 'Military';
27
    const SHIPPING_METHOD_NEXT_DAY = 'NextDay';
28
    const SHIPPING_METHOD_OTHER = 'Other';
29
    const SHIPPING_METHOD_STORE_PICKUP = 'StorePickup';
30
    const SHIPPING_METHOD_TWO_DAY_SERVICE = 'TwoDayService';
31
    const SHIPPING_METHOD_THREE_DAY_SERVICE = 'ThreeDayService';
32
33
    /**
34
     * @var string
35
     */
36
    private $terminal;
37
38
    /**
39
     * @var string
40
     */
41
    private $shopOrderId;
42
43
    /**
44
     * @var float
45
     */
46
    private $amount;
47
48
    /**
49
     * Currency in ISO-4217 format
50
     *
51
     * @var string
52
     */
53
    private $currency;
54
55
    /**
56
     * @var string
57
     */
58
    private $language;
59
60
    /**
61
     * @var array
62
     */
63
    private $transactionInfo;
64
65
    /**
66
     * @var string
67
     */
68
    private $type;
69
70
    /**
71
     * @var string
72
     */
73
    private $ccToken;
74
75
    /**
76
     * @var string
77
     */
78
    private $saleReconciliationIdentifier;
79
80
    /**
81
     * @var string
82
     */
83
    private $saleInvoiceNumber;
84
85
    /**
86
     * @var float
87
     */
88
    private $salesTax;
89
90
    /**
91
     * @var array
92
     */
93
    private $cookieParts;
94
95
    /**
96
     * @var string
97
     */
98
    private $paymentSource;
99
100
    /**
101
     * @var string
102
     */
103
    private $fraudService;
104
105
    /**
106
     * @var string
107
     */
108
    private $shippingMethod;
109
110
    /**
111
     * @var string
112
     */
113
    private $customerCreatedDate;
114
115
    /**
116
     * @var string
117
     */
118
    private $organisationNumber;
119
120
    /**
121
     * @var string
122
     */
123
    private $accountOffer;
124
125
    /**
126
     * @var OrderLineInterface[]
127
     */
128
    private $orderLines;
129
130
    /**
131
     * @var CustomerInfoInterface
132
     */
133
    private $customerInfo;
134
135
    /**
136
     * @var ConfigInterface
137
     */
138
    private $config;
139
140 6
    public function __construct(string $terminal, string $shopOrderId, float $amount, string $currency)
141
    {
142 6
        $this->cookieParts = [];
143 6
        $this->orderLines = [];
144 6
        $this->setTerminal($terminal);
145 6
        $this->setShopOrderId($shopOrderId);
146 6
        $this->setAmount($amount);
147 6
        $this->setCurrency($currency);
148
    }
149
150
    /**
151
     * @return array
152 6
     */
153
    public function getPayload() : array
154
    {
155 6
        $cookie = static::parseCookieParts($this->cookieParts);
156 6
157 6
        $payload = [
158 6
            'terminal' => $this->getTerminal(),
159 6
            'shop_orderid' => $this->getShopOrderId(),
160 6
            'amount' => $this->getAmount(),
161 6
            'currency' => $this->getCurrency(),
162 6
            'language' => $this->getLanguage(),
163 6
            'transaction_info' => $this->getTransactionInfo(),
164 6
            'type' => $this->getType(),
165 6
            'ccToken' => $this->getCcToken(),
166 6
            'sale_reconciliation_identifier' => $this->getSaleReconciliationIdentifier(),
167 6
            'sale_invoice_number' => $this->getSaleInvoiceNumber(),
168 6
            'sales_tax' => $this->getSalesTax(),
169 6
            'cookie' => $cookie,
170 6
            'payment_source' => $this->getPaymentSource(),
171 6
            'fraud_service' => $this->getFraudService(),
172 6
            'shipping_method' => $this->getShippingMethod(),
173 4
            'customer_created_date' => $this->getCustomerCreatedDate(),
174
            'organisation_number' => $this->getOrganisationNumber(),
175
            'account_offer' => $this->getAccountOffer(),
176 6
        ];
177 6
178 3
        // set config payload if any
179 2
        $config = $this->getConfig()->getPayload();
180
        if (!empty($config)) {
181
            $payload['config'] = $config;
182 6
        }
183 6
184 3
        // set customer info payload if any
185 2
        $customerInfo = $this->getCustomerInfo()->getPayload();
186
        if (!empty($customerInfo)) {
187
            $payload['customer_info'] = $customerInfo;
188 6
        }
189 6
190 3
        // create order lines array
191 4
        $orderLines = [];
192
        foreach ($this->getOrderLines() as $orderLine) {
193 6
            $orderLines[] = $orderLine->getPayload();
194 3
        }
195 2
196
        if (!empty($orderLines)) {
197 6
            $payload['orderLines'] = $orderLines;
198
        }
199
200
        return $this->cleanPayload($payload);
201
    }
202
203
    /**
204 3
     * Takes an array of cookie parts and returns an urlencoded string ready to send
205
     *
206 3
     * @param array $cookieParts
207 3
     * @return string
208
     */
209
    public static function parseCookieParts(array $cookieParts) {
210
        $cookie = '';
211
        foreach ($cookieParts as $key => $val) {
212
            $cookie .= rawurlencode($key.'='.rawurlencode($val)).';';
213 6
        }
214
        $cookie = trim($cookie, ';');
215 6
216
        return $cookie;
217
    }
218
219
    /**
220
     * @param OrderLineInterface $orderLine
221
     * @return PaymentRequest
222 6
     */
223
    public function addOrderLine(OrderLineInterface $orderLine) : self
224 6
    {
225 6
        $this->orderLines[] = $orderLine;
226
        return $this;
227
    }
228
229
    /**
230
     * @param string $key
231 6
     * @return string
232
     */
233 6
    public function getCookiePart(string $key) : string
234
    {
235
        return isset($this->cookieParts[$key]) ? $this->cookieParts[$key] : '';
236
    }
237
238
    /**
239
     * @param string $key
240 6
     * @param string $value
241
     * @return PaymentRequest
242 6
     */
243 6
    public function setCookiePart(string $key, string $value) : self
244
    {
245
        $this->cookieParts[$key] = $value;
246
        return $this;
247
    }
248
249 6
    /**
250
     * @return string
251 6
     */
252
    public function getTerminal() : string
253
    {
254
        return $this->terminal;
255
    }
256
257
    /**
258 6
     * @param string $terminal
259
     * @return PaymentRequest
260 6
     */
261 6
    public function setTerminal(string $terminal) : self
262
    {
263
        $this->terminal = $terminal;
264
        return $this;
265
    }
266
267 6
    /**
268
     * @return string
269 6
     */
270
    public function getShopOrderId() : string
271
    {
272
        return $this->shopOrderId;
273
    }
274
275
    /**
276 6
     * @param string $shopOrderId
277
     * @return PaymentRequest
278 6
     */
279 6
    public function setShopOrderId(string $shopOrderId) : self
280
    {
281
        $this->shopOrderId = $shopOrderId;
282
        return $this;
283
    }
284
285 6
    /**
286
     * @return float
287 6
     */
288
    public function getAmount() : float
289
    {
290
        return $this->amount;
291
    }
292
293
    /**
294 3
     * @param float $amount
295
     * @return PaymentRequest
296 3
     */
297 3
    public function setAmount(float $amount) : self
298
    {
299
        $this->amount = $amount;
300
        return $this;
301
    }
302
303 6
    /**
304
     * @return string
305 6
     */
306
    public function getCurrency() : string
307
    {
308
        return $this->currency;
309
    }
310
311
    /**
312 3
     * @param string $currency
313
     * @return PaymentRequest
314 3
     */
315 3
    public function setCurrency(string $currency) : self
316
    {
317
        $this->currency = $currency;
318
        return $this;
319
    }
320
321 6
    /**
322
     * @return string
323 6
     */
324
    public function getLanguage() : ?string
325
    {
326
        return $this->language;
327
    }
328
329
    /**
330 3
     * @param string $language
331
     * @return PaymentRequest
332 3
     */
333 3
    public function setLanguage(string $language) : self
334
    {
335
        $this->language = $language;
336
        return $this;
337
    }
338
339 6
    /**
340
     * @return array
341 6
     */
342
    public function getTransactionInfo() : ?array
343
    {
344
        return $this->transactionInfo;
345
    }
346
347
    /**
348 3
     * @param array $transactionInfo
349
     * @return PaymentRequest
350 3
     */
351 3
    public function setTransactionInfo(array $transactionInfo) : self
352
    {
353
        $this->transactionInfo = $transactionInfo;
354
        return $this;
355
    }
356
357 6
    /**
358
     * @return string
359 6
     */
360
    public function getType() : ?string
361
    {
362
        return $this->type;
363
    }
364
365
    /**
366 3
     * @param string $type
367
     * @return PaymentRequest
368 3
     */
369 3
    public function setType(string $type) : self
370
    {
371
        $this->type = $type;
372
        return $this;
373
    }
374
375 6
    /**
376
     * @return string
377 6
     */
378
    public function getCcToken() : ?string
379
    {
380
        return $this->ccToken;
381
    }
382
383
    /**
384 3
     * @param string $ccToken
385
     * @return PaymentRequest
386 3
     */
387 3
    public function setCcToken(string $ccToken) : self
388
    {
389
        $this->ccToken = $ccToken;
390
        return $this;
391
    }
392
393 6
    /**
394
     * @return string
395 6
     */
396
    public function getSaleReconciliationIdentifier() : ?string
397
    {
398
        return $this->saleReconciliationIdentifier;
399
    }
400
401
    /**
402 3
     * @param string $saleReconciliationIdentifier
403
     * @return PaymentRequest
404 3
     */
405 3
    public function setSaleReconciliationIdentifier(string $saleReconciliationIdentifier) : self
406
    {
407
        $this->saleReconciliationIdentifier = $saleReconciliationIdentifier;
408
        return $this;
409
    }
410
411 6
    /**
412
     * @return string
413 6
     */
414
    public function getSaleInvoiceNumber() : ?string
415
    {
416
        return $this->saleInvoiceNumber;
417
    }
418
419
    /**
420 3
     * @param string $saleInvoiceNumber
421
     * @return PaymentRequest
422 3
     */
423 3
    public function setSaleInvoiceNumber(string $saleInvoiceNumber) : self
424
    {
425
        $this->saleInvoiceNumber = $saleInvoiceNumber;
426
        return $this;
427
    }
428
429 6
    /**
430
     * @return float
431 6
     */
432
    public function getSalesTax() : ?float
433
    {
434
        return $this->salesTax;
435
    }
436
437
    /**
438 3
     * @param float $salesTax
439
     * @return PaymentRequest
440 3
     */
441 3
    public function setSalesTax(float $salesTax) : self
442
    {
443
        $this->salesTax = $salesTax;
444
        return $this;
445
    }
446
447 6
    /**
448
     * @return array
449 6
     */
450
    public function getCookieParts(): array
451
    {
452
        return $this->cookieParts;
453
    }
454
455
    /**
456 3
     * @param array $cookieParts
457
     * @return PaymentRequest
458 3
     */
459 3
    public function setCookieParts(array $cookieParts) : self
460
    {
461
        $this->cookieParts = $cookieParts;
462
        return $this;
463
    }
464
465 6
    /**
466
     * @return string
467 6
     */
468
    public function getPaymentSource() : ?string
469
    {
470
        return $this->paymentSource;
471
    }
472
473
    /**
474 3
     * @param string $paymentSource
475
     * @return PaymentRequest
476 3
     */
477 3
    public function setPaymentSource(string $paymentSource) : self
478
    {
479
        $this->paymentSource = $paymentSource;
480
        return $this;
481
    }
482
483 6
    /**
484
     * @return string
485 6
     */
486
    public function getFraudService() : ?string
487
    {
488
        return $this->fraudService;
489
    }
490
491
    /**
492 3
     * @param string $fraudService
493
     * @return PaymentRequest
494 3
     */
495 3
    public function setFraudService(string $fraudService) : self
496
    {
497
        $this->fraudService = $fraudService;
498
        return $this;
499
    }
500
501 6
    /**
502
     * @return string
503 6
     */
504
    public function getShippingMethod() : ?string
505
    {
506
        return $this->shippingMethod;
507
    }
508
509
    /**
510 3
     * @param string $shippingMethod
511
     * @return PaymentRequest
512 3
     */
513 3
    public function setShippingMethod(string $shippingMethod) : self
514
    {
515
        $this->shippingMethod = $shippingMethod;
516
        return $this;
517
    }
518
519 6
    /**
520
     * @return string
521 6
     */
522
    public function getCustomerCreatedDate() : ?string
523
    {
524
        return $this->customerCreatedDate;
525
    }
526
527
    /**
528 3
     * @param string $customerCreatedDate
529
     * @return PaymentRequest
530 3
     */
531 3
    public function setCustomerCreatedDate(string $customerCreatedDate) : self
532
    {
533
        $this->customerCreatedDate = $customerCreatedDate;
534
        return $this;
535
    }
536
537 6
    /**
538
     * @return string
539 6
     */
540
    public function getOrganisationNumber() : ?string
541
    {
542
        return $this->organisationNumber;
543
    }
544
545
    /**
546
     * @param string $organisationNumber
547
     * @return PaymentRequest
548
     */
549
    public function setOrganisationNumber(string $organisationNumber) : self
550
    {
551
        $this->organisationNumber = $organisationNumber;
552
        return $this;
553
    }
554
555 6
    /**
556
     * @return string
557 6
     */
558 3
    public function getAccountOffer() : ?string
559 2
    {
560 6
        return $this->accountOffer;
561
    }
562
563
    /**
564
     * @param string $accountOffer
565
     * @return PaymentRequest
566
     */
567 3
    public function setAccountOffer(string $accountOffer) : self
568
    {
569 3
        $this->accountOffer = $accountOffer;
570 3
        return $this;
571
    }
572
573
    /**
574
     * @return OrderLineInterface[]
575
     */
576 6
    public function getOrderLines() : ?array
577
    {
578 6
        return $this->orderLines;
579 3
    }
580 2
581 6
    /**
582
     * @param OrderLineInterface[] $orderLines
583
     * @return PaymentRequest
584
     */
585
    public function setOrderLines(array $orderLines) : self
586
    {
587
        $this->orderLines = $orderLines;
588 3
        return $this;
589
    }
590 3
591 3
    /**
592
     * @return CustomerInfoInterface
593
     */
594
    public function getCustomerInfo() : CustomerInfoInterface
595
    {
596
        if (!$this->customerInfo) {
597
            $this->customerInfo = new CustomerInfo();
598
        }
599
        return $this->customerInfo;
600
    }
601
602
    /**
603
     * @param CustomerInfoInterface $customerInfo
604
     * @return PaymentRequest
605
     */
606
    public function setCustomerInfo(CustomerInfoInterface $customerInfo) : self
607
    {
608
        $this->customerInfo = $customerInfo;
609
        return $this;
610
    }
611
612
    /**
613
     * @return ConfigInterface
614
     */
615
    public function getConfig() : ConfigInterface
616
    {
617
        if (!$this->config) {
618
            $this->config = new Config();
619
        }
620
        return $this->config;
621
    }
622
623
    /**
624
     * @param ConfigInterface $config
625
     * @return PaymentRequest
626
     */
627
    public function setConfig(ConfigInterface $config) : self
628
    {
629
        $this->config = $config;
630
        return $this;
631
    }
632
}
633