Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions Config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@
<form name="transfer.configure.bic" class="WireTransfer\Form\ConfigurationForm" />
</forms>

<hooks>
<hook id="wiretransfer.hook" class="WireTransfer\Hook\HookManager" scope="request">
<tag name="hook.event_listener" event="module.configuration" type="back" templates="render:module_configuration.html" />
<tag name="hook.event_listener" event="order-placed.additional-payment-info" type="front" method="onAdditionalPaymentInfo" />
</hook>
</hooks>
<!--
No <hooks> section: WireTransfer\Hook\Back\ConfigurationHook declares itself through
getSubscribedHooks(), which Thelia 3 auto-discovers. The front-office needs no hook —
Flexy has none, so the theme renders the bank details with the wiretransfer_bank_info()
Twig function.
-->

<services>
<service id="send.wiretransfer.mail" class="WireTransfer\Listener\SendPaymentConfirmationEmail" scope="request">
<argument type="service" id="thelia.parser" />
<argument type="service" id="mailer"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
<!--
No <services> section either. SendPaymentConfirmationEmail used to be declared here as
`send.wiretransfer.mail` ON TOP of being auto-discovered by configureServices(), which
registered the SAME class as an event subscriber TWICE (two distinct service ids, so
Symfony wires two listeners) — the customer received the confirmation email twice on
every switch to paid. Auto-discovery alone is authoritative: do not re-declare a class
that lives under the module namespace.
-->

</config>
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<descriptive locale="en_US">
<title>Wire transfer payment</title>
</descriptive>
<version>2.1.3</version>
<version>2.3.0</version>
<author>
<name>Thelia</name>
<email>info@thelia.net</email>
Expand Down
5 changes: 5 additions & 0 deletions Config/routing.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Route declared in XML only. Do NOT also add a #[Route] attribute on the controller:
Thelia 3 loads module Config/routing.xml IN ADDITION to scanning attributes, so the route
would be registered twice.
-->
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
Expand Down
2 changes: 2 additions & 0 deletions Constraints/BIC.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
/*************************************************************************************/


declare(strict_types=1);

namespace WireTransfer\Constraints;
use Symfony\Component\Validator\Constraint;

Expand Down
11 changes: 10 additions & 1 deletion Constraints/BICValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
/*************************************************************************************/


declare(strict_types=1);

namespace WireTransfer\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Thelia\Core\Translation\Translator;

/**
Expand All @@ -43,13 +46,19 @@ class BICValidator extends ConstraintValidator {
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof BIC) {
throw new UnexpectedTypeException($constraint, BIC::class);
}

if (null === $value || '' === $value) {
return;
}

$teststring = preg_replace('/\s+/', '', $value);

if(!preg_match("([a-zA-Z]{4}[a-zA-Z]{2}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?)", $teststring)) {
// A BIC is 8 or 11 characters: 6 letters + 2 alphanumerics, then an optional 3-char branch code.
// The pattern is anchored (^...$) so partial matches embedded in a longer string are rejected.
if(!preg_match('/^[a-zA-Z]{6}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?$/', $teststring)) {
$this->context->addViolation(
Translator::getInstance()->trans(
$constraint->message
Expand Down
22 changes: 14 additions & 8 deletions Controller/ConfigureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */

declare(strict_types=1);

namespace WireTransfer\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Core\Translation\Translator;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Tools\URL;
use WireTransfer\Form\ConfigurationForm;
Expand All @@ -46,7 +48,7 @@
*/
class ConfigureController extends BaseAdminController
{
public function configure(Request $request, Translator $translator)
public function configure(Request $request)
{
if (null !== $response = $this->checkAuth(AdminResources::MODULE, 'WireTransfer', AccessManager::UPDATE)) {
return $response;
Expand Down Expand Up @@ -87,18 +89,22 @@ public function configure(Request $request, Translator $translator)
$error_msg = $ex->getMessage();
}

// At this point, the form has errors, and should be redisplayed. We don not redirect,
// just redisplay the same template.
// Setup the Form error context, to make error information available in the template.
$this->setupFormErrorContext(
$translator->trans('Wire transfer configuration', [], WireTransfer::MESSAGE_DOMAIN),
$this->translator->trans('Wire transfer configuration', [], WireTransfer::MESSAGE_DOMAIN),
$error_msg,
$configurationForm,
$ex
);

// Do not redirect at this point, or the error context will be lost.
// Just redisplay the current template.
return $this->render('module-configure', ['module_code' => 'WireTransfer']);
// The configuration screen is rendered through a hook, so redirect back to the module
// configuration page. That redirect drops the ParserContext form error, hence the flash
// bag (rendered by the default-twig base template as app.flashes).
$session = $request->getSession();
if ($session instanceof FlashBagAwareSessionInterface) {
$session->getFlashBag()->add('danger', $error_msg);
}

return new RedirectResponse(URL::getInstance()->absoluteUrl('/admin/module/WireTransfer'));
}
}
17 changes: 11 additions & 6 deletions Form/ConfigurationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */

declare(strict_types=1);

namespace WireTransfer\Form;

