|
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\Handler; |
|
10
|
|
|
use Loevgaard\Dandomain\Pay\PaymentRequest as DandomainPaymentRequest; |
|
11
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\Payment; |
|
12
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\TerminalInterface; |
|
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
15
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
16
|
|
|
|
|
17
|
|
|
class PaymentRequestPayloadGenerator implements PayloadGeneratorInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ContainerInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $container; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Router |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $router; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var DandomainPaymentRequest |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $paymentRequest; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var TerminalInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $terminal; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Payment |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $payment; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Handler |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $handler; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct( |
|
50
|
|
|
ContainerInterface $container, |
|
51
|
|
|
DandomainPaymentRequest $paymentRequest, |
|
52
|
|
|
TerminalInterface $terminal, |
|
53
|
|
|
Payment $payment, |
|
54
|
|
|
Handler $handler |
|
55
|
|
|
) { |
|
56
|
|
|
$this->container = $container; |
|
57
|
|
|
$this->router = $this->container->get('router'); |
|
58
|
|
|
$this->paymentRequest = $paymentRequest; |
|
59
|
|
|
$this->terminal = $terminal; |
|
60
|
|
|
$this->payment = $payment; |
|
61
|
|
|
$this->handler = $handler; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return PaymentRequestPayload |
|
66
|
|
|
*/ |
|
67
|
|
|
public function generate(): PaymentRequestPayload |
|
68
|
|
|
{ |
|
69
|
|
|
$paymentRequestPayload = new PaymentRequestPayload( |
|
70
|
|
|
$this->terminal->getTitle(), |
|
71
|
|
|
$this->paymentRequest->getOrderId(), |
|
72
|
|
|
$this->paymentRequest->getTotalAmount(), |
|
73
|
|
|
$this->paymentRequest->getCurrencySymbol() |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
foreach ($this->paymentRequest->getPaymentLines() as $paymentLine) { |
|
77
|
|
|
$orderLinePayload = new OrderLinePayload( |
|
78
|
|
|
$paymentLine->getName(), |
|
79
|
|
|
$paymentLine->getProductNumber(), |
|
80
|
|
|
$paymentLine->getQuantity(), |
|
81
|
|
|
$paymentLine->getPrice() |
|
82
|
|
|
); |
|
83
|
|
|
$orderLinePayload->setTaxPercent($paymentLine->getVat()); |
|
84
|
|
|
|
|
85
|
|
|
$paymentRequestPayload->addOrderLine($orderLinePayload); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// add payment fee as an order line if it's set |
|
89
|
|
View Code Duplication |
if ($this->paymentRequest->getPaymentFee()) { |
|
|
|
|
|
|
90
|
|
|
$orderLinePayload = new OrderLinePayload( |
|
91
|
|
|
$this->paymentRequest->getPaymentMethod(), |
|
92
|
|
|
$this->paymentRequest->getPaymentMethod(), |
|
93
|
|
|
1, |
|
94
|
|
|
$this->paymentRequest->getPaymentFee() |
|
95
|
|
|
); |
|
96
|
|
|
$orderLinePayload->setGoodsType(OrderLinePayload::GOODS_TYPE_HANDLING); |
|
97
|
|
|
$paymentRequestPayload->addOrderLine($orderLinePayload); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// add shipping fee as an order line if it's set |
|
101
|
|
View Code Duplication |
if ($this->paymentRequest->getShippingFee()) { |
|
|
|
|
|
|
102
|
|
|
$orderLinePayload = new OrderLinePayload( |
|
103
|
|
|
$this->paymentRequest->getShippingMethod(), |
|
104
|
|
|
$this->paymentRequest->getShippingMethod(), |
|
105
|
|
|
1, |
|
106
|
|
|
$this->paymentRequest->getShippingFee() |
|
107
|
|
|
); |
|
108
|
|
|
$orderLinePayload->setGoodsType(OrderLinePayload::GOODS_TYPE_SHIPMENT); |
|
109
|
|
|
$paymentRequestPayload->addOrderLine($orderLinePayload); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$customerInfoPayload = new CustomerInfoPayload(); |
|
113
|
|
|
$customerNames = explode(' ', $this->paymentRequest->getCustomerName(), 2); |
|
114
|
|
|
$shippingNames = explode(' ', $this->paymentRequest->getDeliveryName(), 2); |
|
115
|
|
|
$customerInfoPayload |
|
116
|
|
|
->setBillingFirstName($customerNames[0] ?? '') |
|
117
|
|
|
->setBillingLastName($customerNames[1] ?? '') |
|
118
|
|
|
->setBillingAddress( |
|
119
|
|
|
$this->paymentRequest->getCustomerAddress(). |
|
120
|
|
|
($this->paymentRequest->getCustomerAddress2() ? "\r\n".$this->paymentRequest->getCustomerAddress2() : '') |
|
121
|
|
|
) |
|
122
|
|
|
->setBillingPostal($this->paymentRequest->getCustomerZipCode()) |
|
123
|
|
|
->setBillingCity($this->paymentRequest->getCustomerCity()) |
|
124
|
|
|
->setBillingCountry($this->paymentRequest->getCustomerCountry()) |
|
125
|
|
|
->setShippingFirstName($shippingNames[0] ?? '') |
|
126
|
|
|
->setShippingLastName($shippingNames[1] ?? '') |
|
127
|
|
|
->setShippingAddress( |
|
128
|
|
|
$this->paymentRequest->getDeliveryAddress(). |
|
129
|
|
|
($this->paymentRequest->getDeliveryAddress2() ? "\r\n".$this->paymentRequest->getDeliveryAddress2() : '') |
|
130
|
|
|
) |
|
131
|
|
|
->setShippingPostal($this->paymentRequest->getDeliveryZipCode()) |
|
132
|
|
|
->setShippingCity($this->paymentRequest->getDeliveryCity()) |
|
133
|
|
|
->setShippingCountry($this->paymentRequest->getDeliveryCountry()) |
|
134
|
|
|
; |
|
135
|
|
|
$paymentRequestPayload->setCustomerInfo($customerInfoPayload); |
|
136
|
|
|
|
|
137
|
|
|
$configPayload = new ConfigPayload(); |
|
138
|
|
|
$configPayload |
|
139
|
|
|
->setCallbackForm( |
|
140
|
|
|
$this->router->generate( |
|
141
|
|
|
'loevgaard_dandomain_altapay_callback_form', |
|
142
|
|
|
[], |
|
143
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
|
144
|
|
|
) |
|
145
|
|
|
) |
|
146
|
|
|
->setCallbackOk( |
|
147
|
|
|
$this->router->generate( |
|
148
|
|
|
'loevgaard_dandomain_altapay_callback_ok', |
|
149
|
|
|
[], |
|
150
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
|
151
|
|
|
) |
|
152
|
|
|
) |
|
153
|
|
|
->setCallbackFail( |
|
154
|
|
|
$this->router->generate( |
|
155
|
|
|
'loevgaard_dandomain_altapay_callback_fail', |
|
156
|
|
|
[], |
|
157
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
|
158
|
|
|
) |
|
159
|
|
|
) |
|
160
|
|
|
->setCallbackRedirect( |
|
161
|
|
|
$this->router->generate( |
|
162
|
|
|
'loevgaard_dandomain_altapay_callback_redirect', |
|
163
|
|
|
[], |
|
164
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
|
165
|
|
|
) |
|
166
|
|
|
) |
|
167
|
|
|
->setCallbackOpen( |
|
168
|
|
|
$this->router->generate( |
|
169
|
|
|
'loevgaard_dandomain_altapay_callback_open', |
|
170
|
|
|
[], |
|
171
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
|
172
|
|
|
) |
|
173
|
|
|
) |
|
174
|
|
|
->setCallbackNotification( |
|
175
|
|
|
$this->router->generate( |
|
176
|
|
|
'loevgaard_dandomain_altapay_callback_notification', |
|
177
|
|
|
[], |
|
178
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
|
179
|
|
|
) |
|
180
|
|
|
) |
|
181
|
|
|
; |
|
182
|
|
|
$paymentRequestPayload->setConfig($configPayload); |
|
183
|
|
|
|
|
184
|
|
|
$paymentRequestPayload |
|
185
|
|
|
->setCookiePart( |
|
186
|
|
|
$this->container->getParameter('loevgaard_dandomain_altapay.cookie_payment_id'), |
|
187
|
|
|
$this->payment->getId() |
|
188
|
|
|
) |
|
189
|
|
|
->setCookiePart( |
|
190
|
|
|
$this->container->getParameter('loevgaard_dandomain_altapay.cookie_checksum_complete'), |
|
191
|
|
|
$this->handler->getChecksum2() |
|
192
|
|
|
) |
|
193
|
|
|
; |
|
194
|
|
|
|
|
195
|
|
|
return $paymentRequestPayload; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
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.