RefundPaymentProcessor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
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()) {
0 ignored issues
show
Deprecated Code introduced by
The function Payum\Core\Model\Gateway...rface::getFactoryName() has been deprecated: since 1.3.3 will be removed in 2.0. set factory option inside the config ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

48
        if (QuadPayGatewayFactory::FACTORY_NAME !== /** @scrutinizer ignore-deprecated */ $paymentMethod->getGatewayConfig()->getFactoryName()) {

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.

Loading history...
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