StatusAction::execute()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 7
nop 1
dl 0
loc 32
rs 9.0968
c 0
b 0
f 0
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\SyliusAdyenPlugin\Action;
14
15
use BitBag\SyliusAdyenPlugin\Bridge\AdyenBridgeInterface;
16
use Payum\Core\Action\ActionInterface;
17
use Payum\Core\ApiAwareInterface;
18
use Payum\Core\Exception\UnsupportedApiException;
19
use Payum\Core\GatewayAwareInterface;
20
use Payum\Core\GatewayAwareTrait;
21
use Payum\Core\Request\GetHttpRequest;
22
use Payum\Core\Request\GetStatusInterface;
23
use Payum\Core\Exception\RequestNotSupportedException;
24
use Sylius\Component\Core\Model\PaymentInterface;
25
26
final class StatusAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
27
{
28
    use GatewayAwareTrait;
29
30
    /**
31
     * @var AdyenBridgeInterface
32
     */
33
    protected $api;
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function setApi($api): void
39
    {
40
        if (false === $api instanceof AdyenBridgeInterface) {
41
            throw new UnsupportedApiException(sprintf('Not supported. Expected %s instance to be set as api.', AdyenBridgeInterface::class));
42
        }
43
44
        $this->api = $api;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     *
50
     * @param GetStatusInterface $request
51
     */
52
    public function execute($request): void
53
    {
54
        RequestNotSupportedException::assertSupports($this, $request);
55
56
        /** @var PaymentInterface $payment */
57
        $payment = $request->getModel();
58
59
        $details = $payment->getDetails();
60
61
        if (!isset($details['authResult'])) {
62
63
            $this->gateway->execute($httpRequest = new GetHttpRequest());
64
65
            if (true === $this->api->verifyRequest($httpRequest->query, $details)) {
66
                $details['authResult'] = $httpRequest->query['authResult'];
67
            } else {
68
                $request->markNew();
69
                return;
70
            }
71
        }
72
73
        if (isset($details['response_status'])) {
74
            if (200 !== $details['response_status']) {
75
                $request->markFailed();
76
                return;
77
            }
78
        }
79
80
        $payment->setDetails($details);
81
82
        $this->resolvePaymentStatus($details['authResult'], $request);
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function supports($request): bool
89
    {
90
        return
91
            $request instanceof GetStatusInterface &&
92
            $request->getModel() instanceof PaymentInterface
93
        ;
94
    }
95
96
    /**
97
     * @param string $authResult
98
     * @param GetStatusInterface $request
99
     */
100
    private function resolvePaymentStatus(string $authResult, GetStatusInterface $request): void
101
    {
102
        switch ($authResult) {
103
            case null:
104
                $request->markNew();
105
                break;
106
            case AdyenBridgeInterface::AUTHORISED:
107
            case AdyenBridgeInterface::AUTHORISATION:
108
                $request->markCaptured();
109
                break;
110
            case AdyenBridgeInterface::PENDING:
111
                $request->markPending();
112
                break;
113
            case AdyenBridgeInterface::CAPTURE:
114
                $request->markCaptured();
115
                break;
116
            case AdyenBridgeInterface::CANCELLED:
117
            case AdyenBridgeInterface::CANCELLATION:
118
            case AdyenBridgeInterface::CANCEL_OR_REFUND:
119
                $request->markCanceled();
120
                break;
121
            case AdyenBridgeInterface::REFUSED:
122
            case AdyenBridgeInterface::ERROR:
123
                $request->markFailed();
124
                break;
125
            case AdyenBridgeInterface::NOTIFICATION_OF_CHARGEBACK:
126
            case AdyenBridgeInterface::CHARGEBACK:
127
            case AdyenBridgeInterface::CHARGEBACK_REVERSED:
128
            case AdyenBridgeInterface::REFUND_FAILED:
129
            case AdyenBridgeInterface::CAPTURE_FAILED:
130
                $request->markSuspended();
131
                break;
132
            case AdyenBridgeInterface::EXPIRE:
133
                $request->markExpired();
134
                break;
135
            case AdyenBridgeInterface::REFUND:
136
            case AdyenBridgeInterface::REFUNDED_REVERSED:
137
                $request->markRefunded();
138
                break;
139
            default:
140
                $request->markUnknown();
141
                break;
142
        }
143
    }
144
}
145