Skip to content

Commit

Permalink
Add a basic form validation for the TpayGatewayConfiguration (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel authored Oct 30, 2024
2 parents 2f813f0 + a9e9011 commit a22da86
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
4 changes: 0 additions & 4 deletions config/services/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use CommerceWeavers\SyliusTpayPlugin\Form\DataTransformer\VisaMobilePhoneDataTransformer;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\DecryptGatewayConfigListener;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\EncryptGatewayConfigListener;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\PreventSavingEmptyPasswordFieldsListener;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\RemoveUnnecessaryPaymentDetailsFieldsListener;
use CommerceWeavers\SyliusTpayPlugin\Form\Extension\CompleteTypeExtension;
use CommerceWeavers\SyliusTpayPlugin\Form\Extension\PaymentTypeExtension;
Expand All @@ -32,7 +31,6 @@
->args([
service('commerce_weavers_sylius_tpay.form.event_listener.decrypt_gateway_config'),
service('commerce_weavers_sylius_tpay.form.event_listener.encrypt_gateway_config'),
service('commerce_weavers_sylius_tpay.form.event_listener.prevent_saving_empty_password_fields'),
])
->tag(
'sylius.gateway_configuration_type',
Expand Down Expand Up @@ -71,7 +69,5 @@
])
;

$services->set('commerce_weavers_sylius_tpay.form.event_listener.prevent_saving_empty_password_fields', PreventSavingEmptyPasswordFieldsListener::class);

$services->set('commerce_weavers_sylius_tpay.form.event_listener.remove_unnecessary_payment_details_fields', RemoveUnnecessaryPaymentDetailsFieldsListener::class);
};
3 changes: 2 additions & 1 deletion config/services/refunding.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Refunding\Checker\RefundPluginAvailabilityChecker;
use CommerceWeavers\SyliusTpayPlugin\Refunding\Checker\RefundPluginAvailabilityCheckerInterface;
use CommerceWeavers\SyliusTpayPlugin\Refunding\Dispatcher\RefundDispatcher;
use CommerceWeavers\SyliusTpayPlugin\Refunding\Dispatcher\RefundDispatcherInterface;
Expand All @@ -13,7 +14,7 @@
return function(ContainerConfigurator $container): void {
$services = $container->services();

$services->set('commerce_weavers_sylius_tpay.refunding.checker.refund_plugin_availability', RefundPluginAvailabilityCheckerInterface::class)
$services->set('commerce_weavers_sylius_tpay.refunding.checker.refund_plugin_availability', RefundPluginAvailabilityChecker::class)
->alias(RefundPluginAvailabilityCheckerInterface::class, 'commerce_weavers_sylius_tpay.refunding.checker.refund_plugin_availability')
;

Expand Down
37 changes: 25 additions & 12 deletions src/Form/Type/TpayGatewayConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@

use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\DecryptGatewayConfigListenerInterface;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\EncryptGatewayConfigListenerInterface;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\PreventSavingEmptyPasswordFieldsListener;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;

final class TpayGatewayConfigurationType extends AbstractType
{
public function __construct(
private readonly DecryptGatewayConfigListenerInterface $decryptGatewayConfigListener,
private readonly EncryptGatewayConfigListenerInterface $encryptGatewayConfigListener,
private readonly PreventSavingEmptyPasswordFieldsListener $preventSavingEmptyClientSecretListener,
) {
}

Expand All @@ -35,13 +34,21 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
TextType::class,
[
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.client_id',
'validation_groups' => ['sylius'],
'constraints' => [
new NotBlank(allowNull: false, groups: ['sylius']),
],
],
)
->add(
'client_secret',
PasswordType::class,
TextType::class,
[
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.client_secret',
'validation_groups' => ['sylius'],
'constraints' => [
new NotBlank(allowNull: false, groups: ['sylius']),
],
],
)
->add(
Expand Down Expand Up @@ -77,27 +84,29 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
],
)
->add(
'google_merchant_id',
'notification_security_code',
TextType::class,
[
'empty_data' => '',
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.google_merchant_id',
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.notification_security_code',
],
)
->add(
'apple_pay_merchant_id',
'google_merchant_id',
TextType::class,
[
'empty_data' => '',
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.apple_pay_merchant_id',
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.google_merchant_id',
'required' => false,
],
)
->add(
'notification_security_code',
PasswordType::class,
'apple_pay_merchant_id',
TextType::class,
[
'empty_data' => '',
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.notification_security_code',
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.apple_pay_merchant_id',
'required' => false,
],
)
->add(
Expand All @@ -115,6 +124,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

$builder->addEventListener(FormEvents::PRE_SET_DATA, $this->decryptGatewayConfigListener);
$builder->addEventListener(FormEvents::POST_SUBMIT, $this->encryptGatewayConfigListener);
$builder->addEventListener(FormEvents::PRE_SUBMIT, $this->preventSavingEmptyClientSecretListener);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('validation_groups', ['sylius']);
}
}
8 changes: 2 additions & 6 deletions src/Payum/Factory/TpayGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ protected function populateConfig(ArrayObject $config): void

$config['payum.api'] = function (ArrayObject $config): TpayApi {
/** @var array{client_id?: string, client_secret?: string, production_mode?: bool, notification_security_code?: string} $config */
$clientId = $config['client_id'] ?? null;
$clientSecret = $config['client_secret'] ?? null;
$clientId = $config['client_id'] ?? '';
$clientSecret = $config['client_secret'] ?? '';
$productionMode = $config['production_mode'] ?? false;
$notificationSecretCode = $config['notification_security_code'] ?? null;

if (null === $clientId || null === $clientSecret) {
throw new \InvalidArgumentException('Tpay ClientId and ClientSecret are required.');
}

return new TpayApi($clientId, $clientSecret, $productionMode, notificationSecretCode: $notificationSecretCode);
};
}
Expand Down

0 comments on commit a22da86

Please sign in to comment.