|
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
|
|
|
* another great project. |
|
7
|
|
|
* You can find more information about us on https://bitbag.shop and write us |
|
8
|
|
|
* an email on [email protected]. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace BitBag\SyliusAdyenPlugin\PaymentProcessing; |
|
14
|
|
|
|
|
15
|
|
|
use BitBag\SyliusAdyenPlugin\AdyenGatewayFactory; |
|
16
|
|
|
use Payum\Core\Payum; |
|
17
|
|
|
use Payum\Core\Request\Refund; |
|
18
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
|
19
|
|
|
use Sylius\Component\Core\Model\PaymentMethodInterface; |
|
20
|
|
|
use Sylius\Component\Resource\Exception\UpdateHandlingException; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
22
|
|
|
|
|
23
|
|
|
final class RefundPaymentProcessor implements PaymentProcessorInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Payum |
|
27
|
|
|
*/ |
|
28
|
|
|
private $payum; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var Session |
|
32
|
|
|
*/ |
|
33
|
|
|
private $session; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param Payum $payum |
|
37
|
|
|
* @param Session $session |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(Payum $payum, Session $session) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->payum = $payum; |
|
42
|
|
|
$this->session = $session; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param PaymentInterface $payment |
|
47
|
|
|
* |
|
48
|
|
|
* @throws UpdateHandlingException |
|
49
|
|
|
* @throws \Payum\Core\Reply\ReplyInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
public function process(PaymentInterface $payment): void |
|
52
|
|
|
{ |
|
53
|
|
|
/** @var PaymentMethodInterface $paymentMethod */ |
|
54
|
|
|
$paymentMethod = $payment->getMethod(); |
|
55
|
|
|
|
|
56
|
|
|
if (AdyenGatewayFactory::FACTORY_NAME !== $paymentMethod->getGatewayConfig()->getFactoryName()) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (false === isset($payment->getDetails()['extraData'])) { |
|
61
|
|
|
$this->session->getFlashBag()->add("info", "The payment refund was made only locally."); |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$hash = null !== $payment ? json_decode($payment->getDetails()['extraData'], true)['refundToken'] : ''; |
|
66
|
|
|
|
|
67
|
|
|
if (false === $token = $this->payum->getTokenStorage()->find($hash)) { |
|
68
|
|
|
throw new UpdateHandlingException(sprintf("A token with hash `%s` could not be found.", $hash)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$gateway = $this->payum->getGateway($token->getGatewayName()); |
|
72
|
|
|
|
|
73
|
|
|
$gateway->execute(new Refund($token)); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|