PaymentRequest::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 20
cts 20
cp 1
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 19
nc 1
nop 0
crap 1
1
<?php
2
namespace Loevgaard\AltaPay\Payload;
3
4
use Assert\Assert;
5
use Loevgaard\AltaPay;
6
use Loevgaard\AltaPay\Payload\PaymentRequest\Config;
7
use Loevgaard\AltaPay\Payload\PaymentRequest\ConfigInterface;
8
use Loevgaard\AltaPay\Payload\PaymentRequest\CustomerInfo;
9
use Loevgaard\AltaPay\Payload\PaymentRequest\CustomerInfoInterface;
10
use Money\Money;
11
12
class PaymentRequest extends Payload implements PaymentRequestInterface
13
{
14
    use OrderLineArrayTrait;
15
16
    const ACCOUNT_OFFER_REQUIRED = 'required';
17
    const ACCOUNT_OFFER_DISABLED = 'disabled';
18
19
    const PAYMENT_SOURCE_ECOMMERCE = 'eCommerce';
20
    const PAYMENT_SOURCE_MOBI = 'mobi';
21
    const PAYMENT_SOURCE_MOTO = 'moto';
22
    const PAYMENT_SOURCE_MAIL_ORDER = 'mail_order';
23
    const PAYMENT_SOURCE_TELEPHONE_ORDER = 'telephone_order';
24
25
    const SHIPPING_METHOD_LOW_COST = 'LowCost';
26
    const SHIPPING_METHOD_DESIGNATED_BY_CUSTOMER = 'DesignatedByCustomer';
27
    const SHIPPING_METHOD_INTERNATIONAL = 'International';
28
    const SHIPPING_METHOD_MILITARY = 'Military';
29
    const SHIPPING_METHOD_NEXT_DAY = 'NextDay';
30
    const SHIPPING_METHOD_OTHER = 'Other';
31
    const SHIPPING_METHOD_STORE_PICKUP = 'StorePickup';
32
    const SHIPPING_METHOD_TWO_DAY_SERVICE = 'TwoDayService';
33
    const SHIPPING_METHOD_THREE_DAY_SERVICE = 'ThreeDayService';
34
35
    /**
36
     * @var string
37
     */
38
    private $terminal;
39
40
    /**
41
     * @var string
42
     */
43
    private $shopOrderId;
44
45
    /**
46
     * @var int
47
     */
48
    private $amount;
49
50
    /**
51
     * Currency in ISO-4217 format
52
     *
53
     * @var string
54
     */
55
    private $currency;
56
57
    /**
58
     * @var string
59
     */
60
    private $language;
61
62
    /**
63
     * @var array
64
     */
65
    private $transactionInfo;
66
67
    /**
68
     * @var string
69
     */
70
    private $type;
71
72
    /**
73
     * @var string
74
     */
75
    private $ccToken;
76
77
    /**
78
     * @var string
79
     */
80
    private $saleReconciliationIdentifier;
81
82
    /**
83
     * @var string
84
     */
85
    private $saleInvoiceNumber;
86
87
    /**
88
     * @var int
89
     */
90
    private $salesTax;
91
92
    /**
93
     * @var array
94
     */
95
    private $cookieParts;
96
97
    /**
98
     * @var string
99
     */
100
    private $paymentSource;
101
102
    /**
103
     * @var string
104
     */
105
    private $fraudService;
106
107
    /**
108
     * @var string
109
     */
110
    private $shippingMethod;
111
112
    /**
113
     * @var \DateTimeInterface
114
     */
115
    private $customerCreatedDate;
116
117
    /**
118
     * @var string
119
     */
120
    private $organisationNumber;
121
122
    /**
123
     * @var string
124
     */
125
    private $accountOffer;
126
127
    /**
128
     * @var CustomerInfoInterface
129
     */
130
    private $customerInfo;
131
132
    /**
133
     * @var ConfigInterface
134
     */
135
    private $config;
136
137 15
    public function __construct(string $terminal, string $shopOrderId, Money $amount)
138
    {
139 15
        $this->cookieParts = [];
140 15
        $this->orderLines = [];
141
142 15
        $this->currency = $amount->getCurrency()->getCode();
143 15
        $this->setTerminal($terminal);
144 15
        $this->setShopOrderId($shopOrderId);
145 15
        $this->setAmount($amount);
146 15
    }
147
148
    /**
149
     * @return array
150
     */
151 9
    public function getPayload() : array
152
    {
153 9
        $this->validate();
154
155 9
        $cookie = static::parseCookieParts($this->cookieParts);
156
157
        $payload = [
158 9
            'terminal' => $this->terminal,
159 9
            'shop_orderid' => $this->shopOrderId,
160 9
            'amount' => AltaPay\floatFromMoney($this->getAmount()),
161 9
            'currency' => $this->getAmount()->getCurrency()->getCode(),
162 9
            'language' => $this->language,
163 9
            'transaction_info' => $this->transactionInfo,
164 9
            'type' => $this->type,
165 9
            'ccToken' => $this->ccToken,
166 9
            'sale_reconciliation_identifier' => $this->saleReconciliationIdentifier,
167 9
            'sale_invoice_number' => $this->saleInvoiceNumber,
168 9
            'sales_tax' => AltaPay\floatFromMoney($this->getSalesTax()),
169 9
            'cookie' => $cookie,
170 9
            'payment_source' => $this->paymentSource,
171 9
            'fraud_service' => $this->fraudService,
172 9
            'shipping_method' => $this->shippingMethod,
173 9
            'customer_created_date' => $this->customerCreatedDate ? $this->customerCreatedDate->format('Y-m-d') : null,
174 9
            'organisation_number' => $this->organisationNumber,
175 9
            'account_offer' => $this->accountOffer,
176 9
            'config' => $this->getConfig(),
177 9
            'customer_info' => $this->getCustomerInfo(),
178 9
            'orderLines' => $this->orderLines,
179
        ];
180
181 9
        return static::simplePayload($payload);
182
    }
183
184 9
    public function validate()
185
    {
186 9
        Assert::that($this->terminal)->string();
187 9
        Assert::that($this->shopOrderId)->string();
188 9
        Assert::that($this->getAmount())->isInstanceOf(Money::class);
189 9
        Assert::that($this->currency)->string();
190 9
        Assert::thatNullOr($this->language)->string();
191 9
        Assert::thatNullOr($this->transactionInfo)->isArray();
192 9
        Assert::thatNullOr($this->type)->string();
193 9
        Assert::thatNullOr($this->ccToken)->string();
194 9
        Assert::thatNullOr($this->saleReconciliationIdentifier)->string();
195 9
        Assert::thatNullOr($this->saleInvoiceNumber)->string();
196 9
        Assert::thatNullOr($this->getSalesTax())->isInstanceOf(Money::class);
197 9
        Assert::thatNullOr($this->paymentSource)->string();
198 9
        Assert::thatNullOr($this->fraudService)->string();
199 9
        Assert::thatNullOr($this->shippingMethod)->string();
200 9
        Assert::thatNullOr($this->customerCreatedDate)->isInstanceOf(\DateTimeInterface::class);
201 9
        Assert::thatNullOr($this->organisationNumber)->string();
202 9
        Assert::thatNullOr($this->accountOffer)->string();
203 9
        Assert::thatNullOr($this->orderLines)->isArray();
204 9
    }
205
206
    /**
207
     * Takes an array of cookie parts and returns an urlencoded string ready to send
208
     *
209
     * @param array $cookieParts
210
     * @return string
211
     */
212 12
    public static function parseCookieParts(array $cookieParts)
213
    {
214 12
        $cookie = '';
215 12
        foreach ($cookieParts as $key => $val) {
216 6
            $cookie .= $key.'='.rawurlencode($val).';';
217
        }
218 12
        $cookie = trim($cookie, ';');
219
220 12
        return $cookie;
221
    }
222
223
    /**
224
     * @param string $key
225
     * @return string
226
     */
227 3
    public function getCookiePart(string $key) : string
228
    {
229 3
        return isset($this->cookieParts[$key]) ? $this->cookieParts[$key] : '';
230
    }
231
232
    /**
233
     * @param string $key
234
     * @param string $value
235
     * @return PaymentRequest
236
     */
237 6
    public function setCookiePart(string $key, string $value) : self
238
    {
239 6
        $this->cookieParts[$key] = $value;
240 6
        return $this;
241
    }
242
243
    /**
244
     * @return string
245
     */
246 6
    public function getTerminal() : string
247
    {
248 6
        return $this->terminal;
249
    }
250
251
    /**
252
     * @param string $terminal
253
     * @return PaymentRequest
254
     */
255 15
    public function setTerminal(string $terminal) : self
256
    {
257 15
        $this->terminal = $terminal;
258 15
        return $this;
259
    }
260
261
    /**
262
     * @return string
263
     */
264 6
    public function getShopOrderId() : string
265
    {
266 6
        return $this->shopOrderId;
267
    }
268
269
    /**
270
     * @param string $shopOrderId
271
     * @return PaymentRequest
272
     */
273 15
    public function setShopOrderId(string $shopOrderId) : self
274
    {
275 15
        $this->shopOrderId = $shopOrderId;
276 15
        return $this;
277
    }
278
279
    /**
280
     * @return Money
281
     */
282 15
    public function getAmount() : ?Money
283
    {
284 15
        return AltaPay\createMoney((string)$this->currency, (int)$this->amount);
285
    }
286
287
    /**
288
     * @param Money $amount
289
     * @return PaymentRequest
290
     */
291 15
    public function setAmount(Money $amount) : self
292
    {
293 15
        if ($amount->getCurrency()->getCode() !== $this->currency) {
294
            throw new \InvalidArgumentException('The $amount does not have the same currency as this payment request');
295
        }
296
297 15
        $this->amount = $amount->getAmount();
0 ignored issues
show
Documentation Bug introduced by
The property $amount was declared of type integer, but $amount->getAmount() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
298
299 15
        return $this;
300
    }
301
302
    /**
303
     * @return string
304
     */
305 3
    public function getLanguage() : ?string
306
    {
307 3
        return $this->language;
308
    }
309
310
    /**
311
     * @param string $language
312
     * @return PaymentRequest
313
     */
314 6
    public function setLanguage(string $language) : self
315
    {
316 6
        $this->language = $language;
317 6
        return $this;
318
    }
319
320
    /**
321
     * @return array
322
     */
323 3
    public function getTransactionInfo() : ?array
324
    {
325 3
        return $this->transactionInfo;
326
    }
327
328
    /**
329
     * @param array $transactionInfo
330
     * @return PaymentRequest
331
     */
332 6
    public function setTransactionInfo(array $transactionInfo) : self
333
    {
334 6
        $this->transactionInfo = $transactionInfo;
335 6
        return $this;
336
    }
337
338
    /**
339
     * @return string
340
     */
341 3
    public function getType() : ?string
342
    {
343 3
        return $this->type;
344
    }
345
346
    /**
347
     * @param string $type
348
     * @return PaymentRequest
349
     */
350 6
    public function setType(string $type) : self
351
    {
352 6
        $this->type = $type;
353 6
        return $this;
354
    }
355
356
    /**
357
     * @return string
358
     */
359 3
    public function getCcToken() : ?string
360
    {
361 3
        return $this->ccToken;
362
    }
363
364
    /**
365
     * @param string $ccToken
366
     * @return PaymentRequest
367
     */
368 6
    public function setCcToken(string $ccToken) : self
369
    {
370 6
        $this->ccToken = $ccToken;
371 6
        return $this;
372
    }
373
374
    /**
375
     * @return string
376
     */
377 3
    public function getSaleReconciliationIdentifier() : ?string
378
    {
379 3
        return $this->saleReconciliationIdentifier;
380
    }
381
382
    /**
383
     * @param string $saleReconciliationIdentifier
384
     * @return PaymentRequest
385
     */
386 6
    public function setSaleReconciliationIdentifier(string $saleReconciliationIdentifier) : self
387
    {
388 6
        $this->saleReconciliationIdentifier = $saleReconciliationIdentifier;
389 6
        return $this;
390
    }
391
392
    /**
393
     * @return string
394
     */
395 3
    public function getSaleInvoiceNumber() : ?string
396
    {
397 3
        return $this->saleInvoiceNumber;
398
    }
399
400
    /**
401
     * @param string $saleInvoiceNumber
402
     * @return PaymentRequest
403
     */
404 6
    public function setSaleInvoiceNumber(string $saleInvoiceNumber) : self
405
    {
406 6
        $this->saleInvoiceNumber = $saleInvoiceNumber;
407 6
        return $this;
408
    }
409
410
    /**
411
     * @return Money
412
     */
413 12
    public function getSalesTax() : ?Money
414
    {
415 12
        if (is_null($this->salesTax)) {
416 6
            return null;
417
        }
418
419 6
        return AltaPay\createMoney((string)$this->currency, (int)$this->salesTax);
420
    }
421
422
    /**
423
     * @param Money $salesTax
424
     * @return PaymentRequest
425
     */
426 6
    public function setSalesTax(Money $salesTax) : self
427
    {
428 6
        if ($salesTax->getCurrency()->getCode() !== $this->currency) {
429
            throw new \InvalidArgumentException('The $salesTax does not have the same currency as this payment request');
430
        }
431
432 6
        $this->salesTax = $salesTax->getAmount();
0 ignored issues
show
Documentation Bug introduced by
The property $salesTax was declared of type integer, but $salesTax->getAmount() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
433 6
        return $this;
434
    }
435
436
    /**
437
     * @return array
438
     */
439 3
    public function getCookieParts(): array
440
    {
441 3
        return $this->cookieParts;
442
    }
443
444
    /**
445
     * @param array $cookieParts
446
     * @return PaymentRequest
447
     */
448 3
    public function setCookieParts(array $cookieParts) : self
449
    {
450 3
        $this->cookieParts = $cookieParts;
451 3
        return $this;
452
    }
453
454
    /**
455
     * @return string
456
     */
457 3
    public function getPaymentSource() : ?string
458
    {
459 3
        return $this->paymentSource;
460
    }
461
462
    /**
463
     * @param string $paymentSource
464
     * @return PaymentRequest
465
     */
466 6
    public function setPaymentSource(string $paymentSource) : self
467
    {
468 6
        $this->paymentSource = $paymentSource;
469 6
        return $this;
470
    }
471
472
    /**
473
     * @return string
474
     */
475 3
    public function getFraudService() : ?string
476
    {
477 3
        return $this->fraudService;
478
    }
479
480
    /**
481
     * @param string $fraudService
482
     * @return PaymentRequest
483
     */
484 6
    public function setFraudService(string $fraudService) : self
485
    {
486 6
        $this->fraudService = $fraudService;
487 6
        return $this;
488
    }
489
490
    /**
491
     * @return string
492
     */
493 3
    public function getShippingMethod() : ?string
494
    {
495 3
        return $this->shippingMethod;
496
    }
497
498
    /**
499
     * @param string $shippingMethod
500
     * @return PaymentRequest
501
     */
502 6
    public function setShippingMethod(string $shippingMethod) : self
503
    {
504 6
        $this->shippingMethod = $shippingMethod;
505 6
        return $this;
506
    }
507
508
    /**
509
     * @return \DateTimeInterface
510
     */
511 3
    public function getCustomerCreatedDate() : ?\DateTimeInterface
512
    {
513 3
        return $this->customerCreatedDate;
514
    }
515
516
    /**
517
     * @param \DateTimeInterface $customerCreatedDate
518
     * @return PaymentRequest
519
     */
520 6
    public function setCustomerCreatedDate(\DateTimeInterface $customerCreatedDate) : self
521
    {
522 6
        $this->customerCreatedDate = $customerCreatedDate;
523 6
        return $this;
524
    }
525
526
    /**
527
     * @return string
528
     */
529 3
    public function getOrganisationNumber() : ?string
530
    {
531 3
        return $this->organisationNumber;
532
    }
533
534
    /**
535
     * @param string $organisationNumber
536
     * @return PaymentRequest
537
     */
538 6
    public function setOrganisationNumber(string $organisationNumber) : self
539
    {
540 6
        $this->organisationNumber = $organisationNumber;
541 6
        return $this;
542
    }
543
544
    /**
545
     * @return string
546
     */
547 3
    public function getAccountOffer() : ?string
548
    {
549 3
        return $this->accountOffer;
550
    }
551
552
    /**
553
     * @param string $accountOffer
554
     * @return PaymentRequest
555
     */
556 6
    public function setAccountOffer(string $accountOffer) : self
557
    {
558 6
        $this->accountOffer = $accountOffer;
559 6
        return $this;
560
    }
561
562
    /**
563
     * @return CustomerInfoInterface
564
     */
565 9
    public function getCustomerInfo() : CustomerInfoInterface
566
    {
567 9
        if (!$this->customerInfo) {
568 3
            $this->customerInfo = new CustomerInfo();
569
        }
570 9
        return $this->customerInfo;
571
    }
572
573
    /**
574
     * @param CustomerInfoInterface $customerInfo
575
     * @return PaymentRequest
576
     */
577 6
    public function setCustomerInfo(CustomerInfoInterface $customerInfo) : self
578
    {
579 6
        $this->customerInfo = $customerInfo;
580 6
        return $this;
581
    }
582
583
    /**
584
     * @return ConfigInterface
585
     */
586 9
    public function getConfig() : ConfigInterface
587
    {
588 9
        if (!$this->config) {
589 6
            $this->config = new Config();
590
        }
591 9
        return $this->config;
592
    }
593
594
    /**
595
     * @param ConfigInterface $config
596
     * @return PaymentRequest
597
     */
598 3
    public function setConfig(ConfigInterface $config) : self
599
    {
600 3
        $this->config = $config;
601 3
        return $this;
602
    }
603
}
604