Skip to content

Commit 06201c4

Browse files
authored
fix: translate iap errors (#123)
1 parent 684b2af commit 06201c4

File tree

9 files changed

+82
-41
lines changed

9 files changed

+82
-41
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace SwagExtensionStore\Exception;
4+
5+
use GuzzleHttp\Exception\ClientException;
6+
use Shopware\Core\Framework\Log\Package;
7+
use Shopware\Core\Framework\Store\Exception\StoreApiException;
8+
9+
#[Package('checkout')]
10+
class ExtensionStoreApiException extends StoreApiException
11+
{
12+
protected string $apiCode;
13+
14+
public function __construct(ClientException $exception)
15+
{
16+
$data = json_decode($exception->getResponse()->getBody()->getContents(), true);
17+
18+
parent::__construct($exception);
19+
20+
$this->apiCode = $data['code'] ?? '';
21+
}
22+
23+
public function getErrors(bool $withTrace = false): \Generator
24+
{
25+
$errors = parent::getErrors($withTrace);
26+
27+
foreach ($errors as $error) {
28+
yield [
29+
...$error,
30+
'apiCode' => $this->apiCode,
31+
];
32+
}
33+
}
34+
}

src/Exception/ExtensionStoreException.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
use Shopware\Core\Framework\Log\Package;
1010
use Shopware\Core\Framework\Store\Exception\InvalidExtensionIdException;
1111
use Shopware\Core\Framework\Store\Exception\InvalidVariantIdException;
12-
use Shopware\Core\Framework\Store\Exception\StoreApiException;
1312
use Symfony\Component\HttpFoundation\Response;
1413

1514
#[Package('checkout')]
1615
class ExtensionStoreException extends HttpException
1716
{
18-
public static function createStoreApiExceptionFromClientError(ClientException $clientException): StoreApiException
17+
public static function createStoreApiExceptionFromClientError(ClientException $clientException): ExtensionStoreApiException
1918
{
20-
return new StoreApiException($clientException);
19+
return new ExtensionStoreApiException($clientException);
2120
}
2221

2322
public static function invalidExtensionId(): InvalidExtensionIdException

src/Resources/app/administration/src/global.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ declare global {
99
inAppPurchasesService: InAppPurchasesService;
1010
}
1111

12-
type ErrorResponse = AxiosError<{ errors: Array<ShopwareHttpError> }>;
12+
type ErrorResponse = AxiosError<{ errors: Array<ShopwareHttpError & { apiCode: string }> }>;
1313
}

src/Resources/app/administration/src/module/sw-in-app-purchases/component/sw-in-app-purchase-checkout-state/index.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ export default Shopware.Component.wrapComponentConfig({
1919
}
2020
},
2121

22-
data() {
23-
return {
24-
allowedErrors: [
25-
'The requested in-app feature has already been purchased',
26-
'Das angefragte In-App Feature wurde bereits erworben'
27-
]
28-
};
29-
},
30-
3122
computed: {
3223
classes() {
3324
return {
@@ -71,28 +62,15 @@ export default Shopware.Component.wrapComponentConfig({
7162
},
7263

7364
errorSnippet(): string {
74-
// if snippet is null, return the default error message
7565
if (!this.error) {
7666
return this.$t('sw-in-app-purchase-checkout-state.errorSubtitle');
7767
}
7868

79-
const slugifiedSnippet = this.error
80-
.toLowerCase()
81-
.replace(/[^a-zA-Z0-9_ -]/g, '') // remove all non-alphanumeric characters except underscores and spaces
82-
.replace(/[\s_]+/g, '-'); // replace spaces and underscores with hyphens
83-
84-
// if snippet slug exists in translation file, it comes from the extension store and must be translated
85-
if (this.$te(`sw-in-app-purchase-checkout-state.errors.${slugifiedSnippet}`)) {
86-
return this.$t(`sw-in-app-purchase-checkout-state.errors.${slugifiedSnippet}`);
87-
}
88-
89-
// if it does not exist in the allowedErrors return the default error message
90-
if (!this.allowedErrors.includes(this.error)) {
91-
return this.$t('sw-in-app-purchase-checkout-state.errorSubtitle');
69+
if (this.$te(`sw-in-app-purchase-checkout-state.errors.${this.error}`)) {
70+
return this.$t(`sw-in-app-purchase-checkout-state.errors.${this.error}`);
9271
}
9372

94-
// if it exists in the allowedErrors it comes from SBP and is already translated
95-
return this.error;
73+
return this.$t('sw-in-app-purchase-checkout-state.errorSubtitle');
9674
}
9775
}
9876
});

src/Resources/app/administration/src/module/sw-in-app-purchases/component/sw-in-app-purchase-checkout-state/sw-in-app-purchase-checkout-state.spec.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ describe('sw-in-app-purchase-checkout-state', () => {
6969
});
7070

7171
it('should compute subtitle correctly', async () => {
72-
// error comes from SBP
73-
wrapper = await createWrapper({ state: 'error', error: 'The requested in-app feature has already been purchased' });
74-
expect(wrapper.vm.subtitle).toBe('The requested in-app feature has already been purchased');
75-
76-
// error comes from ExtensionStore
77-
wrapper = await createWrapper({ state: 'error', error: 'This-error_exists.' });
78-
expect(wrapper.vm.subtitle).toBe(wrapper.vm.$t('sw-in-app-purchase-checkout-state.errors.this-error-exists'));
79-
8072
// error not found in SBP or allowed
8173
wrapper = await createWrapper({ state: 'error', error: 'error is not allowed' });
8274
expect(wrapper.vm.subtitle).toBe(wrapper.vm.$t('sw-in-app-purchase-checkout-state.errorSubtitle'));

src/Resources/app/administration/src/module/sw-in-app-purchases/component/sw-in-app-purchase-checkout/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ export default Shopware.Component.wrapComponentConfig({
170170
},
171171

172172
getError(errorResponse: ErrorResponse): string | null {
173-
return errorResponse?.response?.data.errors[0]?.detail ?? null;
173+
return errorResponse?.response?.data.errors[0]?.apiCode ?? null;
174174
},
175175

176176
reset() {

src/Resources/app/administration/src/module/sw-in-app-purchases/component/sw-in-app-purchase-checkout/sw-in-app-purchase-checkout.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<sw-in-app-purchase-checkout-state
3737
v-if="['loading', 'success', 'error'].includes(state)"
3838
:state="state"
39-
:error-snippet="errorMessage"
39+
:error="errorMessage"
4040
/>
4141

4242
<template #modal-footer>

src/Resources/app/administration/src/module/sw-in-app-purchases/snippet/de-DE.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,26 @@
2323
"successTitle": "In-App Kauf war erfolgreich!",
2424
"successSubtitle": "Du kannst jetzt dein neues Feature erleben.",
2525
"errors": {
26-
"the-extension-provider-disallowed-your-purchase-please-contact-the-extension-provider": "Der Anbieter der Erweiterung hat Ihren Kauf abgelehnt. Bitte kontaktieren Sie den Anbieter."
26+
"ShopwarePlatformException-87": "Ungültiges In-App-Feature",
27+
"ShopwarePlatformException-88": "In-App-Feature nicht gefunden",
28+
"ShopwarePlatformException-89": "In-App-Feature bereits gekauft",
29+
"ShopwarePlatformException-90": "In-App-Feature nicht kaufbar",
30+
"ShopwarePlatformException-91": "Änderung des In-App-Feature-Abos nicht erlaubt",
31+
"ShopwarePlatformException-92": "In-App-Feature bereits zur Herabstufung vorgemerkt",
32+
"ShopwarePlatformException-93": "Ungültige Bestellung",
33+
"ShopwarePlatformException-94": "Unzureichendes Guthaben",
34+
"ShopwarePlatformException-95": "AGB nicht akzeptiert",
35+
"ShopwarePlatformException-96": "In-App-Lizenz nicht gefunden",
36+
"CloudAdminException-42": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-87",
37+
"CloudAdminException-43": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-88",
38+
"CloudAdminException-44": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-89",
39+
"CloudAdminException-45": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-90",
40+
"CloudAdminException-46": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-93",
41+
"CloudAdminException-49": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-91",
42+
"CloudAdminException-47": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-94",
43+
"CloudAdminException-48": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-95",
44+
"CloudAdminException-50": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-96",
45+
"CloudAdminException-51": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-92"
2746
}
2847
},
2948
"sw-in-app-purchase-price-box": {

src/Resources/app/administration/src/module/sw-in-app-purchases/snippet/en-GB.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,26 @@
2323
"successTitle": "In-App Purchase was successful!",
2424
"successSubtitle": "You can now experience your new feature.",
2525
"errors": {
26-
"the-extension-provider-disallowed-your-purchase-please-contact-the-extension-provider": "The extension provider disallowed your purchase. Please contact the extension provider."
26+
"ShopwarePlatformException-87": "Invalid in-app feature",
27+
"ShopwarePlatformException-88": "In-app feature not found",
28+
"ShopwarePlatformException-89": "In-app feature already purchased",
29+
"ShopwarePlatformException-90": "In-app feature not purchasable",
30+
"ShopwarePlatformException-91": "In-app feature subscription change not allowed",
31+
"ShopwarePlatformException-92": "In-app feature already pending downgrade",
32+
"ShopwarePlatformException-93": "Invalid order",
33+
"ShopwarePlatformException-94": "Insufficient balance",
34+
"ShopwarePlatformException-95": "GTC not accepted",
35+
"ShopwarePlatformException-96": "In-app license not found",
36+
"CloudAdminException-42": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-87",
37+
"CloudAdminException-43": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-88",
38+
"CloudAdminException-44": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-89",
39+
"CloudAdminException-45": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-90",
40+
"CloudAdminException-46": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-93",
41+
"CloudAdminException-49": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-91",
42+
"CloudAdminException-47": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-94",
43+
"CloudAdminException-48": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-95",
44+
"CloudAdminException-50": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-96",
45+
"CloudAdminException-51": "@:sw-in-app-purchase-checkout-state.errors.ShopwarePlatformException-92"
2746
}
2847
},
2948
"sw-in-app-purchase-price-box": {

0 commit comments

Comments
 (0)