|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace LauLamanApps\IzettleApi\Client\Purchase; |
|
6
|
|
|
|
|
7
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\AbstractPayment; |
|
8
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Payment\CardPayment; |
|
9
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Payment\CashPayment; |
|
10
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Payment\InvoicePayment; |
|
11
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Payment\MobilePayment; |
|
12
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Payment\SwishPayment; |
|
13
|
|
|
use LauLamanApps\IzettleApi\API\Purchase\Payment\VippsPayment; |
|
14
|
|
|
use LauLamanApps\IzettleApi\Client\Exceptions\PaymentTypeNotConfiguredException; |
|
15
|
|
|
use Money\Currency; |
|
16
|
|
|
use Money\Money; |
|
17
|
|
|
use Ramsey\Uuid\Uuid; |
|
18
|
|
|
|
|
19
|
|
|
final class PaymentBuilder implements PaymentBuilderInterface |
|
20
|
|
|
{ |
|
21
|
|
|
const CARD = 'IZETTLE_CARD'; |
|
22
|
|
|
const CASH = 'IZETTLE_CASH'; |
|
23
|
|
|
const INVOICE = 'IZETTLE_INVOICE'; |
|
24
|
|
|
const MOBILE = 'MOBILE_PAY'; |
|
25
|
|
|
const SWISH = 'SWISH'; |
|
26
|
|
|
const VIPPS = 'VIPPS'; |
|
27
|
|
|
|
|
28
|
1 |
|
public function buildFromJsonArray(array $payments, Currency $currency): array |
|
29
|
|
|
{ |
|
30
|
1 |
|
$data = []; |
|
31
|
1 |
|
foreach ($payments as $payment) { |
|
32
|
1 |
|
$data[] = $this->buildFromJson($payment, $currency); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
return $data; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
9 |
|
public function buildFromJson(array $payment, Currency $currency): AbstractPayment |
|
39
|
|
|
{ |
|
40
|
9 |
|
switch ($payment['type']) { |
|
41
|
9 |
|
case self::CARD: |
|
42
|
2 |
|
return $this->parseCardPayment($payment, $currency); |
|
43
|
7 |
|
case self::CASH: |
|
44
|
2 |
|
return $this->parseCashPayment($payment, $currency); |
|
45
|
5 |
|
case self::INVOICE: |
|
46
|
1 |
|
return $this->parseInvoicePayment($payment, $currency); |
|
47
|
4 |
|
case self::MOBILE: |
|
48
|
1 |
|
return $this->parseMobilePayment($payment, $currency); |
|
49
|
3 |
|
case self::SWISH: |
|
50
|
1 |
|
return $this->parseSwichPayment($payment, $currency); |
|
51
|
2 |
|
case self::VIPPS: |
|
52
|
1 |
|
return $this->parseVippsPayment($payment, $currency); |
|
53
|
|
|
default: |
|
54
|
1 |
|
throw new PaymentTypeNotConfiguredException('Payment type \'' . $payment['type'] . '\' not configured'); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
private function parseCardPayment($payment, Currency $currency): CardPayment |
|
59
|
|
|
{ |
|
60
|
2 |
|
return new CardPayment( |
|
61
|
2 |
|
Uuid::fromString($payment['uuid']), |
|
62
|
2 |
|
new Money($payment['amount'], $currency), |
|
63
|
2 |
|
$payment['attributes']['referenceNumber'], |
|
64
|
2 |
|
$payment['attributes']['maskedPan'], |
|
65
|
2 |
|
$payment['attributes']['cardType'], |
|
66
|
2 |
|
$payment['attributes']['cardPaymentEntryMode'], |
|
67
|
2 |
|
$this->getFromKey('applicationName', $payment['attributes']), |
|
68
|
2 |
|
$this->getFromKey('applicationIdentifier', $payment['attributes']), |
|
69
|
2 |
|
$this->getFromKey('terminalVerificationResults', $payment['attributes']), |
|
70
|
2 |
|
(int) $this->getFromKey('nrOfInstallments', $payment['attributes']) |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
private function parseCashPayment($payment, Currency $currency): CashPayment |
|
75
|
|
|
{ |
|
76
|
2 |
|
return new CashPayment( |
|
77
|
2 |
|
Uuid::fromString($payment['uuid']), |
|
78
|
2 |
|
new Money($payment['amount'], $currency), |
|
79
|
2 |
|
new Money($payment['attributes']['handedAmount'], $currency) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
View Code Duplication |
private function parseInvoicePayment($payment, Currency $currency): InvoicePayment |
|
84
|
|
|
{ |
|
85
|
1 |
|
return new InvoicePayment( |
|
86
|
1 |
|
Uuid::fromString($payment['uuid']), |
|
87
|
1 |
|
new Money($payment['amount'], $currency) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
View Code Duplication |
private function parseMobilePayment($payment, Currency $currency): MobilePayment |
|
92
|
|
|
{ |
|
93
|
1 |
|
return new MobilePayment( |
|
94
|
1 |
|
Uuid::fromString($payment['uuid']), |
|
95
|
1 |
|
new Money($payment['amount'], $currency) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
View Code Duplication |
private function parseSwichPayment($payment, Currency $currency): SwishPayment |
|
100
|
|
|
{ |
|
101
|
1 |
|
return new SwishPayment( |
|
102
|
1 |
|
Uuid::fromString($payment['uuid']), |
|
103
|
1 |
|
new Money($payment['amount'], $currency) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
View Code Duplication |
private function parseVippsPayment($payment, Currency $currency): VippsPayment |
|
108
|
|
|
{ |
|
109
|
1 |
|
return new VippsPayment( |
|
110
|
1 |
|
Uuid::fromString($payment['uuid']), |
|
111
|
1 |
|
new Money($payment['amount'], $currency) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
private function getFromKey($key, array $data) |
|
116
|
|
|
{ |
|
117
|
2 |
|
if (!array_key_exists($key, $data)) { |
|
118
|
1 |
|
return null; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
return $data[$key]; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|