PaymentlinkResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 12
rs 10
cc 1
nc 1
nop 5
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
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Resolver;
13
14
use BitBag\SyliusMolliePlugin\Client\MollieApiClient;
15
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfig;
16
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
17
use BitBag\SyliusMolliePlugin\Form\Type\MollieGatewayConfigurationType;
18
use BitBag\SyliusMolliePlugin\Helper\IntToStringConverter;
19
use BitBag\SyliusMolliePlugin\Preparer\PaymentLinkEmailPreparerInterface;
20
use Liip\ImagineBundle\Exception\Config\Filter\NotFoundException;
21
use Sylius\AdminOrderCreationPlugin\Provider\PaymentTokenProviderInterface;
22
use Sylius\Component\Core\Model\OrderInterface;
23
use Sylius\Component\Core\Model\PaymentInterface;
24
use Sylius\Component\Core\Model\PaymentMethodInterface;
25
use Sylius\Component\Resource\Repository\RepositoryInterface;
26
27
final class PaymentlinkResolver implements PaymentlinkResolverInterface
28
{
29
    /** @var MollieApiClient */
30
    private $mollieApiClient;
31
32
    /** @var IntToStringConverter */
33
    private $intToStringConverter;
34
35
    /** @var RepositoryInterface */
36
    private $orderRepository;
37
38
    /** @var PaymentLinkEmailPreparerInterface */
39
    private $emailPreparer;
40
41
    /** @var PaymentTokenProviderInterface */
42
    private $paymentTokenProvider;
43
44
    public function __construct(
45
        MollieApiClient $mollieApiClient,
46
        IntToStringConverter $intToStringConverter,
47
        RepositoryInterface $orderRepository,
48
        PaymentLinkEmailPreparerInterface $emailPreparer,
49
        PaymentTokenProviderInterface $paymentTokenProvider
50
    ) {
51
        $this->mollieApiClient = $mollieApiClient;
52
        $this->intToStringConverter = $intToStringConverter;
53
        $this->orderRepository = $orderRepository;
54
        $this->emailPreparer = $emailPreparer;
55
        $this->paymentTokenProvider = $paymentTokenProvider;
56
    }
57
58
    public function resolve(OrderInterface $order, array $data, string $templateName): string
59
    {
60
        $methodsArray = [];
61
        $methods = $data['methods'] ?? $data['methods'] = [];
62
63
        /** @var PaymentInterface $syliusPayment */
64
        $syliusPayment = $order->getPayments()->last();
65
66
        /** @var PaymentMethodInterface $paymentMethod */
67
        $paymentMethod = $syliusPayment->getMethod();
68
69
        if (MollieGatewayFactory::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

69
        if (MollieGatewayFactory::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...
70
            throw new NotFoundException('No method mollie found in order');
71
        }
72
73
        $modusKey = $this->getModus($paymentMethod->getGatewayConfig()->getConfig());
74
75
        /** @var MollieGatewayConfig $method */
76
        foreach ($methods as $method) {
77
            if (in_array($method->getMethodId(), self::NO_AVAILABLE_METHODS)) {
78
                continue;
79
            }
80
81
            $methodsArray[] = $method->getMethodId();
82
        }
83
84
        $this->mollieApiClient->setApiKey($modusKey);
85
        $details = $syliusPayment->getDetails();
86
87
        if (empty($details) || !isset($details['webhookUrl'])) {
88
            return '';
89
        }
90
91
        try {
92
            $token = $this->paymentTokenProvider->getPaymentToken($syliusPayment);
93
            $redirectURL = $token->getTargetUrl();
94
        } catch (\Exception $e) {
95
            $redirectURL = $details['backurl'];
96
        }
97
98
        $data = [
99
            'method' => $methodsArray,
100
            'amount' => [
101
                'currency' => (string) $syliusPayment->getCurrencyCode(),
102
                'value' => $this->intToStringConverter->convertIntToString($syliusPayment->getAmount(), 100),
0 ignored issues
show
Bug introduced by
It seems like $syliusPayment->getAmount() can also be of type null; however, parameter $value of BitBag\SyliusMolliePlugi...r::convertIntToString() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

102
                'value' => $this->intToStringConverter->convertIntToString(/** @scrutinizer ignore-type */ $syliusPayment->getAmount(), 100),
Loading history...
103
            ],
104
            'description' => $order->getNumber(),
105
            'redirectUrl' => $redirectURL,
106
            'webhookUrl' => $details['webhookUrl'],
107
            'metadata' => [
108
                'order_id' => $order->getId(),
109
                'refund_token' => $details['refund_token'] ?? null,
110
                'customer_id' => $order->getCustomer()->getId(),
111
            ],
112
        ];
113
114
        $payment = $this->mollieApiClient->payments->create($data);
115
        $details['payment_mollie_id'] = $payment->id;
116
        $details['order_mollie_id'] = null;
117
        $details['metadata']['refund_token'] = $details['refund_token'] ?? null;
118
        $details['payment_mollie_link'] = $payment->_links->checkout->href;
119
        $details['backurl'] = $redirectURL;
120
121
        $syliusPayment->setDetails($details);
122
123
        $this->orderRepository->add($order);
124
125
        $this->emailPreparer->prepare($order, $templateName);
126
127
        return $payment->_links->checkout->href;
128
    }
129
130
    private function getModus(array $config): string
131
    {
132
        if ($config['environment']) {
133
            return $config[MollieGatewayConfigurationType::API_KEY_LIVE];
134
        }
135
136
        return $config[MollieGatewayConfigurationType::API_KEY_TEST];
137
    }
138
}
139