|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file has been created by developers from BitBag. |
|
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
|
6
|
|
|
* You can find more information about us on https://bitbag.io and write us |
|
7
|
|
|
* an email on [email protected]. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace BitBag\SyliusMolliePlugin\Creator; |
|
13
|
|
|
|
|
14
|
|
|
use BitBag\SyliusMolliePlugin\Exceptions\OfflineRefundPaymentMethodNotFound; |
|
15
|
|
|
use BitBag\SyliusMolliePlugin\Refund\Units\PaymentUnitsItemRefundInterface; |
|
16
|
|
|
use BitBag\SyliusMolliePlugin\Refund\Units\ShipmentUnitRefundInterface; |
|
17
|
|
|
use Mollie\Api\Resources\Payment; |
|
18
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
19
|
|
|
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface; |
|
20
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
|
21
|
|
|
use Sylius\RefundPlugin\Command\RefundUnits; |
|
22
|
|
|
use Sylius\RefundPlugin\Provider\RefundPaymentMethodsProviderInterface; |
|
23
|
|
|
use Webmozart\Assert\Assert; |
|
24
|
|
|
|
|
25
|
|
|
final class PaymentRefundCommandCreator implements PaymentRefundCommandCreatorInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var RepositoryInterface */ |
|
28
|
|
|
private $orderRepository; |
|
29
|
|
|
|
|
30
|
|
|
/** @var RepositoryInterface */ |
|
31
|
|
|
private $refundUnitsRepository; |
|
32
|
|
|
|
|
33
|
|
|
/** @var PaymentUnitsItemRefundInterface */ |
|
34
|
|
|
private $itemRefund; |
|
35
|
|
|
|
|
36
|
|
|
/** @var ShipmentUnitRefundInterface */ |
|
37
|
|
|
private $shipmentRefund; |
|
38
|
|
|
|
|
39
|
|
|
/** @var AdjustmentFactoryInterface */ |
|
40
|
|
|
private $adjustmentFactory; |
|
41
|
|
|
|
|
42
|
|
|
/** @var RefundPaymentMethodsProviderInterface */ |
|
43
|
|
|
private $refundPaymentMethodProvider; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct( |
|
46
|
|
|
RepositoryInterface $orderRepository, |
|
47
|
|
|
RepositoryInterface $refundUnitsRepository, |
|
48
|
|
|
PaymentUnitsItemRefundInterface $itemRefund, |
|
49
|
|
|
ShipmentUnitRefundInterface $shipmentRefund, |
|
50
|
|
|
AdjustmentFactoryInterface $adjustmentFactory, |
|
51
|
|
|
RefundPaymentMethodsProviderInterface $refundPaymentMethodProvider |
|
52
|
|
|
) { |
|
53
|
|
|
$this->orderRepository = $orderRepository; |
|
54
|
|
|
$this->refundUnitsRepository = $refundUnitsRepository; |
|
55
|
|
|
$this->itemRefund = $itemRefund; |
|
56
|
|
|
$this->shipmentRefund = $shipmentRefund; |
|
57
|
|
|
$this->adjustmentFactory = $adjustmentFactory; |
|
58
|
|
|
$this->refundPaymentMethodProvider = $refundPaymentMethodProvider; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function fromPayment(Payment $payment): RefundUnits |
|
62
|
|
|
{ |
|
63
|
|
|
$orderId = $payment->metadata->order_id; |
|
64
|
|
|
|
|
65
|
|
|
/** @var OrderInterface $order */ |
|
66
|
|
|
$order = $this->orderRepository->findOneBy(['id' => $orderId]); |
|
67
|
|
|
Assert::notNull($order, sprintf('Cannot find order id with id %s', $orderId)); |
|
68
|
|
|
|
|
69
|
|
|
$allRefunded = $this->refundUnitsRepository->findBy(['orderNumber' => $order->getNumber()]); |
|
70
|
|
|
|
|
71
|
|
|
$refunded = $this->getSumOfAmountExistingRefunds($allRefunded); |
|
72
|
|
|
$mollieRefund = (float) $payment->amountRefunded->value * 100; |
|
73
|
|
|
$toRefund = (int) $mollieRefund - $refunded; |
|
74
|
|
|
|
|
75
|
|
|
$refundMethods = $this->refundPaymentMethodProvider->findForChannel($order->getChannel()); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
if (empty($refundMethods)) { |
|
78
|
|
|
throw new OfflineRefundPaymentMethodNotFound( |
|
79
|
|
|
sprintf('Not found offline payment method on this channel with code :%s', $order->getChannel()->getCode()) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$refundMethod = current($refundMethods); |
|
84
|
|
|
|
|
85
|
|
|
$orderItemUnitRefund = $this->itemRefund->refund($order, $toRefund); |
|
86
|
|
|
$shipmentRefund = $this->shipmentRefund->refund($order, $orderItemUnitRefund, $toRefund); |
|
87
|
|
|
|
|
88
|
|
|
return new RefundUnits($order->getNumber(), $orderItemUnitRefund, $shipmentRefund, $refundMethod->getId(), ''); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function getSumOfAmountExistingRefunds(array $refundedUnits): int |
|
92
|
|
|
{ |
|
93
|
|
|
$sum = 0; |
|
94
|
|
|
|
|
95
|
|
|
if (empty($refundedUnits)) { |
|
96
|
|
|
return $sum; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
foreach ($refundedUnits as $refundedUnit) { |
|
100
|
|
|
$sum += $refundedUnit->getAmount(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $sum; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|