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

CreatePaymentAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 24
c 1
b 0
f 0
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 27 3
A __construct() 0 3 1
A supports() 0 5 2
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\Logger\MollieLoggerActionInterface;
16
use BitBag\SyliusMolliePlugin\Request\Api\CreatePayment;
17
use Mollie\Api\Exceptions\ApiException;
18
use Mollie\Api\Resources\Payment;
19
use Payum\Core\Bridge\Spl\ArrayObject;
20
use Payum\Core\GatewayAwareTrait;
21
use Payum\Core\Reply\HttpRedirect;
22
23
final class CreatePaymentAction extends BaseApiAwareAction
24
{
25
    use GatewayAwareTrait;
26
27
    /** @var MollieLoggerActionInterface */
28
    private $loggerAction;
29
30
    public function __construct(MollieLoggerActionInterface $loggerAction)
31
    {
32
        $this->loggerAction = $loggerAction;
33
    }
34
35
    public function execute($request): void
36
    {
37
        $details = ArrayObject::ensureArrayObject($request->getModel());
38
39
        try {
40
            /** @var Payment $payment */
41
            $payment = $this->mollieApiClient->payments->create([
42
                'method' => $details['metadata']['molliePaymentMethods'] ? $details['metadata']['molliePaymentMethods'] : '',
43
                'issuer' => $details['metadata']['selected_issuer'] ?? null,
44
                'cardToken' => $details['metadata']['cartToken'],
45
                'amount' => $details['amount'],
46
                'description' => $details['description'],
47
                'redirectUrl' => $details['backurl'],
48
                'webhookUrl' => $details['webhookUrl'],
49
                'metadata' => $details['metadata'],
50
            ]);
51
        } catch (\Exception $e) {
52
            $this->loggerAction->addNegativeLog(sprintf('Error with create payment with: %s', $e->getMessage()));
53
54
            throw new ApiException(sprintf('Error with create payment with: %s', $e->getMessage()));
55
        }
56
57
        $details['payment_mollie_id'] = $payment->id;
58
59
        $this->loggerAction->addLog(sprintf('Create payment in mollie with id: %s', $payment->id));
60
61
        throw new HttpRedirect($payment->getCheckoutUrl());
62
    }
63
64
    public function supports($request): bool
65
    {
66
        return
67
            $request instanceof CreatePayment &&
68
            $request->getModel() instanceof \ArrayAccess;
69
    }
70
}
71