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\Action; |
||
13 | |||
14 | use BitBag\SyliusMolliePlugin\Action\Api\BaseApiAwareAction; |
||
15 | use BitBag\SyliusMolliePlugin\Helper\ConvertRefundDataInterface; |
||
16 | use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface; |
||
17 | use BitBag\SyliusMolliePlugin\Request\Api\RefundOrder; |
||
18 | use Mollie\Api\Exceptions\ApiException; |
||
19 | use Payum\Core\Action\ActionInterface; |
||
20 | use Payum\Core\ApiAwareInterface; |
||
21 | use Payum\Core\Bridge\Spl\ArrayObject; |
||
22 | use Payum\Core\Exception\RequestNotSupportedException; |
||
23 | use Payum\Core\GatewayAwareInterface; |
||
24 | use Payum\Core\GatewayAwareTrait; |
||
25 | use Sylius\Component\Core\Model\PaymentInterface; |
||
26 | use Sylius\Component\Resource\Exception\UpdateHandlingException; |
||
27 | |||
28 | final class RefundOrderAction extends BaseApiAwareAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface |
||
29 | { |
||
30 | use GatewayAwareTrait; |
||
31 | |||
32 | /** @var MollieLoggerActionInterface */ |
||
33 | private $loggerAction; |
||
34 | |||
35 | /** @var ConvertRefundDataInterface */ |
||
36 | private $convertOrderRefundData; |
||
37 | |||
38 | public function __construct( |
||
39 | MollieLoggerActionInterface $loggerAction, |
||
40 | ConvertRefundDataInterface $convertOrderRefundData |
||
41 | ) { |
||
42 | $this->loggerAction = $loggerAction; |
||
43 | $this->convertOrderRefundData = $convertOrderRefundData; |
||
44 | } |
||
45 | |||
46 | public function execute($request): void |
||
47 | { |
||
48 | RequestNotSupportedException::assertSupports($this, $request); |
||
49 | |||
50 | $details = ArrayObject::ensureArrayObject($request->getModel()); |
||
51 | |||
52 | /** @var PaymentInterface $payment */ |
||
53 | $payment = $request->getFirstModel(); |
||
54 | $refundData = $this->convertOrderRefundData->convert($details['metadata']['refund'], $payment->getCurrencyCode()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
55 | |||
56 | try { |
||
57 | $order = $this->mollieApiClient->orders->get($details['order_mollie_id'], ['embed' => 'payments']); |
||
58 | $payments = $order->_embedded->payments; |
||
59 | $payment = current($payments); |
||
60 | |||
61 | $molliePayment = $this->mollieApiClient->payments->get($payment->id); |
||
62 | |||
63 | $this->mollieApiClient->payments->refund( |
||
64 | $molliePayment, |
||
65 | [ |
||
66 | 'amount' => $refundData, |
||
67 | ] |
||
68 | ); |
||
69 | |||
70 | $this->loggerAction->addLog(sprintf('Refund order action with order id: %s', $order->id)); |
||
71 | } catch (ApiException $e) { |
||
72 | $this->loggerAction->addNegativeLog(sprintf('Error refund order action with: %s', $e->getMessage())); |
||
73 | |||
74 | throw new UpdateHandlingException(sprintf('API call failed: %s', htmlspecialchars($e->getMessage()))); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | public function supports($request): bool |
||
79 | { |
||
80 | return |
||
81 | $request instanceof RefundOrder && |
||
82 | $request->getModel() instanceof \ArrayAccess; |
||
83 | } |
||
84 | } |
||
85 |