RefundPaymentProcessor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusMultiSafepayPlugin\PaymentProcessing;
12
13
use BitBag\SyliusMultiSafepayPlugin\ApiClient\MultiSafepayApiClientInterface;
14
use BitBag\SyliusMultiSafepayPlugin\MultiSafepayGatewayFactory;
15
use Psr\Log\LoggerInterface;
16
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
17
use Sylius\Component\Core\Model\PaymentInterface;
18
use Sylius\Component\Core\Model\PaymentMethodInterface;
19
use Sylius\Component\Resource\Exception\UpdateHandlingException;
20
use Symfony\Component\HttpFoundation\Session\Session;
21
22
final class RefundPaymentProcessor implements PaymentProcessorInterface
23
{
24
    /** @var Session */
25
    private $session;
26
27
    /** @var MultiSafepayApiClientInterface */
28
    private $multiSafepayApiClient;
29
30
    /** @var LoggerInterface */
31
    private $logger;
32
33
    public function __construct(
34
        Session $session,
35
        MultiSafepayApiClientInterface $multiSafepayApiClient,
36
        LoggerInterface $logger
37
    ) {
38
        $this->session = $session;
39
        $this->multiSafepayApiClient = $multiSafepayApiClient;
40
        $this->logger = $logger;
41
    }
42
43
    public function process(PaymentInterface $payment): void
44
    {
45
        /** @var PaymentMethodInterface $paymentMethod */
46
        $paymentMethod = $payment->getMethod();
47
48
        $details = $payment->getDetails();
49
50
        /** @var GatewayConfigInterface $gatewayConfig */
51
        $gatewayConfig = $paymentMethod->getGatewayConfig();
52
53
        if (
54
            MultiSafepayGatewayFactory::FACTORY_NAME !== $gatewayConfig->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

54
            MultiSafepayGatewayFactory::FACTORY_NAME !== /** @scrutinizer ignore-deprecated */ $gatewayConfig->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...
55
            (isset($details['status']) && MultiSafepayApiClientInterface::STATUS_REFUNDED === $details['status'])
56
        ) {
57
            return;
58
        }
59
60
        if (!isset($details['orderId'])) {
61
            $this->session->getFlashBag()->add('info', 'The payment refund was made only locally.');
62
63
            return;
64
        }
65
66
        $gatewayConfig = $gatewayConfig->getConfig();
67
68
        $this->multiSafepayApiClient->initialise(
69
            $gatewayConfig['apiKey'],
70
            $gatewayConfig['type'],
71
            $gatewayConfig['sandbox']
72
        );
73
74
        try {
75
            $this->multiSafepayApiClient->refund($details['orderId'], (int) $payment->getAmount(), (string) $payment->getCurrencyCode());
76
        } catch (\Exception $exception) {
77
            $message = $exception->getMessage();
78
79
            $this->session->getFlashBag()->add('error', htmlspecialchars($message));
80
81
            throw new UpdateHandlingException();
82
        }
83
    }
84
}
85