use Symfony\Component\Form\Extension\Core\Type\TextareaType;
Expand All @@ -44,7 +46,10 @@
*/
class ConfigurationForm extends BaseForm
{
protected function trans($str, $params = [])
/**
* @param array<string, string|int|float> $params
*/
protected function trans(string $str, array $params = []): string
{
return Translator::getInstance()->trans($str, $params, WireTransfer::MESSAGE_DOMAIN);
}
Expand All @@ -58,7 +63,7 @@ protected function buildForm(): void
[
'constraints' => [new NotBlank()],
'required' => true,
'label' => Translator::getInstance()->trans('Account holder name', [], WireTransfer::MESSAGE_DOMAIN),
'label' => $this->trans('Account holder name'),
'data' => WireTransfer::getConfigValue('name', ''),
'label_attr' => [
'for' => 'namefield',
Expand All @@ -71,7 +76,7 @@ protected function buildForm(): void
[
'constraints' => [new NotBlank(), new Iban()],
'required' => true,
'label' => Translator::getInstance()->trans('IBAN (International Bank Account Number)', [], WireTransfer::MESSAGE_DOMAIN),
'label' => $this->trans('IBAN (International Bank Account Number)'),
'data' => WireTransfer::getConfigValue('iban', ''),
'label_attr' => [
'for' => 'ibanfield',
Expand All @@ -84,7 +89,7 @@ protected function buildForm(): void
[
'constraints' => [new NotBlank(), new BIC()],
'required' => true,
'label' => Translator::getInstance()->trans('BIC (Bank Identifier Code)', [], WireTransfer::MESSAGE_DOMAIN),
'label' => $this->trans('BIC (Bank Identifier Code)'),
'data' => WireTransfer::getConfigValue('bic', ''),
'label_attr' => [
'for' => 'bicfield',
Expand All @@ -96,7 +101,7 @@ protected function buildForm(): void
TextareaType::class,
[
'required' => false,
'label' => Translator::getInstance()->trans('Message displayed to your customer when order is placed', [], WireTransfer::MESSAGE_DOMAIN),
'label' => $this->trans('Message displayed to your customer when order is placed'),
'data' => WireTransfer::getConfigValue('message', ''),
'label_attr' => [
'for' => 'messagefield',
Expand All @@ -109,7 +114,7 @@ protected function buildForm(): void
/**
* @return string the name of you form. This name must be unique
*/
public static function getName()
public static function getName(): string
{
return 'configurewiretransfer';
}
Expand Down
62 changes: 62 additions & 0 deletions Hook/Back/ConfigurationHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/*************************************************************************************/

namespace WireTransfer\Hook\Back;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Thelia\Core\Event\Hook\HookRenderEvent;
use Thelia\Core\Form\TheliaFormFactory;
use Thelia\Core\Hook\BaseHook;
use Thelia\Core\Template\Parser\ParserResolver;
use WireTransfer\Form\ConfigurationForm;

/**
* Renders the WireTransfer configuration screen in the default-twig back-office.
*/
class ConfigurationHook extends BaseHook
{
public function __construct(
private readonly TheliaFormFactory $formFactory,
?EventDispatcherInterface $dispatcher = null,
?ParserResolver $parserResolver = null,
) {
parent::__construct($dispatcher, $parserResolver);
}

public static function getSubscribedHooks(): array
{
return [
'module.configuration' => [
['type' => 'back', 'method' => 'onModuleConfiguration'],
],
];
}

public function onModuleConfiguration(HookRenderEvent $event): void
{
if ('WireTransfer' !== $event->getArgument('modulecode')) {
return;
}

$event->add(
$this->render('WireTransfer/module-configuration.html.twig', [
'config_form' => $this->formFactory
->createForm(ConfigurationForm::getName(), FormType::class)
->createView()
->getView(),
])
);
}
}
34 changes: 0 additions & 34 deletions Hook/HookManager.php

This file was deleted.

3 changes: 3 additions & 0 deletions I18n/en_US.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
return array(
'Account holder name' => 'Account holder name',
'BIC (Bank Identifier Code)' => 'BIC (Bank Identifier Code)',
'BIC code' => 'BIC code',
'Bank information parameters have not been defined.' => 'Bank information parameters have not been defined.',
'IBAN' => 'IBAN',
'IBAN (International Bank Account Number)' => 'IBAN (International Bank Account Number)',
'Message displayed to your customer when order is placed' => 'Message displayed to your customer when order is placed',
'Wire transfer configuration' => 'Wire transfer configuration',
'You may now do a transfer to this bank account: ' => 'You may now do a transfer to this bank account: ',
);
3 changes: 3 additions & 0 deletions I18n/fr_FR.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
return array(
'Account holder name' => 'Titulaire du compte',
'BIC (Bank Identifier Code)' => 'BIC (Bank Identifier Code)',
'BIC code' => 'Code BIC',
'Bank information parameters have not been defined.' => 'Les paramètres bancaires n\'ont pas été définis',
'IBAN' => 'IBAN',
'IBAN (International Bank Account Number)' => 'IBAN (International Bank Account Number)',
'Message displayed to your customer when order is placed' => 'Message affiché à vos clients une fois leur commande payée',
'Wire transfer configuration' => 'Informations de virement',
'You may now do a transfer to this bank account: ' => 'Merci de virer le montant de votre commande sur le compte suivant :',
);
9 changes: 0 additions & 9 deletions I18n/frontOffice/default/en_US.php

This file was deleted.

9 changes: 0 additions & 9 deletions I18n/frontOffice/default/fr_FR.php

This file was deleted.

Loading