-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransitionSubscriber.php
More file actions
209 lines (182 loc) · 9 KB
/
TransitionSubscriber.php
File metadata and controls
209 lines (182 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
declare(strict_types=1);
/*
* Copyright (c) Ratepay GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ratepay\RpayPayments\Components\StateMachine\Subscriber;
use Exception;
use Psr\Log\LoggerInterface;
use Ratepay\RpayPayments\Components\RatepayApi\Dto\OrderOperationData;
use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\AbstractModifyRequest;
use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\PaymentCancelService;
use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\PaymentDeliverService;
use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\PaymentReturnService;
use Ratepay\RpayPayments\Core\Entity\Extension\OrderExtension;
use Ratepay\RpayPayments\Core\Entity\RatepayOrderDataEntity;
use Ratepay\RpayPayments\Core\PluginConfigService;
use Ratepay\RpayPayments\Util\CriteriaHelper;
use Ratepay\RpayPayments\Util\MethodHelper;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryDefinition;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryStates;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class TransitionSubscriber implements EventSubscriberInterface
{
public const PREVENT_BIDIRECTIONALITY = 'prevent_bidirectionality';
public function __construct(
private readonly EntityRepository $orderDeliveryRepository,
private readonly EntityRepository $orderTransactionRepository,
private readonly EntityRepository $orderRepository,
private readonly PluginConfigService $configService,
private readonly PaymentDeliverService $paymentDeliverService,
private readonly PaymentCancelService $paymentCancelService,
private readonly PaymentReturnService $paymentReturnService,
private readonly LoggerInterface $logger
) {
}
public static function getSubscribedEvents(): array
{
return [
StateMachineTransitionEvent::class => 'onTransition',
];
}
public function onTransition(StateMachineTransitionEvent $event): void
{
if ($event->getEntityName() === OrderTransactionDefinition::ENTITY_NAME) {
$this->onTransactionStatusTransition($event);
} else if ($event->getEntityName() === OrderDeliveryDefinition::ENTITY_NAME) {
$this->onDeliveryStatusTransition($event);
}
}
protected function onTransactionStatusTransition(StateMachineTransitionEvent $event): void
{
if ($this->configService->getCaptureTrigger() !== 'pay') {
return;
}
/** @var ArrayStruct|null $struct */
$struct = $event->getContext()->getExtension('ratepay');
if ($struct?->get(self::PREVENT_BIDIRECTIONALITY) === true) {
return;
}
/** @var OrderTransactionEntity $orderTransaction */
$orderTransaction = $this->orderTransactionRepository->search(new Criteria([$event->getEntityId()]), $event->getContext())->first();
/** @var OrderEntity $order */
$order = $this->orderRepository->search(CriteriaHelper::getCriteriaForOrder($orderTransaction->getOrderId()), $event->getContext())->first();
if (!MethodHelper::isRatepayOrder($order)) {
return;
}
$ratepayData = $order->getExtension(OrderExtension::EXTENSION_NAME);
if (!$ratepayData instanceof RatepayOrderDataEntity) {
$this->logger->warning('Error during bidirectionality: No Ratepay Data was found.', [
'order' => $order->getId(),
'orderNumber' => $order->getOrderNumber(),
]);
return;
}
switch ($event->getToPlace()->getTechnicalName()) {
case OrderTransactionStates::STATE_PAID:
$operation = OrderOperationData::OPERATION_DELIVER;
$service = $this->paymentDeliverService;
break;
default:
// do nothing
return;
}
$this->performOperation($event, $order, $operation, $service, $ratepayData);
}
protected function onDeliveryStatusTransition(StateMachineTransitionEvent $event): void
{
if (!$this->configService->isAutoOperationBasedOnDeliveryStatusEnabled()) {
return;
}
/** @var ArrayStruct|null $struct */
$struct = $event->getContext()->getExtension('ratepay');
if ($struct?->get(self::PREVENT_BIDIRECTIONALITY) === true) {
return;
}
/** @var OrderDeliveryEntity $orderDelivery */
$orderDelivery = $this->orderDeliveryRepository->search(new Criteria([$event->getEntityId()]), $event->getContext())->first();
/** @var OrderEntity $order */
$order = $this->orderRepository->search(CriteriaHelper::getCriteriaForOrder($orderDelivery->getOrderId()), $event->getContext())->first();
if (!MethodHelper::isRatepayOrder($order)) {
return;
}
$ratepayData = $order->getExtension(OrderExtension::EXTENSION_NAME);
if (!$ratepayData instanceof RatepayOrderDataEntity) {
$this->logger->warning('Error during bidirectionality: No Ratepay Data was found.', [
'order' => $order->getId(),
'orderNumber' => $order->getOrderNumber(),
]);
return;
}
switch ($event->getToPlace()->getTechnicalName()) {
case OrderDeliveryStates::STATE_SHIPPED:
if ($this->configService->getCaptureTrigger() !== 'deliver') {
// do nothing
return;
}
$operation = OrderOperationData::OPERATION_DELIVER;
$service = $this->paymentDeliverService;
break;
case OrderDeliveryStates::STATE_CANCELLED:
if ($this->configService->getPerformTransactionRefunds() !== 'automatic') {
// do nothing
return;
}
$operation = OrderOperationData::OPERATION_CANCEL;
$service = $this->paymentCancelService;
break;
case OrderDeliveryStates::STATE_RETURNED:
if ($this->configService->getPerformTransactionRefunds() !== 'automatic') {
// do nothing
return;
}
$operation = OrderOperationData::OPERATION_RETURN;
$service = $this->paymentReturnService;
break;
default:
// do nothing
return;
}
$this->performOperation($event, $order, $operation, $service, $ratepayData);
}
protected function performOperation(StateMachineTransitionEvent $event, OrderEntity $order, string $operation, AbstractModifyRequest $service, RatepayOrderDataEntity $ratepayData): void
{
// we need this to prevent endless recursion if update-delivery-status is enabled.
// this should never happen, because shopware can not change the status to the actual status again and so this subscriber should never called again.
$event->getContext()->addExtension('ratepay', new ArrayStruct([
'onTransition' => true,
]));
$orderOperationData = new OrderOperationData($event->getContext(), $order, $operation, null, false);
try {
$response = $service->doRequest($orderOperationData);
if (!$response->getResponse()->isSuccessful()) {
$this->logger->error('Error during bidirectionality. (Exception: ' . $response->getResponse()->getReasonMessage() . ')', [
'order' => $order->getId(),
'transactionId' => $ratepayData->getTransactionId(),
'orderNumber' => $order->getOrderNumber(),
'itemsToProcess' => $orderOperationData->getItems(),
]);
}
} catch (Exception $exception) {
$this->logger->critical('Exception during bidirectionality. (Exception: ' . $exception->getMessage() . ')', [
'order' => $order->getId(),
'transactionId' => $ratepayData->getTransactionId(),
'orderNumber' => $order->getOrderNumber(),
'itemsToProcess' => $orderOperationData->getItems(),
]);
}
$event->getContext()->removeExtension('ratepay');
}
}