Skip to content

Commit ca4b0ce

Browse files
[Admin][Fix] Allow creating sandbox paypal payment method
1 parent 6e832af commit ca4b0ce

File tree

5 files changed

+49
-30
lines changed

5 files changed

+49
-30
lines changed

src/DependencyInjection/SyliusPayPalExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ private function setCommunicationParameters(ContainerBuilder $container, array $
8484
{
8585
$container->setParameter('sylius.paypal.logging.increased', (bool) $config['logging']['increased']);
8686
$container->setParameter('sylius_paypal.logging.increased', $container->getParameter('sylius.paypal.logging.increased'));
87+
$container->setParameter('sylius_paypal.sandbox', (bool) $config['sandbox']);
8788

88-
if ($config['sandbox']) {
89+
if ($container->getParameter('sylius_paypal.sandbox')) {
8990
$container->setParameter('sylius.pay_pal.facilitator_url', 'https://paypal.sylius.com');
9091
$container->setParameter('sylius.pay_pal.api_base_url', 'https://api.sandbox.paypal.com/');
9192
$container->setParameter('sylius.pay_pal.reports_sftp_host', 'reports.sandbox.paypal.com');

src/Form/Type/PayPalConfigurationType.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,53 @@
2222

2323
final class PayPalConfigurationType extends AbstractType
2424
{
25+
private const SANDBOX_ATTRIBUTION_ID = 'sylius-ppcp4p-bn-code';
26+
27+
private const SANDBOX_SYLIUS_MERCHANT_ID = 'SYLIUS_SANDBOX_MERCHANT_ID';
28+
2529
private const HIDDEN_FIELDS = [
2630
'merchant_id',
2731
'sylius_merchant_id',
2832
'partner_attribution_id',
2933
'use_authorize',
3034
];
3135

36+
public function __construct(private bool $isSandbox = false)
37+
{
38+
}
39+
3240
public function buildForm(FormBuilderInterface $builder, array $options): void
3341
{
3442
$originalData = [];
3543

36-
$builder
37-
->add('client_id', TextType::class, ['label' => 'sylius.pay_pal.client_id', 'attr' => ['readonly' => true]])
38-
->add('client_secret', TextType::class, ['label' => 'sylius.pay_pal.client_secret', 'attr' => ['readonly' => true]])
39-
->add('merchant_id', HiddenType::class, ['attr' => ['readonly' => true]])
40-
->add('sylius_merchant_id', HiddenType::class, ['attr' => ['readonly' => true]])
41-
->add('partner_attribution_id', HiddenType::class, ['attr' => ['readonly' => true]])
42-
// we need to force Sylius Payum integration to postpone creating an order, it's the easiest way
43-
->add('use_authorize', HiddenType::class, ['data' => true, 'attr' => ['readonly' => true]])
44-
->add('reports_sftp_username', TextType::class, ['label' => 'sylius.pay_pal.sftp_username', 'required' => false])
45-
->add('reports_sftp_password', TextType::class, ['label' => 'sylius.pay_pal.sftp_password', 'required' => false])
46-
;
44+
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
45+
$form = $event->getForm();
46+
47+
if ($this->isSandbox) {
48+
$form
49+
->add('sylius_merchant_id', HiddenType::class, ['data' => self::SANDBOX_SYLIUS_MERCHANT_ID, 'attr' => ['readonly' => true]])
50+
->add('partner_attribution_id', HiddenType::class, ['data' => self::SANDBOX_ATTRIBUTION_ID, 'attr' => ['readonly' => true]])
51+
->add('client_id', TextType::class, ['label' => 'sylius.pay_pal.client_id'])
52+
->add('client_secret', TextType::class, ['label' => 'sylius.pay_pal.client_secret'])
53+
->add('merchant_id', TextType::class, ['label' => 'sylius.pay_pal.merchant_id'])
54+
;
55+
} else {
56+
$form
57+
->add('sylius_merchant_id', HiddenType::class, ['attr' => ['readonly' => true]])
58+
->add('partner_attribution_id', HiddenType::class, ['attr' => ['readonly' => true]])
59+
->add('client_id', TextType::class, ['label' => 'sylius.pay_pal.client_id', 'attr' => ['readonly' => true]])
60+
->add('client_secret', TextType::class, ['label' => 'sylius.pay_pal.client_secret', 'attr' => ['readonly' => true]])
61+
->add('merchant_id', HiddenType::class, ['attr' => ['readonly' => true]])
62+
;
63+
}
64+
65+
$form
66+
// we need to force Sylius Payum integration to postpone creating an order, it's the easiest way
67+
->add('use_authorize', HiddenType::class, ['data' => true, 'attr' => ['readonly' => true]])
68+
->add('reports_sftp_username', TextType::class, ['label' => 'sylius.pay_pal.sftp_username', 'required' => false])
69+
->add('reports_sftp_password', TextType::class, ['label' => 'sylius.pay_pal.sftp_password', 'required' => false])
70+
;
71+
});
4772

4873
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use (&$originalData): void {
4974
$data = $event->getData();

src/Listener/PayPalPaymentMethodListener.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,16 @@
2929

3030
final class PayPalPaymentMethodListener
3131
{
32-
private OnboardingInitiatorInterface $onboardingInitiator;
33-
34-
private UrlGeneratorInterface $urlGenerator;
35-
36-
private FlashBagInterface|RequestStack $flashBagOrRequestStack;
37-
38-
private PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider;
39-
4032
public function __construct(
41-
OnboardingInitiatorInterface $onboardingInitiator,
42-
UrlGeneratorInterface $urlGenerator,
43-
FlashBagInterface|RequestStack $flashBagOrRequestStack,
44-
PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider,
33+
private OnboardingInitiatorInterface $onboardingInitiator,
34+
private UrlGeneratorInterface $urlGenerator,
35+
private FlashBagInterface|RequestStack $flashBagOrRequestStack,
36+
private PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider,
37+
private bool $isSandbox = false,
4538
) {
4639
if ($flashBagOrRequestStack instanceof FlashBagInterface) {
4740
trigger_deprecation('sylius/paypal-plugin', '1.5', sprintf('Passing an instance of %s as constructor argument for %s is deprecated as of PayPalPlugin 1.5 and will be removed in 2.0. Pass an instance of %s instead.', FlashBagInterface::class, self::class, RequestStack::class));
4841
}
49-
50-
$this->onboardingInitiator = $onboardingInitiator;
51-
$this->urlGenerator = $urlGenerator;
52-
$this->flashBagOrRequestStack = $flashBagOrRequestStack;
53-
$this->payPalPaymentMethodProvider = $payPalPaymentMethodProvider;
5442
}
5543

5644
public function initializeCreate(ResourceControllerEvent $event): void
@@ -74,7 +62,7 @@ public function initializeCreate(ResourceControllerEvent $event): void
7462
return;
7563
}
7664

77-
if (!$this->onboardingInitiator->supports($paymentMethod)) {
65+
if ($this->isSandbox || !$this->onboardingInitiator->supports($paymentMethod)) {
7866
return;
7967
}
8068

src/Resources/config/services.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
<service id="sylius_paypal.form.extension.payment_method" alias="Sylius\PayPalPlugin\Form\Extension\PaymentMethodTypeExtension" />
4848

4949
<service id="Sylius\PayPalPlugin\Form\Type\PayPalConfigurationType">
50+
<argument>%sylius_paypal.sandbox%</argument>
51+
52+
<tag name="form.type" />
5053
<tag name="sylius.gateway_configuration_type" type="sylius.pay_pal" label="sylius.pay_pal.label" />
5154
</service>
5255
<service id="sylius_paypal.form.type.paypal_configuration" alias="Sylius\PayPalPlugin\Form\Type\PayPalConfigurationType" />
@@ -69,6 +72,7 @@
6972
<argument type="service" id="router" />
7073
<argument type="service" id="request_stack" />
7174
<argument type="service" id="sylius_paypal.provider.paypal_payment_method" />
75+
<argument>%sylius_paypal.sandbox%</argument>
7276
<tag name="kernel.event_listener" event="sylius.payment_method.initialize_create" method="initializeCreate" />
7377
</service>
7478
<service id="sylius_paypal.listener.paypal_payment_method" alias="Sylius\PayPalPlugin\Listener\PayPalPaymentMethodListener" />

src/Resources/translations/messages.en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ sylius:
1212
expiration_date: 'Expiration date'
1313
invalid_form: 'Form is invalid'
1414
label: 'PayPal'
15+
merchant_id: 'Merchant ID'
1516
missing_billing_address_content: 'Please, go back to the Addressing step and complete it'
1617
missing_billing_address_header: 'We could not fetch any billing address from PayPal'
1718
pay_with_card: 'Pay with Card'

0 commit comments

Comments
 (0)