Skip to content

Commit 18e355b

Browse files
committed
Shopware 5.3 compatible version
1 parent d0697b1 commit 18e355b

File tree

12 files changed

+87
-84
lines changed

12 files changed

+87
-84
lines changed

Components/MonduApi/Service/MonduClient.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Shopware\Components\HttpClient\RequestException;
88
use Shopware\Components\HttpClient\GuzzleHttpClient;
99
use Shopware\Components\HttpClient\HttpClientInterface;
10-
use Shopware\Components\Logger;
10+
use Monolog\Logger;
1111

1212
class MonduClient {
1313
private $config;
@@ -54,15 +54,15 @@ public function createOrder($order, $returnUrl, $cancelUrl, $declineUrl, $paymen
5454
* @return array|null
5555
* @throws RequestException
5656
*/
57-
public function confirmMonduOrder($orderUid): ?array {
57+
public function confirmMonduOrder($orderUid) {
5858
$response = $this->sendRequest('post', 'orders/'. $orderUid . '/confirm', null, 'CONFIRM_ORDER');
5959
return $response['order'] ?? null;
6060
}
6161

6262
/**
6363
* @throws RequestException
6464
*/
65-
public function getMonduOrder($orderUid): ?array {
65+
public function getMonduOrder($orderUid) {
6666
$response = $this->sendRequest('get', 'orders/' . $orderUid, null, 'GET_ORDER');
6767
return $response['order'] ?? null;
6868
}
@@ -77,7 +77,7 @@ public function getOrders() {
7777
/**
7878
* @throws RequestException
7979
*/
80-
public function cancelOrder($orderUid): ?string {
80+
public function cancelOrder($orderUid) {
8181
$response = $this->sendRequest('post', 'orders/' . $orderUid .'/cancel', null, 'CANCEL_ORDER');
8282

8383
return $response['order']['state'] ?? null;

Controllers/Frontend/Mondu.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ public function directAction()
6767
$mapping = array_flip(PaymentMethods::MAPPING);
6868
$paymentMethod = $mapping[$this->getPaymentShortName()];
6969

70-
[ $checkoutUrl ] = $this->sessionService->createCheckoutSession(
70+
$data = $this->sessionService->createCheckoutSession(
7171
$this->getReturnUrl($this->persistBasket()),
7272
$this->getCancelUrl(),
7373
$this->getDeclineUrl(),
7474
$paymentMethod
7575
);
7676

77+
$checkoutUrl = $data[0];
78+
7779
if(!$checkoutUrl) {
7880
$this->handleError('Mondu: Error during checkout, please try again');
7981
}
@@ -134,10 +136,9 @@ public function returnAction()
134136
);
135137
}
136138

137-
138139
$repo = $this->getModelManager()->getRepository(Order::class);
139140
$order = $repo->findOneBy(['number' => $orderNumber]);
140-
$monduOrder = $this->orderHelper->updateExternalInfoOrder($order);
141+
$monduOrder = $this->orderHelper->updateExternalInfoOrder($order, (string) $orderNumber);
141142

142143
$this->updateShopwareOrder($order,
143144
[

Controllers/Frontend/MonduWebhook.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ public function executeAction() {
8686
try {
8787
$this->webhookService->getWebhookHandler($topic)->invoke($webhookData);
8888
$this->response->setBody(\json_encode(['error' => 0, 'message' => 'ok']));
89-
} catch (OrderNotFoundException| RuntimeException $e) {
89+
} catch (OrderNotFoundException $e) {
90+
$this->responseWithError(404, 'Order not found');
91+
return;
92+
} catch (RuntimeException $e) {
9093
$this->responseWithError(404, 'Order not found');
9194
return;
9295
}
@@ -108,7 +111,7 @@ private function validateSignature(Enlight_Controller_Request_RequestHttp $reque
108111
* @return void
109112
*/
110113
private function responseWithError($code = 400, $message = 'Bad Request') {
111-
$this->response->setStatusCode($code);
114+
$this->response->setHttpResponseCode($code);
112115
$this->response->setBody(json_encode(
113116
[
114117
'error' => 1,

Helpers/ModuleHelper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public function getModuleVersion() {
2222
}
2323

2424
public function getShopVersion() {
25-
$shopwareRelease = Shopware()->Container()->getParameter('shopware.release.version');
26-
return $shopwareRelease;
25+
return \Shopware::VERSION;
2726
}
2827
}

Helpers/OrderHelper.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ class OrderHelper
4646
public function __construct(
4747
ModelManager $modelManager,
4848
DocumentHelper $documentHelper,
49-
Logger $logger,
5049
ConfigService $configService,
5150
CartHelper $cartHelper,
5251
CustomerHelper $customerHelper
5352
) {
5453
$this->modelManager = $modelManager;
5554
$this->documentHelper = $documentHelper;
56-
$this->logger = $logger;
55+
56+
$this->logger = Shopware()->Container()->get('mond1_s_w_r5.logger');
5757
$this->configService = $configService;
5858
$this->cartHelper = $cartHelper;
5959
$this->customerHelper = $customerHelper;
@@ -163,13 +163,13 @@ public function updateOrder($order) {
163163
return $newOrder;
164164
}
165165

166-
public function updateExternalInfoOrder($order) {
166+
public function updateExternalInfoOrder($order, $orderNumber = '') {
167167
/**
168168
* @var MonduClient
169169
*/
170170
$client = Shopware()->Container()->get(MonduClient::class);
171171
$updateOrderData = [
172-
'external_reference_id' => $order->getNumber()
172+
'external_reference_id' => $orderNumber ?: $order->getNumber()
173173
];
174174

175175
return $client->updateExternalInfoOrder($order->getTransactionId(), $updateOrderData);
@@ -179,7 +179,6 @@ public function getOrderFromOrderVariables($orderVariables) {
179179
$userData = $orderVariables['sUserData'];
180180
$basket = $orderVariables['sBasket'];
181181
$content = $basket['content'];
182-
183182
$totalAmount = $this->cartHelper->getTotalAmount($orderVariables['sBasket'], $orderVariables['sUserData']);
184183
$shippingAmount = $this->cartHelper->getShippingAmount($orderVariables['sBasket'], $orderVariables['sUserData']);
185184
$chargeVat = $this->customerHelper->chargeVat($orderVariables['sUserData']);
@@ -211,7 +210,9 @@ public function getOrderAdjustment($order) {
211210
if($detail->getPrice() > 0) {
212211
$lineitems = $this->getLineItemsFromDetail($detail, $order, $lineitems);
213212
} else {
214-
[$total, $net] = $this->getAmountsFromDetail($detail, $detail->getQuantity(), $order->getNet());
213+
$amounts = $this->getAmountsFromDetail($detail, $detail->getQuantity(), $order->getNet());
214+
$total = $amounts[0];
215+
$net = $amounts[1];
215216
$totalDiscount += round(abs($net) * 100);
216217
$totalDiscountGross += round(abs($total) * 100);
217218
}
@@ -249,7 +250,8 @@ public function getLineItems($content, $chargeVat): array
249250
{
250251
$lineItems = [];
251252
foreach ($content as $item) {
252-
if($item['amountNumeric'] <= 0) {
253+
$amountNumeric = (float) $item['price'];
254+
if($amountNumeric <= 0) {
253255
continue;
254256
}
255257

@@ -261,23 +263,25 @@ public function getLineItems($content, $chargeVat): array
261263
public function getTotalDiscount($content, $chargeVat) {
262264
$discount = 0;
263265
foreach ($content as $item) {
264-
if ($item['amountNumeric'] > 0) {
266+
$amountNumeric = (float) $item['priceNumeric'];
267+
if ($amountNumeric > 0) {
265268
continue;
266269
}
270+
267271
if ($chargeVat) {
268-
$amountNumeric = abs($item['amountWithTax'] ?? $item['amountNumeric']);
272+
$amountNumeric = abs($amountNumeric);
269273
} else {
270-
$amountNumeric = abs($item['amountnetNumeric']);
274+
$amountNumeric = abs((float) $item['netprice']);
271275
}
272276
$discount += round($amountNumeric * 100);
273277
}
274278
return $discount;
275279
}
276280

277281
public function getLineItem($item, $chargeVat) {
278-
$itemAmountNet = $item['netprice'];
279-
$totalAmountNet = $item['amountnetNumeric'];
280-
$taxAmount = str_replace(',', '.', $item['tax']);
282+
$itemAmountNet = (float) $item['netprice'];
283+
$totalAmountNet = (float) str_replace(',', '.', $item['amountnet']);
284+
$taxAmount = (float) str_replace(',', '.', $item['tax']);
281285

282286
return [
283287
'external_reference_id' => $item['ordernumber'],
@@ -407,7 +411,7 @@ private function getAmountsFromDetail($detail, $quantity, $net) {
407411
}
408412

409413
if ($net) {
410-
$amountGross = Shopware()->Container()->get('shopware.cart.net_rounding')->round($price, $taxValue, $quantity);
414+
$amountGross = $this->round($price, $taxValue, $quantity);
411415
return [$amountGross, $amount];
412416
} else {
413417
$amountNet = round(($price * $quantity) / (100 + $taxValue) * 100, 2);
@@ -423,8 +427,13 @@ private function getAmountsFromDetail($detail, $quantity, $net) {
423427
*/
424428
private function getLineItemsFromDetail($detail, $order, array $lineItems): array
425429
{
426-
[$totalAmount, $totalAmountNet] = $this->getAmountsFromDetail($detail, $detail->getQuantity(), $order->getNet());
427-
[$itemAmount, $itemAmountNet] = $this->getAmountsFromDetail($detail, 1, $order->getNet());
430+
$totalAmounts = $this->getAmountsFromDetail($detail, $detail->getQuantity(), $order->getNet());
431+
$totalAmount = $totalAmounts[0];
432+
$totalAmountNet = $totalAmounts[1];
433+
434+
$itemAmounts = $this->getAmountsFromDetail($detail, 1, $order->getNet());
435+
$itemAmount = $itemAmounts[0];
436+
$itemAmountNet = $itemAmounts[1];
428437
$chargeVat = !$order->getTaxFree();
429438
$lineItems[] = [
430439
'external_reference_id' => $detail->getArticleNumber(),
@@ -470,4 +479,16 @@ private function removeDuplicateSwReferenceIds(array $lineItems): array
470479
}
471480
return $newLineItems;
472481
}
482+
483+
private function round(float $netPrice, float $tax, int $quantity = null): float
484+
{
485+
if ($quantity === 0) {
486+
return 0.0;
487+
}
488+
489+
$netPrice = round(round($netPrice, 2) / 100 * (100 + $tax), 2);
490+
$netPrice = (\is_int($quantity) && $quantity !== 1) ? round($netPrice * $quantity, 2) : $netPrice;
491+
492+
return $netPrice;
493+
}
473494
}

Helpers/WebhookHelper.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,6 @@ public function registerWebhooksIfNotRegistered()
4040
{
4141
$webhooks = $this->monduClient->getWebhooks();
4242

43-
//fix for incorrect webhook url for versions < 1.0.11
44-
foreach ($webhooks as $webhook) {
45-
if (str_contains($webhook['address'], 'backend')) {
46-
$this->monduClient->deleteWebhook($webhook['uuid']);
47-
}
48-
}
49-
50-
$webhooks = array_filter($webhooks, function ($webhook) {
51-
if (str_contains($webhook['address'], 'backend'))
52-
return false;
53-
return true;
54-
});
55-
5643
$registeredTopics = array_map(function ($webhook) {
5744
return $webhook['topic'];
5845
}, $webhooks);

Resources/services.xml

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,54 @@
99
</imports>
1010

1111
<services>
12+
<service id="Mond1SWR5\Bootstrap\TranslationTransformer" class="Mond1SWR5\Bootstrap\TranslationTransformer" public="true"/>
1213

13-
<defaults public="true" autowire="true">
14-
<bind key="$logger" type="service" id="mond1_s_w_r5.logger" />
15-
<bind key="$configReader" type="service" id="shopware.plugin.cached_config_reader" />
16-
</defaults>
17-
<service id="Mond1SWR5\Bootstrap\TranslationTransformer" public="true"/>
18-
19-
<service id="Mond1SWR5\Services\SessionService" public="true">
20-
<argument id="session" type="service" key="$session"/>
14+
<service autowire="true" id="Mond1SWR5\Services\SessionService" class="Mond1SWR5\Services\SessionService" public="true">
2115
</service>
22-
<service id="Mond1SWR5\Helpers\DocumentHelper">
23-
<argument key="$router" id="router" type="service"/>
24-
<argument key="$modelManager" id="models" type="service"/>
16+
<service autowire="true" id="Mond1SWR5\Helpers\DocumentHelper" class="Mond1SWR5\Helpers\DocumentHelper">
2517
</service>
26-
<service id="Mond1SWR5\Components\PluginConfig\Service\ConfigService" public="true">
27-
<argument key="$router" id="router" type="service"/>
18+
<service autowire="true" id="Mond1SWR5\Components\PluginConfig\Service\ConfigService" class="Mond1SWR5\Components\PluginConfig\Service\ConfigService" public="true">
2819
</service>
29-
<service id="Mond1SWR5\Services\PaymentService" public="true"/>
30-
<service id="Mond1SWR5\Services\Webhook\WebhookService" public="true"/>
31-
<service id="Mond1SWR5\Services\PaymentStatusService" public="true"/>
32-
<service id="Mond1SWR5\Services\ShopwarePaymentService" public="true">
20+
<service id="Mond1SWR5\Services\PaymentService" class="Mond1SWR5\Services\PaymentService" public="true"/>
21+
<service id="Mond1SWR5\Services\Webhook\WebhookService" class="Mond1SWR5\Services\Webhook\WebhookService" public="true"/>
22+
<service autowire="true" id="Mond1SWR5\Services\PaymentStatusService" class="Mond1SWR5\Services\PaymentStatusService" public="true"/>
23+
<service id="Mond1SWR5\Services\ShopwarePaymentService" class="Mond1SWR5\Services\ShopwarePaymentService" public="true">
3324
<argument type="service" id="translation"/>
3425
</service>
35-
<service id="Mond1SWR5\Services\ShopwareShipmentService" public="true">
26+
<service id="Mond1SWR5\Services\ShopwareShipmentService" class="Mond1SWR5\Services\ShopwareShipmentService" public="true">
3627
<argument type="service" id="translation"/>
3728
</service>
38-
<service id="Mond1SWR5\Components\MonduApi\Service\MonduClient" public="true">
39-
<argument key="$logger" id="pluginlogger" />
29+
<service id="Mond1SWR5\Components\MonduApi\Service\MonduClient" class="Mond1SWR5\Components\MonduApi\Service\MonduClient" public="true">
30+
<argument type="service" id="Mond1SWR5\Components\PluginConfig\Service\ConfigService" />
31+
<argument type="service" id="guzzle_http_client_factory" />
32+
<argument type="service" id="mond1_s_w_r5.logger" />
33+
<argument type="service" id="Mond1SWR5\Helpers\ModuleHelper" />
4034
</service>
41-
<service id="Mond1SWR5\Helpers\OrderHelper" public="true"/>
42-
<service id="Mond1SWR5\Helpers\CartHelper" public="true"/>
43-
<service id="Mond1SWR5\Helpers\CustomerHelper" public="true"/>
44-
<service id="Mond1SWR5\Helpers\PaymentHelper" public="true"/>
45-
<service id="Mond1SWR5\Helpers\ModuleHelper" public="true"/>
46-
<service id="Mond1SWR5\Helpers\WebhookHelper" public="true">
35+
<service autowire="true" id="Mond1SWR5\Helpers\OrderHelper" class="Mond1SWR5\Helpers\OrderHelper" public="true"/>
36+
<service autowire="true" id="Mond1SWR5\Helpers\CartHelper" class="Mond1SWR5\Helpers\CartHelper" public="true"/>
37+
<service id="Mond1SWR5\Helpers\CustomerHelper" class="Mond1SWR5\Helpers\CustomerHelper" public="true"/>
38+
<service autowire="true" id="Mond1SWR5\Helpers\PaymentHelper" class="Mond1SWR5\Helpers\PaymentHelper" public="true"/>
39+
<service id="Mond1SWR5\Helpers\ModuleHelper" class="Mond1SWR5\Helpers\ModuleHelper" public="true"/>
40+
<service id="Mond1SWR5\Helpers\WebhookHelper" class="Mond1SWR5\Helpers\WebhookHelper" public="true">
4741
<argument type="service" id="Mond1SWR5\Components\MonduApi\Service\MonduClient" />
4842
<argument type="service" id="Mond1SWR5\Components\PluginConfig\Service\ConfigService" />
4943
</service>
5044

51-
<service id="Mond1SWR5\Subscriber\Cron\OrderStatusCron">
45+
<service id="Mond1SWR5\Subscriber\Cron\OrderStatusCron" class="Mond1SWR5\Subscriber\Cron\OrderStatusCron">
5246
<argument key="$modelManager" id="models" type="service"/>
5347
<argument key="$db" id="db" type="service"/>
5448
<tag name="shopware.event_subscriber"/>
5549
</service>
5650

57-
<service id="Mond1SWR5\Commands\ValidateCommand">
51+
<service id="Mond1SWR5\Commands\ValidateCommand" class="Mond1SWR5\Commands\ValidateCommand">
5852
<argument type="service" id="Mond1SWR5\Helpers\WebhookHelper"/>
5953
<tag name="console.command" command="sw:Mond1SWR5:validate"/>
6054
</service>
61-
<service id="Mond1SWR5\Commands\ActivatePaymentCommand">
55+
<service id="Mond1SWR5\Commands\ActivatePaymentCommand" class="Mond1SWR5\Commands\ActivatePaymentCommand">
6256
<argument type="service" id="Mond1SWR5\Services\ShopwarePaymentService"/>
6357
<tag name="console.command" command="sw:Mond1SWR5:activate:payment"/>
6458
</service>
65-
<service id="Mond1SWR5\Commands\ActivateShipmentCostCommand">
59+
<service id="Mond1SWR5\Commands\ActivateShipmentCostCommand" class="Mond1SWR5\Commands\ActivateShipmentCostCommand">
6660
<argument type="service" id="Mond1SWR5\Services\ShopwareShipmentService"/>
6761
<tag name="console.command" command="sw:Mond1SWR5:activate:shipment:cost"/>
6862
</service>

Resources/services/subscriber.xml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
55
<services>
6-
7-
<defaults public="true" autowire="true">
6+
<service autowire="true" id="Mond1SWR5\Subscriber\TemplateRegistration" class="Mond1SWR5\Subscriber\TemplateRegistration">
7+
<argument>%mond1_s_w_r5.plugin_dir%</argument>
88
<tag name="shopware.event_subscriber" />
9-
</defaults>
10-
11-
<service id="Mond1SWR5\Subscriber\TemplateRegistration">
12-
<argument key="$pluginDirectory">%mond1_s_w_r5.plugin_dir%</argument>
139
</service>
14-
<service id="Mond1SWR5\Subscriber\Frontend\PaymentFilterSubscriber" >
15-
<argument key="$session" id="session" type="service"/>
10+
<service autowire="true" id="Mond1SWR5\Subscriber\Frontend\PaymentFilterSubscriber" class="Mond1SWR5\Subscriber\Frontend\PaymentFilterSubscriber" >
11+
<tag name="shopware.event_subscriber" />
1612
</service>
17-
<service id="Mond1SWR5\Subscriber\OrderSubscriber">
18-
<argument key="$pluginDirectory">%mond1_s_w_r5.plugin_dir%</argument>
13+
<service autowire="true" id="Mond1SWR5\Subscriber\OrderSubscriber" class="Mond1SWR5\Subscriber\OrderSubscriber">
14+
<argument>%mond1_s_w_r5.plugin_dir%</argument>
15+
<tag name="shopware.event_subscriber" />
1916
</service>
2017
</services>
2118
</container>

Services/SessionService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ private function reserveOrderNumber() {
5454

5555
public function getOrder() {
5656
$orderVariables = Shopware()->Session()->get('sOrderVariables');
57+
5758
return $this->orderHelper->getOrderFromOrderVariables($orderVariables);
5859
}
5960

Services/ShopwareShipmentService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function updateShippingCost($shippingCost)
181181
Shopware()->Container()->get(ModelManager::class)->flush();
182182
$shippingCost['id'] = $dispatchModel->getId();
183183
} catch (Exception $e) {
184-
$message = "Failed to update {$shippingCost["name"]}, error message:" + $e->getMessage();
184+
$message = "Failed to update {$shippingCost["name"]}, error message:" . $e->getMessage();
185185
}
186186
return $message;
187187
}

0 commit comments

Comments
 (0)