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\SyliusQuadPayPlugin\PaymentProcessing; |
14
|
|
|
|
15
|
|
|
use BitBag\SyliusQuadPayPlugin\Client\QuadPayApiClientInterface; |
16
|
|
|
use BitBag\SyliusQuadPayPlugin\QuadPayGatewayFactory; |
17
|
|
|
use GuzzleHttp\Exception\ClientException; |
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\Response; |
22
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
23
|
|
|
|
24
|
|
|
final class RefundPaymentProcessor implements PaymentProcessorInterface |
25
|
|
|
{ |
26
|
|
|
/** @var Session */ |
27
|
|
|
private $session; |
28
|
|
|
|
29
|
|
|
/** @var QuadPayApiClientInterface */ |
30
|
|
|
private $quadPayApiClient; |
31
|
|
|
|
32
|
|
|
/** @var \Faker\Generator */ |
33
|
|
|
private $faker; |
34
|
|
|
|
35
|
|
|
public function __construct(Session $session, QuadPayApiClientInterface $quadPayApiClient) |
36
|
|
|
{ |
37
|
|
|
$this->session = $session; |
38
|
|
|
$this->quadPayApiClient = $quadPayApiClient; |
39
|
|
|
|
40
|
|
|
$this->faker = \Faker\Factory::create(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function process(PaymentInterface $payment): void |
44
|
|
|
{ |
45
|
|
|
/** @var PaymentMethodInterface $paymentMethod */ |
46
|
|
|
$paymentMethod = $payment->getMethod(); |
47
|
|
|
|
48
|
|
|
if (QuadPayGatewayFactory::FACTORY_NAME !== $paymentMethod->getGatewayConfig()->getFactoryName()) { |
|
|
|
|
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$details = $payment->getDetails(); |
53
|
|
|
|
54
|
|
|
if (false === isset($details['orderToken'])) { |
55
|
|
|
$this->session->getFlashBag()->add('info', 'The payment refund was made only locally.'); |
56
|
|
|
|
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$gatewayConfig = $paymentMethod->getGatewayConfig()->getConfig(); |
61
|
|
|
|
62
|
|
|
$this->quadPayApiClient->setConfig( |
63
|
|
|
$gatewayConfig['clientId'], |
64
|
|
|
$gatewayConfig['clientSecret'], |
65
|
|
|
$gatewayConfig['apiEndpoint'], |
66
|
|
|
$gatewayConfig['authTokenEndpoint'], |
67
|
|
|
$gatewayConfig['apiAudience'] |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$merchantRefundReference = $this->faker->uuid; |
71
|
|
|
|
72
|
|
|
$details['merchantRefundReference'] = $merchantRefundReference; |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$result = $this->quadPayApiClient->refund( |
76
|
|
|
$payment->getAmount() / 100, |
77
|
|
|
$merchantRefundReference, |
78
|
|
|
$details['orderToken'], |
79
|
|
|
$details['orderId'] ?? null |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$details['refundDetails'] = $result; |
83
|
|
|
|
84
|
|
|
$payment->setDetails($details); |
85
|
|
|
} catch (ClientException $clientException) { |
86
|
|
|
$message = $clientException->getMessage(); |
87
|
|
|
|
88
|
|
|
if (Response::HTTP_UNPROCESSABLE_ENTITY === $clientException->getCode()) { |
89
|
|
|
$message = (string) $clientException->getResponse()->getBody(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->session->getFlashBag()->add('error', $message); |
93
|
|
|
|
94
|
|
|
throw new UpdateHandlingException(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.