Passed
Push — master ( eb8dec...5ac1fa )
by
unknown
05:11
created

ConvertPaymentAction   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 56
c 1
b 0
f 0
dl 0
loc 109
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B execute() 0 67 7
A supports() 0 6 3
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\SyliusMolliePlugin\Action;
14
15
use BitBag\SyliusMolliePlugin\Action\Api\BaseApiAwareAction;
16
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfigInterface;
17
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactoryInterface;
18
use BitBag\SyliusMolliePlugin\Helper\ConvertOrderInterface;
19
use BitBag\SyliusMolliePlugin\Payments\PaymentTerms\Options;
20
use BitBag\SyliusMolliePlugin\Request\Api\CreateCustomer;
21
use Payum\Core\Action\ActionInterface;
22
use Payum\Core\ApiAwareInterface;
23
use Payum\Core\Exception\RequestNotSupportedException;
24
use Payum\Core\GatewayAwareInterface;
25
use Payum\Core\GatewayAwareTrait;
26
use Payum\Core\Request\Convert;
27
use Payum\Core\Request\GetCurrency;
28
use Sylius\Bundle\CoreBundle\Context\CustomerContext;
29
use Sylius\Bundle\PayumBundle\Provider\PaymentDescriptionProviderInterface;
30
use Sylius\Component\Core\Model\OrderInterface;
31
use Sylius\Component\Core\Model\PaymentInterface;
32
use Sylius\Component\Resource\Repository\RepositoryInterface;
33
use Symfony\Component\HttpFoundation\Session\SessionInterface;
34
35
final class ConvertPaymentAction extends BaseApiAwareAction implements ActionInterface, GatewayAwareInterface, ApiAwareInterface
36
{
37
    use GatewayAwareTrait;
38
39
    /** @var PaymentDescriptionProviderInterface */
40
    private $paymentDescriptionProvider;
41
42
    /** @var SessionInterface */
43
    private $session;
44
45
    /** @var RepositoryInterface */
46
    private $mollieMethodsRepository;
47
48
    /** @var ConvertOrderInterface */
49
    private $orderConverter;
50
51
    /** @var CustomerContext */
52
    private $customerContext;
53
54
    public function __construct(
55
        PaymentDescriptionProviderInterface $paymentDescriptionProvider,
56
        SessionInterface $session,
57
        RepositoryInterface $mollieMethodsRepository,
58
        ConvertOrderInterface $orderConverter,
59
        CustomerContext $customerContext
60
    ) {
61
        $this->paymentDescriptionProvider = $paymentDescriptionProvider;
62
        $this->session = $session;
63
        $this->mollieMethodsRepository = $mollieMethodsRepository;
64
        $this->orderConverter = $orderConverter;
65
        $this->customerContext = $customerContext;
66
    }
67
68
    /** @param Convert $request */
69
    public function execute($request): void
70
    {
71
        RequestNotSupportedException::assertSupports($this, $request);
72
73
        /** @var PaymentInterface $payment */
74
        $payment = $request->getSource();
75
76
        /** @var OrderInterface $order */
77
        $order = $payment->getOrder();
78
79
        $customer = $order->getCustomer();
80
81
        $this->gateway->execute($currency = new GetCurrency($payment->getCurrencyCode()));
82
83
        $divisor = 10 ** $currency->exp;
84
85
        $amount = number_format(abs($payment->getAmount() / $divisor), 2, '.', '');
86
        $paymentOptions = $this->session->get('mollie_payment_options');
87
88
        /** @var MollieGatewayConfigInterface $method */
89
        $method = $this->mollieMethodsRepository->findOneBy(['methodId' => $paymentOptions['molliePaymentMethods']]);
90
        $gatewayConfig = $method->getGateway()->getConfig();
91
92
        $details = [
93
            'amount' => [
94
                'value' => "$amount",
95
                'currency' => $currency->code,
96
            ],
97
            'description' => $this->paymentDescriptionProvider->getPaymentDescription($payment),
98
            'metadata' => [
99
                'order_id' => $order->getId(),
100
                'customer_id' => $customer->getId() ?? null,
101
                'molliePaymentMethods' => $paymentOptions['molliePaymentMethods'] ?? null,
102
                'cartToken' => $paymentOptions['cartToken'] ?? null,
103
                'selected_issuer' => $paymentOptions['selected_issuer'] ?? null,
104
            ],
105
            'full_name' => $customer->getFullName() ?? null,
106
            'email' => $customer->getEmail() ?? null,
107
        ];
108
109
        if (null !== $this->customerContext->getCustomer() && true === $gatewayConfig['single_click_enabled']) {
110
            $this->gateway->execute($mollieCustomer = new CreateCustomer($details));
111
            $model = $mollieCustomer->getModel();
112
            $details['metadata']['customer_mollie_id'] = $model['customer_mollie_id'];
113
        }
114
115
        if (true === $this->mollieApiClient->isRecurringSubscription()) {
116
            $config = $this->mollieApiClient->getConfig();
117
118
            $details['times'] = $config['times'];
119
            $details['interval'] = $config['interval'];
120
        }
121
122
        if (false === $this->mollieApiClient->isRecurringSubscription()) {
123
            $details['customerId'] = $model['customer_mollie_id'] ?? null;
124
            $details['metadata']['methodType'] = Options::PAYMENT_API;
125
            $details['locale'] = true === in_array($order->getLocaleCode(), MollieGatewayFactoryInterface::LOCALES_AVAILABLE) ? $order->getLocaleCode() : 'en_US';
126
127
            if (array_search($method->getPaymentType(), Options::getAvailablePaymentType()) === Options::ORDER_API) {
128
                unset($details['customerId']);
129
130
                $details['metadata']['methodType'] = Options::ORDER_API;
131
                $details = $this->orderConverter->convert($order, $details, $divisor);
132
            }
133
        }
134
135
        $request->setResult($details);
136
    }
137
138
    public function supports($request): bool
139
    {
140
        return
141
            $request instanceof Convert &&
142
            $request->getSource() instanceof PaymentInterface &&
143
            $request->getTo() === 'array'
144
            ;
145
    }
146
}
147