1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\PayloadGenerator; |
4
|
|
|
|
5
|
|
|
use Loevgaard\AltaPay\Payload\OrderLine as OrderLinePayload; |
6
|
|
|
use Loevgaard\AltaPay\Payload\PaymentRequest as PaymentRequestPayload; |
7
|
|
|
use Loevgaard\AltaPay\Payload\PaymentRequest\Config as ConfigPayload; |
8
|
|
|
use Loevgaard\AltaPay\Payload\PaymentRequest\CustomerInfo as CustomerInfoPayload; |
9
|
|
|
use Loevgaard\Dandomain\Pay\Helper\ChecksumHelper; |
10
|
|
|
use Loevgaard\Dandomain\Pay\Model\Payment as DandomainPayment; |
11
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\Payment; |
12
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\Terminal; |
13
|
|
|
use Money\Money; |
14
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
15
|
|
|
use Symfony\Component\Routing\RouterInterface; |
16
|
|
|
|
17
|
|
|
class PaymentRequestPayloadGenerator implements PayloadGeneratorInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var RouterInterface |
21
|
|
|
*/ |
22
|
|
|
protected $router; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var DandomainPayment |
26
|
|
|
*/ |
27
|
|
|
protected $dandomainPayment; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Terminal |
31
|
|
|
*/ |
32
|
|
|
protected $terminal; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Payment |
36
|
|
|
*/ |
37
|
|
|
protected $payment; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ChecksumHelper |
41
|
|
|
*/ |
42
|
|
|
protected $checksumHelper; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $cookiePaymentId; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $cookieChecksumComplete; |
53
|
|
|
|
54
|
|
|
public function __construct( |
55
|
|
|
RouterInterface $router, |
56
|
|
|
DandomainPayment $paymentRequest, |
57
|
|
|
Terminal $terminal, |
58
|
|
|
Payment $payment, |
59
|
|
|
ChecksumHelper $checksumHelper, |
60
|
|
|
string $cookiePaymentId, |
61
|
|
|
string $cookieChecksumComplete |
62
|
|
|
) { |
63
|
|
|
$this->router = $router; |
64
|
|
|
$this->dandomainPayment = $paymentRequest; |
65
|
|
|
$this->terminal = $terminal; |
66
|
|
|
$this->payment = $payment; |
67
|
|
|
$this->checksumHelper = $checksumHelper; |
68
|
|
|
$this->cookiePaymentId = $cookiePaymentId; |
69
|
|
|
$this->cookieChecksumComplete = $cookieChecksumComplete; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return PaymentRequestPayload |
74
|
|
|
*/ |
75
|
|
|
public function generate(): PaymentRequestPayload |
76
|
|
|
{ |
77
|
|
|
$paymentRequestPayload = new PaymentRequestPayload( |
78
|
|
|
$this->terminal->getTitle(), |
79
|
|
|
$this->dandomainPayment->getOrderId(), |
80
|
|
|
$this->dandomainPayment->getTotalAmount() |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
foreach ($this->dandomainPayment->getPaymentLines() as $paymentLine) { |
84
|
|
|
$orderLinePayload = $this->createOrderLine( |
85
|
|
|
$paymentLine->getName(), |
86
|
|
|
$paymentLine->getProductNumber(), |
87
|
|
|
$paymentLine->getQuantity(), |
88
|
|
|
$paymentLine->getPriceExclVat(), |
89
|
|
|
$paymentLine->getVat() |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$paymentRequestPayload->addOrderLine($orderLinePayload); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// add payment fee as an order line if it's set |
96
|
|
View Code Duplication |
if ($this->dandomainPayment->getPaymentFee() && 0 !== (int) $this->dandomainPayment->getPaymentFee()->getAmount()) { |
|
|
|
|
97
|
|
|
$orderLinePayload = $this->createOrderLine( |
98
|
|
|
$this->dandomainPayment->getPaymentMethod(), |
99
|
|
|
$this->dandomainPayment->getPaymentMethod(), |
100
|
|
|
1, |
101
|
|
|
$this->dandomainPayment->getPaymentFee(), |
102
|
|
|
null, |
103
|
|
|
OrderLinePayload::GOODS_TYPE_HANDLING |
104
|
|
|
); |
105
|
|
|
$paymentRequestPayload->addOrderLine($orderLinePayload); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// add shipping fee as an order line if it's set |
109
|
|
View Code Duplication |
if ($this->dandomainPayment->getShippingFee() && 0 !== (int) $this->dandomainPayment->getShippingFee()->getAmount()) { |
|
|
|
|
110
|
|
|
$orderLinePayload = $this->createOrderLine( |
111
|
|
|
$this->dandomainPayment->getShippingMethod(), |
112
|
|
|
$this->dandomainPayment->getShippingMethod(), |
113
|
|
|
1, |
114
|
|
|
$this->dandomainPayment->getShippingFee(), |
115
|
|
|
null, |
116
|
|
|
OrderLinePayload::GOODS_TYPE_SHIPMENT |
117
|
|
|
); |
118
|
|
|
$paymentRequestPayload->addOrderLine($orderLinePayload); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$customerNames = explode(' ', $this->dandomainPayment->getCustomerName(), 2); |
122
|
|
|
$shippingNames = explode(' ', $this->dandomainPayment->getDeliveryName(), 2); |
123
|
|
|
|
124
|
|
|
$customerInfoPayload = $this->createCustomerInfo( |
125
|
|
|
$customerNames[0] ?? '', |
126
|
|
|
$customerNames[1] ?? '', |
127
|
|
|
$this->dandomainPayment->getCustomerAddress().($this->dandomainPayment->getCustomerAddress2() ? "\r\n".$this->dandomainPayment->getCustomerAddress2() : ''), |
128
|
|
|
$this->dandomainPayment->getCustomerZipCode(), |
129
|
|
|
$this->dandomainPayment->getCustomerCity(), |
130
|
|
|
$this->dandomainPayment->getCustomerCountryCode(), |
131
|
|
|
$shippingNames[0] ?? '', |
132
|
|
|
$shippingNames[1] ?? '', |
133
|
|
|
$this->dandomainPayment->getDeliveryAddress().($this->dandomainPayment->getDeliveryAddress2() ? "\r\n".$this->dandomainPayment->getDeliveryAddress2() : ''), |
134
|
|
|
$this->dandomainPayment->getDeliveryZipCode(), |
135
|
|
|
$this->dandomainPayment->getDeliveryCity(), |
136
|
|
|
$this->dandomainPayment->getDeliveryCountryCode() |
137
|
|
|
); |
138
|
|
|
$paymentRequestPayload->setCustomerInfo($customerInfoPayload); |
139
|
|
|
|
140
|
|
|
$configPayload = $this->createConfig( |
141
|
|
|
$this->router->generate('loevgaard_dandomain_altapay_callback_form', [], UrlGeneratorInterface::ABSOLUTE_URL), |
142
|
|
|
$this->router->generate('loevgaard_dandomain_altapay_callback_ok', [], UrlGeneratorInterface::ABSOLUTE_URL), |
143
|
|
|
$this->router->generate('loevgaard_dandomain_altapay_callback_fail', [], UrlGeneratorInterface::ABSOLUTE_URL), |
144
|
|
|
$this->router->generate('loevgaard_dandomain_altapay_callback_redirect', [], UrlGeneratorInterface::ABSOLUTE_URL), |
145
|
|
|
$this->router->generate('loevgaard_dandomain_altapay_callback_open', [], UrlGeneratorInterface::ABSOLUTE_URL), |
146
|
|
|
$this->router->generate('loevgaard_dandomain_altapay_callback_notification', [], UrlGeneratorInterface::ABSOLUTE_URL) |
147
|
|
|
); |
148
|
|
|
$paymentRequestPayload->setConfig($configPayload); |
149
|
|
|
|
150
|
|
|
$paymentRequestPayload |
151
|
|
|
->setCookiePart($this->cookiePaymentId, $this->payment->getId()) |
152
|
|
|
->setCookiePart($this->cookieChecksumComplete, $this->checksumHelper->getChecksum2()) |
153
|
|
|
; |
154
|
|
|
|
155
|
|
|
return $paymentRequestPayload; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $description |
160
|
|
|
* @param string $itemId |
161
|
|
|
* @param string $quantity |
162
|
|
|
* @param Money $unitPrice |
163
|
|
|
* @param float|null $taxPercent |
164
|
|
|
* @param string|null $goodsType |
165
|
|
|
* |
166
|
|
|
* @return OrderLinePayload |
167
|
|
|
*/ |
168
|
|
|
protected function createOrderLine( |
169
|
|
|
string $description, |
170
|
|
|
string $itemId, |
171
|
|
|
string $quantity, |
172
|
|
|
Money $unitPrice, |
173
|
|
|
float $taxPercent = null, |
174
|
|
|
string $goodsType = null |
175
|
|
|
): OrderLinePayload { |
176
|
|
|
$payload = new OrderLinePayload($description, $itemId, $quantity, $unitPrice); |
177
|
|
|
|
178
|
|
|
if ($taxPercent) { |
179
|
|
|
$payload->setTaxPercent($taxPercent); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if ($goodsType) { |
|
|
|
|
183
|
|
|
$payload->setGoodsType($goodsType); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $payload; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $billingFirstName |
191
|
|
|
* @param string $billingLastName |
192
|
|
|
* @param string $billingAddress |
193
|
|
|
* @param string $billingPostal |
194
|
|
|
* @param string $billingCity |
195
|
|
|
* @param string $billingCountry |
196
|
|
|
* @param string $shippingFirstName |
197
|
|
|
* @param string $shippingLastName |
198
|
|
|
* @param string $shippingAddress |
199
|
|
|
* @param string $shippingPostal |
200
|
|
|
* @param string $shippingCity |
201
|
|
|
* @param string $shippingCountry |
202
|
|
|
* |
203
|
|
|
* @return CustomerInfoPayload |
204
|
|
|
*/ |
205
|
|
|
protected function createCustomerInfo( |
206
|
|
|
string $billingFirstName, |
207
|
|
|
string $billingLastName, |
208
|
|
|
string $billingAddress, |
209
|
|
|
string $billingPostal, |
210
|
|
|
string $billingCity, |
211
|
|
|
string $billingCountry, |
212
|
|
|
string $shippingFirstName, |
213
|
|
|
string $shippingLastName, |
214
|
|
|
string $shippingAddress, |
215
|
|
|
string $shippingPostal, |
216
|
|
|
string $shippingCity, |
217
|
|
|
string $shippingCountry |
218
|
|
|
): CustomerInfoPayload { |
219
|
|
|
$payload = new CustomerInfoPayload(); |
220
|
|
|
$payload |
221
|
|
|
->setBillingFirstName($billingFirstName) |
222
|
|
|
->setBillingLastName($billingLastName) |
223
|
|
|
->setBillingAddress($billingAddress) |
224
|
|
|
->setBillingPostal($billingPostal) |
225
|
|
|
->setBillingCity($billingCity) |
226
|
|
|
->setBillingCountry($billingCountry) |
227
|
|
|
->setShippingFirstName($shippingFirstName) |
228
|
|
|
->setShippingLastName($shippingLastName) |
229
|
|
|
->setShippingAddress($shippingAddress) |
230
|
|
|
->setShippingPostal($shippingPostal) |
231
|
|
|
->setShippingCity($shippingCity) |
232
|
|
|
->setShippingCountry($shippingCountry) |
233
|
|
|
; |
234
|
|
|
|
235
|
|
|
return $payload; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param string $callbackForm |
240
|
|
|
* @param string $callbackOk |
241
|
|
|
* @param string $callbackFail |
242
|
|
|
* @param string $callbackRedirect |
243
|
|
|
* @param string $callbackOpen |
244
|
|
|
* @param string $callbackNotification |
245
|
|
|
* |
246
|
|
|
* @return ConfigPayload |
247
|
|
|
*/ |
248
|
|
|
protected function createConfig( |
249
|
|
|
string $callbackForm, |
250
|
|
|
string $callbackOk, |
251
|
|
|
string $callbackFail, |
252
|
|
|
string $callbackRedirect, |
253
|
|
|
string $callbackOpen, |
254
|
|
|
string $callbackNotification |
255
|
|
|
): ConfigPayload { |
256
|
|
|
$payload = new ConfigPayload(); |
257
|
|
|
$payload |
258
|
|
|
->setCallbackForm($callbackForm) |
259
|
|
|
->setCallbackOk($callbackOk) |
260
|
|
|
->setCallbackFail($callbackFail) |
261
|
|
|
->setCallbackRedirect($callbackRedirect) |
262
|
|
|
->setCallbackOpen($callbackOpen) |
263
|
|
|
->setCallbackNotification($callbackNotification) |
264
|
|
|
; |
265
|
|
|
|
266
|
|
|
return $payload; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.