16
16
use Mollie \Provider \CreditCardLogoProvider ;
17
17
use Mollie \Provider \TaxCalculatorProvider ;
18
18
use Mollie \Repository \PaymentMethodRepository ;
19
+ use Mollie \Repository \PaymentMethodRepositoryInterface ;
20
+ use Mollie \Service \ApiService ;
21
+ use Mollie \Service \CancelService ;
22
+ use Mollie \Service \CaptureService ;
19
23
use Mollie \Service \MolliePaymentMailService ;
24
+ use Mollie \Service \RefundService ;
25
+ use Mollie \Service \ShipService ;
20
26
use Mollie \Utility \NumberUtility ;
21
27
use Mollie \Utility \TimeUtility ;
28
+ use Mollie \Utility \TransactionUtility ;
22
29
23
30
if (!defined ('_PS_VERSION_ ' )) {
24
31
exit ;
@@ -31,6 +38,10 @@ class AdminMollieAjaxController extends ModuleAdminController
31
38
32
39
public function postProcess ()
33
40
{
41
+ if (!Tools::isSubmit ('ajax ' )) {
42
+ return ;
43
+ }
44
+
34
45
$ action = Tools::getValue ('action ' );
35
46
36
47
$ this ->context ->smarty ->assign ('bootstrap ' , true );
@@ -54,6 +65,25 @@ public function postProcess()
54
65
case 'updateFixedPaymentFeePrice ' :
55
66
$ this ->updateFixedPaymentFeePrice ();
56
67
break ;
68
+ case 'refundAll ' :
69
+ case 'refund ' :
70
+ $ this ->processRefund ();
71
+ break ;
72
+ case 'shipAll ' :
73
+ case 'ship ' :
74
+ $ this ->processShip ();
75
+ break ;
76
+ case 'captureAll ' :
77
+ case 'capture ' :
78
+ $ this ->processCapture ();
79
+ break ;
80
+ case 'cancelAll ' :
81
+ case 'cancel ' :
82
+ $ this ->processCancel ();
83
+ break ;
84
+ case 'retrieve ' :
85
+ $ this ->retrieveOrderInfo ();
86
+ break ;
57
87
default :
58
88
break ;
59
89
}
@@ -213,4 +243,202 @@ private function updateFixedPaymentFeePrice(): void
213
243
])
214
244
);
215
245
}
246
+
247
+ private function processRefund (): void
248
+ {
249
+ try {
250
+ $ transactionId = Tools::getValue ('transactionId ' );
251
+ $ refundAmount = (float ) Tools::getValue ('refundAmount ' ) ?: null ;
252
+ $ orderLineId = Tools::getValue ('orderline ' ) ?: null ;
253
+
254
+ /** @var RefundService $refundService */
255
+ $ refundService = $ this ->module ->getService (RefundService::class);
256
+
257
+ $ status = $ refundService ->handleRefund ($ transactionId , $ refundAmount , $ orderLineId );
258
+
259
+ $ this ->ajaxRender (json_encode ($ status ));
260
+ } catch (\Throwable $ e ) {
261
+ $ this ->ajaxRender (
262
+ json_encode ([
263
+ 'success ' => false ,
264
+ 'message ' => $ this ->module ->l ('An error occurred while processing the request. ' ),
265
+ 'error ' => $ e ->getMessage (),
266
+ ])
267
+ );
268
+ }
269
+ }
270
+
271
+ private function processShip (): void
272
+ {
273
+ $ orderId = (int ) Tools::getValue ('orderId ' );
274
+ $ orderLines = Tools::getValue ('orderLines ' ) ?: [];
275
+ $ tracking = Tools::getValue ('tracking ' );
276
+ $ orderlineId = Tools::getValue ('orderline ' );
277
+
278
+ try {
279
+ $ order = new Order ($ orderId );
280
+
281
+ /** @var PaymentMethodRepositoryInterface $paymentMethodRepo */
282
+ $ paymentMethodRepo = $ this ->module ->getService (PaymentMethodRepositoryInterface::class);
283
+ $ transactionId = $ paymentMethodRepo ->getPaymentBy ('order_id ' , (string ) $ orderId )['transaction_id ' ];
284
+
285
+ /** @var ShipService $shipService */
286
+ $ shipService = $ this ->module ->getService (ShipService::class);
287
+ $ status = $ shipService ->handleShip ($ transactionId , $ orderlineId , $ tracking );
288
+
289
+ $ this ->ajaxRender (json_encode ($ status ));
290
+ } catch (\Throwable $ e ) {
291
+ $ this ->ajaxRender (
292
+ json_encode ([
293
+ 'success ' => false ,
294
+ 'message ' => $ this ->module ->l ('An error occurred while processing the request. ' ),
295
+ 'error ' => $ e ->getMessage (),
296
+ ])
297
+ );
298
+ }
299
+ }
300
+
301
+ private function processCapture (): void
302
+ {
303
+ $ orderId = (int ) Tools::getValue ('orderId ' );
304
+ $ captureAmount = Tools::getValue ('captureAmount ' ) ?: null ;
305
+
306
+ try {
307
+ $ order = new Order ($ orderId );
308
+
309
+ /** @var PaymentMethodRepositoryInterface $paymentMethodRepo */
310
+ $ paymentMethodRepo = $ this ->module ->getService (PaymentMethodRepositoryInterface::class);
311
+ $ transactionId = $ paymentMethodRepo ->getPaymentBy ('order_id ' , (string ) $ orderId )['transaction_id ' ];
312
+
313
+ /** @var CaptureService $captureService */
314
+ $ captureService = $ this ->module ->getService (CaptureService::class);
315
+
316
+ // Use provided amount for individual product capture, or full order amount for capture all
317
+ $ amount = $ captureAmount ? (float ) $ captureAmount : $ order ->total_paid ;
318
+ $ status = $ captureService ->handleCapture ($ transactionId , $ amount );
319
+
320
+ $ this ->ajaxRender (json_encode ($ status ));
321
+ } catch (\Throwable $ e ) {
322
+ $ this ->ajaxRender (
323
+ json_encode ([
324
+ 'success ' => false ,
325
+ 'message ' => $ this ->module ->l ('An error occurred while processing the request. ' ),
326
+ 'error ' => $ e ->getMessage (),
327
+ ])
328
+ );
329
+ }
330
+ }
331
+
332
+ private function processCancel (): void
333
+ {
334
+ $ orderId = (int ) Tools::getValue ('orderId ' );
335
+ $ orderlineId = Tools::getValue ('orderline ' ) ?: null ;
336
+
337
+ try {
338
+ $ order = new Order ($ orderId );
339
+
340
+ /** @var PaymentMethodRepositoryInterface $paymentMethodRepo */
341
+ $ paymentMethodRepo = $ this ->module ->getService (PaymentMethodRepositoryInterface::class);
342
+ $ transactionId = $ paymentMethodRepo ->getPaymentBy ('order_id ' , (string ) $ orderId )['transaction_id ' ];
343
+
344
+ /** @var CancelService $cancelService */
345
+ $ cancelService = $ this ->module ->getService (CancelService::class);
346
+ $ status = $ cancelService ->handleCancel ($ transactionId , $ orderlineId );
347
+
348
+ $ this ->ajaxRender (json_encode ($ status ));
349
+ } catch (\Throwable $ e ) {
350
+ $ this ->ajaxRender (
351
+ json_encode ([
352
+ 'success ' => false ,
353
+ 'message ' => $ this ->module ->l ('An error occurred while processing the request. ' ),
354
+ 'error ' => $ e ->getMessage (),
355
+ ])
356
+ );
357
+ }
358
+ }
359
+
360
+ private function retrieveOrderInfo (): void
361
+ {
362
+ $ orderId = (int ) Tools::getValue ('orderId ' );
363
+
364
+ try {
365
+ $ order = new Order ($ orderId );
366
+
367
+ /** @var PaymentMethodRepositoryInterface $paymentMethodRepo */
368
+ $ paymentMethodRepo = $ this ->module ->getService (PaymentMethodRepositoryInterface::class);
369
+ $ transaction = $ paymentMethodRepo ->getPaymentBy ('order_id ' , (string ) $ orderId );
370
+
371
+ if (!$ transaction ) {
372
+ $ this ->ajaxRender (
373
+ json_encode ([
374
+ 'success ' => false ,
375
+ 'message ' => $ this ->module ->l ('No Mollie transaction found for this order. ' ),
376
+ ])
377
+ );
378
+
379
+ return ;
380
+ }
381
+
382
+ $ transactionId = $ transaction ['transaction_id ' ];
383
+ $ this ->module ->updateApiKey ((int ) $ order ->id_shop );
384
+
385
+ if (!$ this ->module ->getApiClient ()) {
386
+ $ this ->ajaxRender (
387
+ json_encode ([
388
+ 'success ' => false ,
389
+ 'message ' => $ this ->module ->l ('Unable to connect to Mollie API. ' ),
390
+ ])
391
+ );
392
+
393
+ return ;
394
+ }
395
+
396
+ /** @var ApiService $apiService */
397
+ $ apiService = $ this ->module ->getService (ApiService::class);
398
+
399
+ $ mollieApiType = TransactionUtility::isOrderTransaction ($ transactionId ) ? 'orders ' : 'payments ' ;
400
+
401
+ if ($ mollieApiType === 'orders ' ) {
402
+ $ orderInfo = $ this ->module ->getApiClient ()->orders ->get ($ transactionId );
403
+ $ isShipping = $ orderInfo ->status === 'completed ' ;
404
+ $ isCaptured = $ orderInfo ->isPaid ();
405
+ $ isRefunded = $ orderInfo ->amountRefunded ->value > 0 ;
406
+ $ isCanceled = $ orderInfo ->status === 'canceled ' ;
407
+
408
+ $ response = [
409
+ 'success ' => true ,
410
+ 'isShipping ' => $ isShipping ,
411
+ 'isCaptured ' => $ isCaptured ,
412
+ 'isRefunded ' => $ isRefunded ,
413
+ 'isCanceled ' => $ isCanceled ,
414
+ 'orderStatus ' => $ orderInfo ->status ?? null ,
415
+ ];
416
+ } else {
417
+ $ paymentInfo = $ this ->module ->getApiClient ()->payments ->get ($ transactionId );
418
+ $ isShipping = false ;
419
+ $ isCaptured = false ;
420
+
421
+ $ isCaptured = $ paymentInfo ->isPaid ();
422
+ $ isRefunded = $ paymentInfo ->amountRefunded ->value > 0 ;
423
+
424
+ $ response = [
425
+ 'success ' => true ,
426
+ 'isShipping ' => $ isShipping ,
427
+ 'isCaptured ' => $ isCaptured ,
428
+ 'isRefunded ' => $ isRefunded ,
429
+ 'orderStatus ' => $ paymentInfo ->status ?? null ,
430
+ ];
431
+ }
432
+
433
+ $ this ->ajaxRender (json_encode ($ response ));
434
+ } catch (Exception $ e ) {
435
+ $ this ->ajaxRender (
436
+ json_encode ([
437
+ 'success ' => false ,
438
+ 'message ' => $ this ->module ->l ('An error occurred while retrieving order information. ' ),
439
+ 'error ' => $ e ->getMessage (),
440
+ ])
441
+ );
442
+ }
443
+ }
216
444
}
0 commit comments