StatusAction   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 99
dl 0
loc 178
rs 9.76
c 0
b 0
f 0
wmc 33

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A supports() 0 5 2
D execute() 0 142 30
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\Action;
13
14
use BitBag\SyliusMolliePlugin\Action\Api\BaseApiAwareAction;
15
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
16
use BitBag\SyliusMolliePlugin\Payments\Methods\MealVoucher;
17
use BitBag\SyliusMolliePlugin\Refund\OrderRefundInterface;
18
use BitBag\SyliusMolliePlugin\Refund\PaymentRefundInterface;
19
use BitBag\SyliusMolliePlugin\Updater\Order\OrderVoucherAdjustmentUpdaterInterface;
20
use Mollie\Api\Exceptions\ApiException;
21
use Mollie\Api\Resources\Customer;
22
use Mollie\Api\Resources\Payment;
23
use Mollie\Api\Resources\Subscription;
24
use Mollie\Api\Types\PaymentStatus;
25
use Mollie\Api\Types\SubscriptionStatus;
26
use Payum\Core\Exception\RequestNotSupportedException;
27
use Payum\Core\GatewayAwareTrait;
28
use Payum\Core\Request\GetStatusInterface;
29
use Sylius\Component\Core\Model\PaymentInterface;
30
31
final class StatusAction extends BaseApiAwareAction implements StatusActionInterface
32
{
33
    use GatewayAwareTrait;
34
35
    /** @var PaymentRefundInterface */
36
    private $paymentRefund;
37
38
    /** @var OrderRefundInterface */
39
    private $orderRefund;
40
41
    /** @var MollieLoggerActionInterface */
42
    private $loggerAction;
43
44
    /** @var OrderVoucherAdjustmentUpdaterInterface */
45
    private $orderVoucherAdjustmentUpdater;
46
47
    public function __construct(
48
        PaymentRefundInterface $paymentRefund,
49
        OrderRefundInterface $orderRefund,
50
        MollieLoggerActionInterface $loggerAction,
51
        OrderVoucherAdjustmentUpdaterInterface $orderVoucherAdjustmentUpdater
52
    ) {
53
        $this->paymentRefund = $paymentRefund;
54
        $this->orderRefund = $orderRefund;
55
        $this->loggerAction = $loggerAction;
56
        $this->orderVoucherAdjustmentUpdater = $orderVoucherAdjustmentUpdater;
57
    }
58
59
    /** @param GetStatusInterface $request */
60
    public function execute($request): void
61
    {
62
        RequestNotSupportedException::assertSupports($this, $request);
63
64
        /** @var PaymentInterface $payment */
65
        $payment = $request->getModel();
66
67
        $details = $payment->getDetails();
68
69
        if (
70
            !isset($details['payment_mollie_id']) &&
71
            !isset($details['subscription_mollie_id']) &&
72
            !isset($details['order_mollie_id']) &&
73
            !isset($details['statusError'])
74
        ) {
75
            $request->markNew();
76
            $this->loggerAction->addLog(sprintf('Mark new payment with id %s', $payment->getId()));
77
78
            return;
79
        }
80
81
        if (isset($details['statusError'])) {
82
            $request->markFailed();
83
84
            return;
85
        }
86
87
        if (true === isset($details['subscription_mollie_id'])) {
88
            try {
89
                /** @var Customer $customer */
90
                $customer = $this->mollieApiClient->customers->get($details['customer_mollie_id']);
91
            } catch (\Exception $e) {
92
                $this->loggerAction->addNegativeLog(sprintf('Error with get customer from mollie with: %s', $e->getMessage()));
93
94
                throw new ApiException(sprintf('Error with get customer from mollie with: %s', $e->getMessage()));
95
            }
96
97
            /** @var Subscription $subscription */
98
            $subscription = $customer->getSubscription($details['subscription_mollie_id']);
99
100
            switch ($subscription->status) {
101
                case SubscriptionStatus::STATUS_CANCELED:
102
                    $request->markCanceled();
103
104
                    break;
105
                case SubscriptionStatus::STATUS_ACTIVE:
106
                case SubscriptionStatus::STATUS_PENDING:
107
                case SubscriptionStatus::STATUS_COMPLETED:
108
                case SubscriptionStatus::STATUS_SUSPENDED:
109
                    $request->markCaptured();
110
111
                    break;
112
                default:
113
                    $request->markUnknown();
114
115
                    break;
116
            }
117
118
            $this->loggerAction->addLog(sprintf('Mark subscription status to: %s', $subscription->status));
119
120
            return;
121
        }
122
123
        if (false === isset($details['subscription_mollie_id']) && isset($details['payment_mollie_id'])) {
124
            try {
125
                $molliePayment = $this->mollieApiClient->payments->get($details['payment_mollie_id']);
126
            } catch (\Exception $e) {
127
                $this->loggerAction->addNegativeLog(sprintf('Error with get payment in status action with id %s', $details['payment_mollie_id']));
128
129
                throw new ApiException(sprintf('Error with get payment in status action with id %s', $details['payment_mollie_id']));
130
            }
131
        }
132
133
        if (false === isset($details['subscription_mollie_id']) && isset($details['order_mollie_id'])) {
134
            try {
135
                $order = $this->mollieApiClient->orders->get($details['order_mollie_id'], ['embed' => 'payments']);
136
                $payments = $order->_embedded->payments;
137
138
                /** @var Payment $payment */
139
                $payment = current($payments);
140
141
                if ($payment->method === MealVoucher::MEAL_VOUCHERS) {
142
                    $this->orderVoucherAdjustmentUpdater->update($payment, $order->metadata->order_id);
143
                }
144
145
                /** @var Payment $molliePayment */
146
                $molliePayment = $this->mollieApiClient->payments->get($payment->id);
147
                $molliePayment->metadata = $order->metadata;
148
            } catch (\Exception $e) {
149
                $this->loggerAction->addNegativeLog(sprintf('Error with get payment page with id %s', $details['payment_mollie_id']));
150
151
                throw new ApiException(sprintf('Error with get payment page with id %s', $details['payment_mollie_id']));
152
            }
153
        }
154
        if ($molliePayment->hasRefunds() || $molliePayment->hasChargebacks()) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $molliePayment does not seem to be defined for all execution paths leading up to this point.
Loading history...
155
            if (isset($details['order_mollie_id'])) {
156
                $this->orderRefund->refund($order);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $order does not seem to be defined for all execution paths leading up to this point.
Loading history...
157
                $this->loggerAction->addLog(sprintf('Mark payment order refunded to: %s', $molliePayment->status));
158
159
                return;
160
            }
161
162
            $this->paymentRefund->refund($molliePayment);
163
164
            $this->loggerAction->addLog(sprintf('Mark payment refunded to: %s', $molliePayment->status));
165
166
            return;
167
        }
168
169
        switch ($molliePayment->status) {
170
            case PaymentStatus::STATUS_PENDING:
171
            case PaymentStatus::STATUS_OPEN:
172
                $request->markPending();
173
174
                break;
175
            case PaymentStatus::STATUS_AUTHORIZED:
176
                $request->markAuthorized();
177
178
                break;
179
            case PaymentStatus::STATUS_PAID:
180
                $request->markCaptured();
181
182
                break;
183
            case PaymentStatus::STATUS_CANCELED:
184
                $request->markCanceled();
185
186
                break;
187
            case PaymentStatus::STATUS_FAILED:
188
                $request->markFailed();
189
190
                break;
191
            case PaymentStatus::STATUS_EXPIRED:
192
                $request->markExpired();
193
194
                break;
195
            default:
196
                $request->markUnknown();
197
198
                break;
199
        }
200
201
        $this->loggerAction->addLog(sprintf('Mark payment status to: %s', $molliePayment->status));
202
    }
203
204
    public function supports($request): bool
205
    {
206
        return
207
            $request instanceof GetStatusInterface &&
208
            $request->getModel() instanceof PaymentInterface;
209
    }
210
}
211