Skip to content

Commit

Permalink
Add locale based translation for tpay connection testing messages (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel authored Jan 16, 2025
2 parents ea783a2 + f16806d commit cc18f28
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
39 changes: 32 additions & 7 deletions assets/admin/js/test_payment_method_connection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
document.addEventListener('DOMContentLoaded', () => {
const testConnectionButton = document.getElementById('test-connection-button');
const testConnectionMessage = document.getElementById('test-connection-message');
let localeCode = '';

if (testConnectionButton === null || testConnectionMessage === null) {
return;
Expand All @@ -23,29 +24,53 @@ document.addEventListener('DOMContentLoaded', () => {
},
})
.then(response => {
const jsonResponse = response.json();
return response.json().then(jsonResponse => {
localeCode = response.headers.get('Content-Language');

if (!response.ok) {
throw new Error();
}
if (!response.ok) {
throw new Error();
}

return jsonResponse;
return jsonResponse;
});
})
.then(data => {
convertTpayChannelIdInputIntoSelect(data);

testConnectionMessage.innerText = 'Connection test successful. Channels loaded.';
testConnectionMessage.innerText = getNotificationMessage(localeCode, 'success');
testConnectionMessage.classList.remove('negative');
testConnectionMessage.classList.add('positive');
})
.catch(() => {
testConnectionMessage.innerText = 'Connection test failed. Please check your credentials and try again.';
testConnectionMessage.innerText = getNotificationMessage(localeCode, 'error');
testConnectionMessage.classList.remove('positive');
testConnectionMessage.classList.add('negative');
})
});
});

const NOTIFICATION_MESSAGES = {
en: {
connectionTest: {
success: 'Connection test successful. Channels loaded.',
error: 'Connection test failed. Please check your credentials and try again.'
}
},
pl: {
connectionTest: {
success: 'Test połączenia powiódł się. Kanały załadowane.',
error: 'Test połączenia nie powiódł się. Sprawdź swoje dane uwierzytelniające i spróbuj ponownie.'
}
}
};

function getNotificationMessage(localeCode, type = 'success', messageKey = 'connectionTest') {
const locale = localeCode.startsWith('pl') ? 'pl' : 'en';
const messages = NOTIFICATION_MESSAGES[locale]?.[messageKey];

return messages?.[type] || messages?.success || NOTIFICATION_MESSAGES.en[messageKey].success;
}

function convertTpayChannelIdInputIntoSelect(channels) {
let tpayChannelIdFormType = document.getElementsByName('sylius_payment_method[gatewayConfig][config][tpay_channel_id]')[0];
if (tpayChannelIdFormType === undefined) {
Expand Down
8 changes: 8 additions & 0 deletions config/services/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use CommerceWeavers\SyliusTpayPlugin\Controller\DisplayWaitingForPaymentPage;
use CommerceWeavers\SyliusTpayPlugin\Controller\PaymentNotificationAction;
use CommerceWeavers\SyliusTpayPlugin\Controller\RetryPaymentAction;
use CommerceWeavers\SyliusTpayPlugin\Controller\TpayGetChannelsAction;
use CommerceWeavers\SyliusTpayPlugin\Controller\TpayNotificationAction;

return function(ContainerConfigurator $container): void {
Expand Down Expand Up @@ -69,4 +70,11 @@
])
->tag('controller.service_arguments')
;

$services->set(TpayGetChannelsAction::class)
->args([
service('sylius.context.locale'),
])
->tag('controller.service_arguments')
;
};
14 changes: 12 additions & 2 deletions src/Controller/TpayGetChannelsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CommerceWeavers\SyliusTpayPlugin\Controller;

use Sylius\Component\Locale\Context\LocaleContextInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -12,6 +13,11 @@

final class TpayGetChannelsAction
{
public function __construct(
private readonly LocaleContextInterface $localeContext,
) {
}

public function __invoke(Request $request): Response
{
$tpayApi = new TpayApi(
Expand All @@ -20,17 +26,21 @@ public function __invoke(Request $request): Response
$request->query->getBoolean('productionMode', true),
);

$localeCode = $this->localeContext->getLocaleCode();

try {
$tpayResponse = $tpayApi->transactions()->getChannels();
} catch (TpayException $e) {
return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_UNAUTHORIZED);
return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_UNAUTHORIZED, ['Content-Language' => $localeCode]);
}

$channels = [];
foreach ($tpayResponse['channels'] as $channel) {
$channels[$channel['id']] = $channel['name'];
}

return new JsonResponse($channels);
return new JsonResponse($channels, headers: [
'Content-Language' => $localeCode,
]);
}
}

0 comments on commit cc18f28

Please sign in to comment.