Completed
Push — master ( b7e254...bcf326 )
by
unknown
09:07
created

CreateOrderAction::execute()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 48
rs 9.0808
cc 5
nc 8
nop 1
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\Api;
14
15
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfigInterface;
16
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
17
use BitBag\SyliusMolliePlugin\Request\Api\CreateOrder;
18
use BitBag\SyliusMolliePlugin\Resolver\PaymentMethodConfigResolverInterface;
19
use Mollie\Api\Exceptions\ApiException;
20
use Payum\Core\Bridge\Spl\ArrayObject;
21
use Payum\Core\GatewayAwareTrait;
22
use Payum\Core\Reply\HttpRedirect;
23
24
final class CreateOrderAction extends BaseApiAwareAction
25
{
26
    use GatewayAwareTrait;
27
28
    /** @var PaymentMethodConfigResolverInterface */
29
    private $methodConfigResolver;
30
31
    /** @var MollieLoggerActionInterface */
32
    private $loggerAction;
33
34
    public function __construct(
35
        PaymentMethodConfigResolverInterface $methodConfigResolver,
36
        MollieLoggerActionInterface $loggerAction
37
    ) {
38
        $this->methodConfigResolver = $methodConfigResolver;
39
        $this->loggerAction = $loggerAction;
40
    }
41
42
    public function execute($request): void
43
    {
44
        $details = ArrayObject::ensureArrayObject($request->getModel());
45
46
        $issuer = $details['metadata']['selected_issuer'] ?? null;
47
        $method = $details['metadata']['molliePaymentMethods'] ? $details['metadata']['molliePaymentMethods'] : '';
48
49
        if (null !== $method) {
50
            /** @var MollieGatewayConfigInterface $paymentMethod */
51
            $paymentMethod = $this->methodConfigResolver->getConfigFromMethodId($method);
52
53
            $orderExpiredTime = $paymentMethod->getOrderExpiration();
54
            $interval = new \DateInterval('P' . $orderExpiredTime . 'D');
55
            $dateExpired = new \DateTimeImmutable('now');
56
            $dateExpired = $dateExpired->add($interval);
57
        }
58
59
        try {
60
            $order = $this->mollieApiClient->orders->create([
61
                'method' => $method,
62
                'payment' => [
63
                    'issuer' => $issuer,
64
                    'cardToken' => $details['metadata']['cartToken'],
65
                ],
66
                'amount' => $details['amount'],
67
                'billingAddress' => $details['billingAddress'],
68
                'shippingAddress' => $details['shippingAddress'],
69
                'metadata' => $details['metadata'],
70
                'locale' => $details['locale'],
71
                'orderNumber' => $details['orderNumber'],
72
                'redirectUrl' => $details['backurl'],
73
                'webhookUrl' => $details['webhookUrl'],
74
                'lines' => $details['lines'],
75
                'expiresAt' => isset($dateExpired) ?
76
                    $dateExpired->format('Y-m-d') :
77
                    (new \DateTimeImmutable('now'))->format('Y-m-d'),
78
            ]);
79
        } catch (\Exception $e) {
80
            $this->loggerAction->addNegativeLog(sprintf('Error with create order with: %s', $e->getMessage()));
81
82
            throw new ApiException(sprintf('Error with create order with: %s', $e->getMessage()));
83
        }
84
85
        $details['order_mollie_id'] = $order->id;
86
87
        $this->loggerAction->addLog(sprintf('Create new order in mollie with id: %s', $order->id));
88
89
        throw new HttpRedirect($order->getCheckoutUrl());
90
    }
91
92
    public function supports($request): bool
93
    {
94
        return
95
            $request instanceof CreateOrder &&
96
            $request->getModel() instanceof \ArrayAccess;
97
    }
98
}
99