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 Mollie\Api\Exceptions\ApiException; |
18
|
|
|
use Payum\Core\Action\ActionInterface; |
19
|
|
|
use Payum\Core\ApiAwareInterface; |
20
|
|
|
use Payum\Core\Bridge\Spl\ArrayObject; |
21
|
|
|
use Payum\Core\Exception\RequestNotSupportedException; |
22
|
|
|
use Payum\Core\GatewayAwareInterface; |
23
|
|
|
use Payum\Core\GatewayAwareTrait; |
24
|
|
|
use Payum\Core\Request\Refund; |
25
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
26
|
|
|
use Sylius\Component\Resource\Exception\UpdateHandlingException; |
27
|
|
|
|
28
|
|
|
final class RefundAction 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
|
|
|
/** @param Refund $request */ |
47
|
|
|
public function execute($request): void |
48
|
|
|
{ |
49
|
|
|
RequestNotSupportedException::assertSupports($this, $request); |
50
|
|
|
|
51
|
|
|
$details = ArrayObject::ensureArrayObject($request->getModel()); |
52
|
|
|
/** @var PaymentInterface $payment */ |
53
|
|
|
$payment = $request->getFirstModel(); |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$molliePayment = $this->mollieApiClient->payments->get($details['payment_mollie_id']); |
57
|
|
|
$refundData = $this->convertOrderRefundData->convert($details['metadata']['refund'], $payment->getCurrencyCode()); |
|
|
|
|
58
|
|
|
|
59
|
|
|
if (true === $molliePayment->canBeRefunded()) { |
60
|
|
|
$molliePayment->refund(['amount' => $refundData]); |
61
|
|
|
$this->loggerAction->addLog(sprintf('Refund action with payment id %s', $molliePayment->id)); |
62
|
|
|
} else { |
63
|
|
|
$this->loggerAction->addNegativeLog(sprintf('Payment %s can not be refunded.', $molliePayment->id)); |
64
|
|
|
|
65
|
|
|
throw new UpdateHandlingException(sprintf('Payment %s can not be refunded.', $molliePayment->id)); |
66
|
|
|
} |
67
|
|
|
} catch (ApiException $e) { |
68
|
|
|
$this->loggerAction->addNegativeLog(sprintf('API call failed: %s', htmlspecialchars($e->getMessage()))); |
69
|
|
|
|
70
|
|
|
throw new \Exception(sprintf('API call failed: %s', htmlspecialchars($e->getMessage()))); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function supports($request): bool |
75
|
|
|
{ |
76
|
|
|
return |
77
|
|
|
$request instanceof Refund && |
78
|
|
|
$request->getModel() instanceof \ArrayAccess |
79
|
|
|
; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|