-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductPanel.php
More file actions
239 lines (206 loc) · 10.1 KB
/
ProductPanel.php
File metadata and controls
239 lines (206 loc) · 10.1 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?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\OrderManagement\Controller;
use Ratepay\RpayPayments\Components\OrderManagement\Service\LineItemFactory;
use Ratepay\RpayPayments\Components\RatepayApi\Dto\AddCreditData;
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\PaymentCreditService;
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\Extension\OrderLineItemExtension;
use Ratepay\RpayPayments\Core\Entity\RatepayOrderDataEntity;
use Ratepay\RpayPayments\Core\Entity\RatepayOrderLineItemDataEntity;
use Ratepay\RpayPayments\Core\Util\LineItemUtil;
use Ratepay\RpayPayments\Util\CriteriaHelper;
use RuntimeException;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Routing\Exception\InvalidRequestParameterException;
use Shopware\Core\Framework\Routing\RoutingException;
use Shopware\Core\Framework\Validation\DataValidationDefinition;
use Shopware\Core\Framework\Validation\DataValidator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\AtLeastOneOf;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\LessThan;
use Symfony\Component\Validator\Constraints\NotBlank;
#[Route(
path: '/api/_action/order/{orderId}/ratepay/',
defaults: [
'_routeScope' => ['administration'],
]
)]
class ProductPanel extends AbstractController
{
/**
* @var AbstractModifyRequest[]
*/
private array $requestServicesByOperation = [];
public function __construct(
private readonly EntityRepository $orderRepository,
PaymentDeliverService $paymentDeliverService,
PaymentReturnService $paymentReturnService,
PaymentCancelService $paymentCancelService,
private readonly PaymentCreditService $creditService,
private readonly LineItemFactory $lineItemFactory,
private readonly DataValidator $dataValidator
) {
$this->requestServicesByOperation = [
OrderOperationData::OPERATION_DELIVER => $paymentDeliverService,
OrderOperationData::OPERATION_CANCEL => $paymentCancelService,
OrderOperationData::OPERATION_RETURN => $paymentReturnService,
];
}
#[Route(path: 'info', name: 'ratepay.order_management.product_panel.load', methods: ['GET'])]
public function load(string $orderId, Context $context): JsonResponse
{
$criteria = new Criteria([$orderId]);
$criteria->addAssociation(OrderExtension::EXTENSION_NAME);
$criteria->addAssociation('lineItems');
$criteria->addAssociation('lineItems.' . OrderLineItemExtension::EXTENSION_NAME);
$order = $this->orderRepository->search($criteria, $context)->first();
if ($order instanceof OrderEntity) {
$items = [];
foreach ($order->getLineItems() as $lineItem) {
$extension = $lineItem->getExtension(OrderLineItemExtension::EXTENSION_NAME);
if ($extension instanceof RatepayOrderLineItemDataEntity) {
$items[$lineItem->getId()] = [
'id' => $lineItem->getId(),
'name' => $lineItem->getLabel(),
'ordered' => $lineItem->getQuantity(),
'unitPrice' => $lineItem->getUnitPrice(),
'totalPrice' => $lineItem->getTotalPrice(),
'position' => LineItemUtil::addMaxActionValues(
$extension->getPosition(),
$lineItem->getQuantity()
),
];
}
}
$orderExtension = $order->getExtension(OrderExtension::EXTENSION_NAME);
if ($orderExtension instanceof RatepayOrderDataEntity && $orderExtension->getShippingPosition()) {
$items[OrderOperationData::ITEM_ID_SHIPPING] = [
'id' => OrderOperationData::ITEM_ID_SHIPPING,
'name' => 'shipping',
'ordered' => 1,
'unitPrice' => $order->getShippingTotal(),
'totalPrice' => $order->getShippingTotal(),
'position' => LineItemUtil::addMaxActionValues($orderExtension->getShippingPosition(), 1),
];
}
return $this->json([
'success' => true,
'data' => array_values($items),
], 200);
}
return $this->json([
'success' => false,
'message' => 'Order not found',
], 404);
}
#[Route(path: 'deliver', name: 'ratepay.order_management.product_panel.deliver', methods: ['POST'])]
public function deliver(string $orderId, Request $request, Context $context): Response
{
return $this->processModify($request, $context, OrderOperationData::OPERATION_DELIVER, $orderId);
}
#[Route(path: 'cancel', name: 'ratepay.order_management.product_panel.cancel', methods: ['POST'])]
public function cancel(string $orderId, Request $request, Context $context): Response
{
return $this->processModify($request, $context, OrderOperationData::OPERATION_CANCEL, $orderId);
}
#[Route(path: 'refund', name: 'ratepay.order_management.product_panel.return', methods: ['POST'])]
public function return(string $orderId, Request $request, Context $context): Response
{
return $this->processModify($request, $context, OrderOperationData::OPERATION_RETURN, $orderId);
}
#[Route(path: 'addItem', name: 'ratepay.order_management.product_panel.add_item', methods: ['POST'])]
public function addItem(string $orderId, Request $request, Context $context): Response
{
$order = $this->fetchOrder($context, $orderId);
if (!$order instanceof OrderEntity) {
throw $this->createNotFoundException('Order was not found');
}
// validate provided data
$definition = new DataValidationDefinition();
$definition->add('name', new NotBlank());
$definition->add('grossAmount', new NotBlank(), new AtLeastOneOf([new GreaterThan(0), new LessThan(0)]));
$definition->add('taxId', new NotBlank());
$this->dataValidator->validate($request->request->all(), $definition);
$lineItem = $this->lineItemFactory->createLineItem(
$order,
(string) $request->request->get('name'),
(float) (string) $request->request->get('grossAmount'),
(string) $request->request->get('taxId'),
$context
);
$response = $this->creditService->doRequest(new AddCreditData($context, $order, [$lineItem]));
if (!$response->getResponse()->isSuccessful()) {
return $this->json([
'success' => false,
'message' => $response->getResponse()->getReasonMessage(),
], 500);
}
return new Response(null, Response::HTTP_NO_CONTENT);
}
protected function processModify(Request $request, Context $context, string $operation, string $orderId): Response
{
$order = $this->fetchOrder($context, $orderId);
if (!$order instanceof OrderEntity) {
return $this->json([
'success' => false,
'message' => 'Order not found',
], 400);
}
$params = $request->request->all();
if (!isset($params['items']) || !is_array($params['items'])) {
if (class_exists(RoutingException::class) && method_exists(RoutingException::class, 'invalidRequestParameter')) {
throw RoutingException::invalidRequestParameter('items');
} elseif (class_exists(InvalidRequestParameterException::class)) {
// required for shopware == 6.5.0.0
throw new InvalidRequestParameterException('items');
}
// should never occur - just to be safe
throw new RuntimeException('Invalid request parameter: items');
}
$items = [];
foreach ($params['items'] as $data) {
$items[$data['id']] = (int) $data['quantity'];
}
$items = array_filter($items, static fn ($quantity): bool => $quantity > 0);
if ($items === []) {
return $this->json([
'success' => false,
'message' => 'Please provide at least on item', // todo translation - should we translate it?
], 400); // todo is this status OK ?
}
$response = $this->requestServicesByOperation[$operation]->doRequest(
new OrderOperationData($context, $order, $operation, $items, (bool) $request->request->get('updateStock'))
);
if (!$response->getResponse()->isSuccessful()) {
return $this->json([
'success' => false,
'message' => $response->getResponse()->getReasonMessage(),
], 500);
}
return new Response(null, Response::HTTP_NO_CONTENT);
}
protected function fetchOrder(Context $context, string $orderId): ?OrderEntity
{
return $this->orderRepository->search(CriteriaHelper::getCriteriaForOrder($orderId), $context)->first();
}
}