1
|
|
|
<?php |
2
|
|
|
use Shopware\Bundle\AttributeBundle\Service\DataLoader; |
3
|
|
|
use Shopware\Models\Order\Order; |
4
|
|
|
use Shopware\Components\CSRFWhitelistAware; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Klarna payment controller |
8
|
|
|
*/ |
9
|
|
|
class Shopware_Controllers_Backend_PaymentKlarna extends Enlight_Controller_Action implements CSRFWhitelistAware |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Shopware_Plugins_Frontend_SwagPaymentKlarna_Bootstrap |
13
|
|
|
*/ |
14
|
|
|
private $plugin; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Enlight_Config |
18
|
|
|
*/ |
19
|
|
|
private $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Shopware\Components\Model\ModelManager |
23
|
|
|
*/ |
24
|
|
|
private $modelManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Shopware\Models\Order\Repository |
28
|
|
|
*/ |
29
|
|
|
private $orderRepository; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function preDispatch() |
35
|
|
|
{ |
36
|
|
|
$this->plugin = $this->get('plugins')->Frontend()->SwagPaymentKlarna(); |
37
|
|
|
$this->config = $this->plugin->Config(); |
38
|
|
|
$this->modelManager = $this->get('models'); |
39
|
|
|
$this->orderRepository = $this->modelManager->getRepository('Shopware\Models\Order\Order'); |
40
|
|
|
|
41
|
|
|
$this->Front()->Plugins()->Json()->setRenderer(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function get($name) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
if (version_compare(Shopware::VERSION, '4.2.0', '<') && Shopware::VERSION != '___VERSION___') { |
50
|
|
|
if ($name == 'pluginlogger') { |
51
|
|
|
$name = 'log'; |
52
|
|
|
} |
53
|
|
|
$name = ucfirst($name); |
54
|
|
|
return Shopware()->Bootstrap()->getResource($name); |
55
|
|
|
} |
56
|
|
|
return parent::get($name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return \Doctrine\ORM\Query |
61
|
|
|
*/ |
62
|
|
|
protected function getOrderByIdQuery() |
63
|
|
|
{ |
64
|
|
|
$builder = $this->orderRepository->createQueryBuilder('o'); |
65
|
|
|
$builder->addSelect(array( |
66
|
|
|
'details', |
67
|
|
|
'customer', |
68
|
|
|
'documents', |
69
|
|
|
'attribute', |
70
|
|
|
'detailsAttribute' |
71
|
|
|
)); |
72
|
|
|
$builder->leftJoin('o.details', 'details') |
73
|
|
|
->leftJoin('details.attribute', 'detailsAttribute') |
74
|
|
|
->leftJoin('o.customer', 'customer') |
75
|
|
|
->leftJoin('o.documents', 'documents') |
76
|
|
|
->leftJoin('o.attribute', 'attribute'); |
77
|
|
|
$builder->where('o.id = :orderId'); |
78
|
|
|
$query = $builder->getQuery(); |
79
|
|
|
return $query; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $orderId |
84
|
|
|
* @return Shopware\Models\Order\Order |
85
|
|
|
*/ |
86
|
|
|
protected function getOrderById($orderId) |
87
|
|
|
{ |
88
|
|
|
$query = $this->getOrderByIdQuery(); |
89
|
|
|
$query->setParameter('orderId', $orderId); |
90
|
|
|
return $query->getOneOrNullResult(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function registerShopByOrderId($orderId) |
94
|
|
|
{ |
95
|
|
|
$repository = $this->modelManager->getRepository('Shopware\Models\Shop\Shop'); |
96
|
|
|
|
97
|
|
|
$order = $this->orderRepository->find($orderId); |
98
|
|
|
if ($order === null) { |
99
|
|
|
throw new \Exception("Order {$orderId} not found"); |
100
|
|
|
} |
101
|
|
|
$shopId = $order->getLanguageIso(); |
102
|
|
|
if ($shopId === null) { |
103
|
|
|
$shop = $repository->getActiveDefault(); |
104
|
|
|
} else { |
105
|
|
|
$shop = $repository->getActiveById($shopId); |
106
|
|
|
} |
107
|
|
|
if ($shop === null) { |
108
|
|
|
throw new \Exception("Shop {$shopId} not found"); |
109
|
|
|
} |
110
|
|
|
$shop->registerResources(Shopware()->Bootstrap()); |
111
|
|
|
$this->modelManager->clear(); |
112
|
|
|
return $shop; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function activateAction() |
116
|
|
|
{ |
117
|
|
|
$orderId = $this->Request()->getParam('orderId'); |
118
|
|
|
$positions = $this->Request()->getParam('positions', array()); |
119
|
|
|
$this->registerShopByOrderId($orderId); |
120
|
|
|
$order = $this->getOrderById($orderId); |
121
|
|
|
$service = $this->getService($order); |
122
|
|
|
$detailStatus = $this->modelManager->find('\Shopware\Models\Order\DetailStatus', 3); |
123
|
|
|
$paymentStatus = $this->plugin->Config()->get('activateStatusId'); |
124
|
|
View Code Duplication |
if (!empty($paymentStatus)) { |
|
|
|
|
125
|
|
|
$paymentStatus = $this->modelManager->find('\Shopware\Models\Order\Status', $paymentStatus); |
126
|
|
|
} else { |
127
|
|
|
$paymentStatus = null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** @var Shopware\Models\Order\Detail $position */ |
131
|
|
|
foreach ($order->getDetails() as $position) { |
132
|
|
|
if (empty($positions)) { |
133
|
|
|
$selection = $position->getQuantity() - $position->getShipped(); |
134
|
|
|
if (!empty($selection)) { |
135
|
|
|
// $service->addArtNo($selection, $position->getArticleNumber()); |
|
|
|
|
136
|
|
|
$position->setShipped($position->getShipped() + $selection); |
137
|
|
|
$position->setStatus($detailStatus); |
138
|
|
|
} |
139
|
|
|
} elseif (!empty($positions[$position->getId()])) { |
140
|
|
|
$quantity = $position->getQuantity() - $position->getShipped(); |
141
|
|
|
$selection = min($positions[$position->getId()], $quantity); |
142
|
|
|
if (!empty($selection)) { |
143
|
|
|
$service->addArtNo($selection, $position->getArticleNumber()); |
144
|
|
|
$position->setShipped($position->getShipped() + $selection); |
145
|
|
|
if ($selection == $quantity) { |
146
|
|
|
$position->setStatus($detailStatus); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
try { |
153
|
|
|
$result = $service->activate($order->getTransactionId()); |
154
|
|
|
if (!empty($result[1])) { |
155
|
|
|
$last = true; |
156
|
|
|
foreach ($order->getDetails() as $position) { |
157
|
|
|
if ((empty($positions) || !empty($positions[$position->getId()]))) { |
158
|
|
|
if ($position->getAttribute()->getSwagKlarnaInvoiceNumber() === null) { |
159
|
|
|
$position->getAttribute()->setSwagKlarnaInvoiceNumber( |
160
|
|
|
$result[1] |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
} elseif ($position->getStatus()->getId() == 0) { |
164
|
|
|
$last = false; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$order->getAttribute()->setSwagKlarnaInvoiceNumber( |
169
|
|
|
$result[1] |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
if (empty($positions) || $last) { |
173
|
|
|
$order->setClearedDate(new DateTime()); |
174
|
|
|
$order->getAttribute()->setSwagKlarnaStatus( |
175
|
|
|
'activate' |
176
|
|
|
); |
177
|
|
|
if ($paymentStatus !== null) { |
178
|
|
|
$order->setPaymentStatus($paymentStatus); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
$this->modelManager->flush(); |
182
|
|
|
$this->View()->assign(array( |
183
|
|
|
'success' => true, |
184
|
|
|
'number' => $result[1] |
185
|
|
|
)); |
186
|
|
|
} else { |
187
|
|
|
$this->View()->assign(array( |
188
|
|
|
'success' => false, |
189
|
|
|
'status' => $result[0] |
190
|
|
|
)); |
191
|
|
|
} |
192
|
|
|
} catch (Exception $e) { |
193
|
|
|
$this->View()->assign(array( |
194
|
|
|
'success' => false, |
195
|
|
|
'message' => $e->getMessage() |
196
|
|
|
)); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function creditAction() |
201
|
|
|
{ |
202
|
|
|
$orderId = $this->Request()->getParam('orderId'); |
203
|
|
|
$this->registerShopByOrderId($orderId); |
204
|
|
|
$order = $this->getOrderById($orderId); |
205
|
|
|
|
206
|
|
|
$service = $this->getService($order); |
207
|
|
|
$status = $this->modelManager->find('\Shopware\Models\Order\DetailStatus', 2); |
208
|
|
|
|
209
|
|
|
try { |
210
|
|
|
$invoices = array( |
211
|
|
|
$order->getAttribute()->getSwagKlarnaInvoiceNumber() |
212
|
|
|
); |
213
|
|
|
/** @var Shopware\Models\Order\Detail $position */ |
214
|
|
|
foreach ($order->getDetails() as $position) { |
215
|
|
|
if ($position->getAttribute()->getSwagKlarnaInvoiceNumber() !== null) { |
216
|
|
|
$invoices[] = $position->getAttribute()->getSwagKlarnaInvoiceNumber(); |
217
|
|
|
} |
218
|
|
|
$position->setStatus($status); |
219
|
|
|
} |
220
|
|
|
$result = array(); |
221
|
|
|
foreach (array_unique($invoices) as $invoice) { |
222
|
|
|
$result[] = $service->creditInvoice($invoice); |
223
|
|
|
} |
224
|
|
|
$order->getAttribute()->setSwagKlarnaStatus( |
225
|
|
|
'credit' |
226
|
|
|
); |
227
|
|
|
$this->modelManager->flush(); |
228
|
|
|
$this->View()->assign(array( |
229
|
|
|
'success' => !empty($result), |
230
|
|
|
'number' => $result |
231
|
|
|
)); |
232
|
|
|
} catch (Exception $e) { |
233
|
|
|
$this->View()->assign(array( |
234
|
|
|
'success' => false, |
235
|
|
|
'message' => $e->getMessage() |
236
|
|
|
)); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function cancelAction() |
241
|
|
|
{ |
242
|
|
|
$orderId = $this->Request()->getParam('orderId'); |
243
|
|
|
$this->registerShopByOrderId($orderId); |
244
|
|
|
$order = $this->getOrderById($orderId); |
245
|
|
|
$service = $this->getService($order); |
246
|
|
|
$detailStatus = $this->modelManager->find('\Shopware\Models\Order\DetailStatus', 2); |
247
|
|
|
$paymentStatus = $this->plugin->Config()->get('cancelStatusId'); |
248
|
|
View Code Duplication |
if (!empty($paymentStatus)) { |
|
|
|
|
249
|
|
|
$paymentStatus = $this->modelManager->find('\Shopware\Models\Order\Status', $paymentStatus); |
250
|
|
|
} else { |
251
|
|
|
$paymentStatus = null; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
try { |
255
|
|
|
/** @var Shopware\Models\Order\Detail $position */ |
256
|
|
|
foreach ($order->getDetails() as $position) { |
257
|
|
|
if (!$position->getStatus()->getId()) { |
258
|
|
|
$position->setStatus($detailStatus); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
$result = $service->cancelReservation( |
262
|
|
|
$order->getTransactionId() |
263
|
|
|
); |
264
|
|
|
if ($paymentStatus !== null) { |
265
|
|
|
$order->setPaymentStatus($paymentStatus); |
266
|
|
|
} |
267
|
|
|
$order->getAttribute()->setSwagKlarnaStatus( |
268
|
|
|
'cancel' |
269
|
|
|
); |
270
|
|
|
$this->modelManager->flush(); |
271
|
|
|
$this->View()->assign(array( |
272
|
|
|
'success' => $result |
273
|
|
|
)); |
274
|
|
|
} catch (Exception $e) { |
275
|
|
|
$this->View()->assign(array( |
276
|
|
|
'success' => false, |
277
|
|
|
'message' => $e->getMessage() |
278
|
|
|
)); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function refundAction() |
283
|
|
|
{ |
284
|
|
|
$orderId = $this->Request()->getParam('orderId'); |
285
|
|
|
$this->registerShopByOrderId($orderId); |
286
|
|
|
$order = $this->getOrderById($orderId); |
287
|
|
|
$service = $this->getService($order); |
288
|
|
|
$status = $this->modelManager->find('\Shopware\Models\Order\DetailStatus', 2); |
289
|
|
|
$positions = $this->Request()->getParam('positions', array()); |
290
|
|
|
|
291
|
|
|
try { |
292
|
|
|
$invoices = array(); |
293
|
|
|
/** @var Shopware\Models\Order\Detail $position */ |
294
|
|
|
foreach ($order->getDetails() as $position) { |
295
|
|
|
if (!empty($positions[$position->getId()])) { |
296
|
|
|
$invoiceNumber = $position->getAttribute()->getSwagKlarnaInvoiceNumber(); |
297
|
|
|
$selection = min($positions[$position->getId()], $position->getShipped()); |
298
|
|
|
if ($invoiceNumber !== null && !empty($selection)) { |
299
|
|
|
$invoices[$invoiceNumber][$position->getArticleNumber()] = $selection; |
300
|
|
|
$position->setStatus($status); |
301
|
|
|
$position->setShipped($position->getShipped() - $selection); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
$result = array(); |
306
|
|
|
foreach ($invoices as $invoiceNumber => $articles) { |
307
|
|
|
foreach ($articles as $articleNumber => $quantity) { |
308
|
|
|
$service->addArtNo($quantity, $articleNumber); |
309
|
|
|
} |
310
|
|
|
$result[] = $service->creditPart($invoiceNumber); |
311
|
|
|
} |
312
|
|
|
$this->modelManager->flush(); |
313
|
|
|
$this->View()->assign(array( |
314
|
|
|
'success' => true, |
315
|
|
|
'number' => $result |
316
|
|
|
)); |
317
|
|
|
} catch (Exception $e) { |
318
|
|
|
$this->View()->assign(array( |
319
|
|
|
'success' => false, |
320
|
|
|
'message' => $e->getMessage() |
321
|
|
|
)); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function updateAction() |
326
|
|
|
{ |
327
|
|
|
$orderId = $this->Request()->getParam('orderId'); |
328
|
|
|
$this->registerShopByOrderId($orderId); |
329
|
|
|
$order = $this->getOrderById($orderId); |
330
|
|
|
|
331
|
|
|
$service = $this->getService($order); |
332
|
|
|
$status = $this->modelManager->find('\Shopware\Models\Order\DetailStatus', 0); |
333
|
|
|
$positions = $this->Request()->getParam('positions', array()); |
334
|
|
|
if (is_string($positions)) { |
335
|
|
|
$positions = json_decode($positions, true); |
336
|
|
|
} |
337
|
|
|
$newPositions = array(); |
338
|
|
|
|
339
|
|
|
try { |
340
|
|
|
if ($order->getInvoiceShipping() > 0) { |
341
|
|
|
$taxRate = round(($order->getInvoiceShipping() - $order->getInvoiceShippingNet()) / $order->getInvoiceShippingNet() * 100, 2); |
342
|
|
|
$service->addArticle( |
343
|
|
|
1, |
344
|
|
|
'SHIPPING', |
345
|
|
|
'Shipping Fee', |
346
|
|
|
$order->getInvoiceShipping(), |
347
|
|
|
$taxRate, |
348
|
|
|
0, |
349
|
|
|
KlarnaFlags::INC_VAT | KlarnaFlags::IS_SHIPMENT |
350
|
|
|
); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
if (!empty($positions)) { |
354
|
|
|
foreach ($positions as $positionData) { |
355
|
|
|
if (empty($positionData['price'])) { |
356
|
|
|
$positionData = $this->getPriceData($positionData); |
357
|
|
|
} |
358
|
|
|
$position = null; |
359
|
|
|
if (!empty($positionData['id'])) { |
360
|
|
|
/** @var \Shopware\Models\Order\Detail $testPosition */ |
361
|
|
|
foreach ($order->getDetails() as $testPosition) { |
362
|
|
|
if ($testPosition->getId() == $positionData['id']) { |
363
|
|
|
$position = $testPosition; |
364
|
|
|
break; |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
if ($position === null) { |
369
|
|
|
$position = new Shopware\Models\Order\Detail(); |
370
|
|
|
$position->fromArray(array( |
371
|
|
|
'order' => $order, |
372
|
|
|
'number' => $order->getNumber(), |
373
|
|
|
'status' => $status, |
374
|
|
|
'attribute' => array('attribute1' => '') |
375
|
|
|
)); |
376
|
|
|
} |
377
|
|
|
if (!empty($positionData['taxId']) |
378
|
|
|
&& ($position->getTax() === null || $position->getTax()->getId() != $positionData['taxId']) |
379
|
|
|
) { |
380
|
|
|
/** @var \Shopware\Models\Tax\Tax $tax */ |
381
|
|
|
$tax = $this->modelManager->find('\Shopware\Models\Tax\Tax', $positionData['taxId']); |
382
|
|
|
$position->setTax($tax); |
383
|
|
|
$positionData['taxRate'] = $tax->getTax(); |
384
|
|
|
} |
385
|
|
|
$position->fromArray(array( |
386
|
|
|
'taxRate' => $positionData['taxRate'], |
387
|
|
|
'articleId' => $positionData['articleId'], |
388
|
|
|
'articleNumber' => $positionData['articleNumber'], |
389
|
|
|
'articleName' => $positionData['articleName'], |
390
|
|
|
'price' => $positionData['price'], |
391
|
|
|
'quantity' => $positionData['quantity'] |
392
|
|
|
)); |
393
|
|
|
$newPositions[] = $position; |
394
|
|
|
} |
395
|
|
|
$order->setDetails($newPositions); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** @var Shopware\Models\Order\Detail $position */ |
399
|
|
|
foreach ($order->getDetails() as $position) { |
400
|
|
|
$flags = KlarnaFlags::INC_VAT; |
401
|
|
|
if ($position->getMode() != 0 && $position->getPrice() > 0) { |
402
|
|
|
$flags |= KlarnaFlags::IS_HANDLING; |
403
|
|
|
} elseif ($position->getArticleNumber() == 'SHIPPING') { |
404
|
|
|
$flags |= KlarnaFlags::IS_SHIPMENT; |
405
|
|
|
} |
406
|
|
|
$articleName = utf8_decode($position->getArticleName()); |
407
|
|
|
|
408
|
|
|
$service->addArticle( |
409
|
|
|
$position->getQuantity(), |
410
|
|
|
$position->getArticleNumber(), |
411
|
|
|
$articleName, |
412
|
|
|
$position->getPrice(), |
413
|
|
|
$position->getTaxRate(), |
414
|
|
|
0, |
415
|
|
|
$flags |
416
|
|
|
); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
$service->update($order->getTransactionId()); |
420
|
|
|
$this->modelManager->flush(); |
421
|
|
|
$this->View()->assign(array( |
422
|
|
|
'success' => true |
423
|
|
|
)); |
424
|
|
|
} catch (Exception $e) { |
425
|
|
|
$this->View()->assign(array( |
426
|
|
|
'success' => false, |
427
|
|
|
'message' => $e->getMessage() |
428
|
|
|
)); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
public function discountAction() |
433
|
|
|
{ |
434
|
|
|
$orderId = $this->Request()->getParam('orderId'); |
435
|
|
|
$this->registerShopByOrderId($orderId); |
436
|
|
|
$order = $this->getOrderById($orderId); |
437
|
|
|
$value = (float)str_replace(',', '.', $this->Request()->getParam('value')); |
438
|
|
|
$service = $this->getService($order); |
439
|
|
|
|
440
|
|
|
$invoiceNumber = $order->getAttribute()->getSwagKlarnaInvoiceNumber(); |
441
|
|
|
$status = $this->modelManager->find('\Shopware\Models\Order\DetailStatus', 3); |
442
|
|
|
$tax = null; |
443
|
|
|
/** @var Shopware\Models\Order\Detail $detail */ |
444
|
|
|
foreach ($order->getDetails() as $detail) { |
445
|
|
|
if ($detail->getTax() !== null && $detail->getTax()->getId() != 0) { |
446
|
|
|
$tax = $detail->getTax(); |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
try { |
451
|
|
|
$result = $service->returnAmount( |
452
|
|
|
$invoiceNumber, |
453
|
|
|
$value, |
454
|
|
|
$detail->getTaxRate(), |
|
|
|
|
455
|
|
|
KlarnaFlags::INC_VAT, |
456
|
|
|
'discount' |
457
|
|
|
); |
458
|
|
|
|
459
|
|
|
$value = $value > 0 ? $value * -1 : $value; |
460
|
|
|
$detail = new Shopware\Models\Order\Detail(); |
461
|
|
|
$detail->fromArray(array( |
462
|
|
|
'order' => $order, |
463
|
|
|
'number' => $order->getNumber(), |
464
|
|
|
'tax' => $tax, |
465
|
|
|
'status' => $status, |
466
|
|
|
'taxRate' => $tax !== null ? $tax->getTax() : 0, |
467
|
|
|
'articleId' => 0, |
468
|
|
|
'articleNumber' => 'DISCOUNT', |
469
|
|
|
'articleName' => 'Discount', |
470
|
|
|
'price' => $value, |
471
|
|
|
'quantity' => 1, |
472
|
|
|
'attribute' => array( |
473
|
|
|
'swagKlarnaInvoiceNumber' => $result |
474
|
|
|
) |
475
|
|
|
)); |
476
|
|
|
$order->getDetails()->add($detail); |
477
|
|
|
$this->modelManager->flush(); |
478
|
|
|
$this->View()->assign(array( |
479
|
|
|
'success' => true, |
480
|
|
|
'number' => $result |
481
|
|
|
)); |
482
|
|
|
} catch (Exception $e) { |
483
|
|
|
$this->View()->assign(array( |
484
|
|
|
'success' => false, |
485
|
|
|
'message' => $e->getMessage() |
486
|
|
|
)); |
487
|
|
|
} |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
public function autoUpdateAction() |
491
|
|
|
{ |
492
|
|
|
$orderId = $this->Request()->getParam('orderId'); |
493
|
|
|
$this->registerShopByOrderId($orderId); |
494
|
|
|
$order = $this->getOrderById($orderId); |
495
|
|
|
|
496
|
|
|
if ($order === null) { |
497
|
|
|
return; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
if ($order->getInvoiceShipping() > 0 && $this->config->get('convertShippingFee')) { |
501
|
|
|
$status = $this->modelManager->find('\Shopware\Models\Order\DetailStatus', 0); |
502
|
|
|
$taxRate = round(($order->getInvoiceShipping() - $order->getInvoiceShippingNet()) / $order->getInvoiceShippingNet() * 100, 2); |
503
|
|
|
|
504
|
|
|
$detail = new Shopware\Models\Order\Detail(); |
505
|
|
|
$detail->fromArray(array( |
506
|
|
|
'order' => $order, |
507
|
|
|
'number' => $order->getNumber(), |
508
|
|
|
'status' => $status, |
509
|
|
|
'taxRate' => $taxRate, |
510
|
|
|
'articleId' => 0, |
511
|
|
|
'articleNumber' => 'SHIPPING', |
512
|
|
|
'articleName' => 'Shipping Fee', |
513
|
|
|
'price' => $order->getInvoiceShipping(), |
514
|
|
|
'quantity' => 1, |
515
|
|
|
'attribute' => array('attribute1' => '') |
516
|
|
|
)); |
517
|
|
|
$order->getDetails()->add($detail); |
518
|
|
|
$order->setInvoiceShipping(0); |
519
|
|
|
$order->setInvoiceShippingNet(0); |
520
|
|
|
$this->modelManager->flush(); |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
$this->View()->assign(array( |
524
|
|
|
'success' => true, |
525
|
|
|
)); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
public function getInvoiceSecretAction() |
529
|
|
|
{ |
530
|
|
|
$orderId = $this->Request()->getParam('orderId'); |
531
|
|
|
$invoiceNo = $this->Request()->getParam('invoiceNo'); |
532
|
|
|
|
533
|
|
|
if (empty($invoiceNo)) { |
534
|
|
|
/** @var DataLoader $dataLoader */ |
535
|
|
|
$dataLoader = $this->get('shopware_attribute.data_loader'); |
536
|
|
|
|
537
|
|
|
try { |
538
|
|
|
$data = $dataLoader->load('s_order_details_attributes', $orderId); |
539
|
|
|
} catch (Exception $e) { |
540
|
|
|
$this->View()->assign(['success' => false, 'message' => $e->getMessage()]); |
541
|
|
|
return; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
if (empty($data)) { |
545
|
|
|
$this->View()->assign(['success' => false, 'message' => 'Dataset was empty!']); |
546
|
|
|
return; |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
$invoiceNo = $data['swag_klarna_invoice_number']; |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
$eid = $this->config->get('merchantId'); |
553
|
|
|
$sharedSecret = $this->config->get('sharedSecret'); |
554
|
|
|
$testDrive = $this->config->get('testDrive'); |
555
|
|
|
|
556
|
|
|
$secretParts = [$eid, $invoiceNo, $sharedSecret]; |
557
|
|
|
$secret = urlencode( |
558
|
|
|
base64_encode( |
559
|
|
|
hash('sha512', implode(':', $secretParts), true) |
560
|
|
|
) |
561
|
|
|
); |
562
|
|
|
|
563
|
|
|
$this->View()->assign([ |
564
|
|
|
'success' => true, |
565
|
|
|
'invoiceNo' => $invoiceNo, |
566
|
|
|
'secret' => $secret, |
567
|
|
|
'testDrive' => $testDrive |
568
|
|
|
]); |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Helper method to get the default price for an article |
573
|
|
|
* @param $positionData |
574
|
|
|
*/ |
575
|
|
|
private function getPriceData($positionData) |
576
|
|
|
{ |
577
|
|
|
/** @var Shopware\Models\Article\Detail $articleDetail */ |
578
|
|
|
$articleDetail = Shopware()->Models()->getRepository('Shopware\Models\Article\Detail')->findOneBy(array( |
579
|
|
|
'number' => $positionData['articleNumber'] |
580
|
|
|
)); |
581
|
|
|
$article = $articleDetail->getArticle(); |
582
|
|
|
$tax = intval($article->getTax()->getTax()); |
583
|
|
|
$price = Shopware()->Models()->getRepository('Shopware\Models\Article\Price')->findOneBy(array( |
584
|
|
|
'articleDetailsId' => $articleDetail->getId(), |
585
|
|
|
'from' => 1 |
586
|
|
|
))->getPrice(); |
587
|
|
|
|
588
|
|
|
$price = round($price * (100 + $tax) / 100, 3); |
589
|
|
|
|
590
|
|
|
$positionData['taxRate'] = $tax; |
591
|
|
|
$positionData['price'] = $price; |
592
|
|
|
|
593
|
|
|
return $positionData; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* Helper method to get the proper service with the correct EID |
598
|
|
|
* |
599
|
|
|
* @param Order $order |
600
|
|
|
* @return Klarna |
601
|
|
|
*/ |
602
|
|
|
private function getService($order) |
603
|
|
|
{ |
604
|
|
|
$plugin = $this->plugin; |
605
|
|
|
|
606
|
|
|
if ($order->getPayment()->getName() !== 'klarna_checkout' |
607
|
|
|
&& $this->plugin->isKpmActive()) { |
608
|
|
|
$plugin = Shopware()->Plugins()->Frontend()->SwagPaymentKlarnaKpm(); |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
if (!method_exists($plugin, 'getService')) { |
612
|
|
|
//Older KPM version |
613
|
|
|
return $plugin->getKlarnaServiceByConfig($plugin->Config()); |
614
|
|
|
} |
615
|
|
|
return $plugin->getService(); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
|
619
|
|
|
public function getWhitelistedCSRFActions() |
620
|
|
|
{ |
621
|
|
|
$csrfActions = array( |
622
|
|
|
'activate', |
623
|
|
|
'autoUpdate', |
624
|
|
|
'cancel', |
625
|
|
|
'credit', |
626
|
|
|
'discount', |
627
|
|
|
'refund', |
628
|
|
|
'update' |
629
|
|
|
); |
630
|
|
|
|
631
|
|
|
return $csrfActions; |
632
|
|
|
} |
633
|
|
|
} |
634
|
|
|
|
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.