Skip to content

Commit 27d7940

Browse files
Reapply "Add service for creating PayPal sandbox payment methods" (Sylius#376) (Sylius#424)
This reverts commit 577418b, reversing changes made to e4ee93c. | Q | A | --------------- | ----- | Branch? | 2.0 | Bug fix? | yes | New feature? | no Fixes Sylius#388
2 parents 9d393ec + a8712fc commit 27d7940

20 files changed

+581
-50
lines changed

config/app/twig_hooks/admin/payment_method/index.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ sylius_twig_hooks:
44
confirmation_modal_create_paypal:
55
template: '@SyliusPayPalPlugin/admin/payment_method/grid/confirmation_modal_create_paypal.html.twig'
66
priority: -100
7+
confirmation_modal_create_paypal_sandbox:
8+
template: '@SyliusPayPalPlugin/admin/payment_method/grid/confirmation_modal_create_paypal_sandbox.html.twig'
9+
priority: -200

config/services.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
<tag name="sylius.gateway_configuration_type" type="sylius_paypal" label="sylius_paypal.label" />
5151
</service>
5252

53+
<service id="sylius_paypal.form.type.paypal_sandbox_credentials" class="Sylius\PayPalPlugin\Form\Type\PayPalSandboxCredentialsType">
54+
<tag name="form.type" />
55+
</service>
56+
5357
<service
5458
id="sylius_paypal.generator.paypal_auth_assertion"
5559
class="Sylius\PayPalPlugin\Generator\PayPalAuthAssertionGenerator"
@@ -307,14 +311,38 @@
307311
</service>
308312
<service id="Sylius\PayPalPlugin\Registrar\SellerWebhookRegistrarInterface" alias="sylius_paypal.registrar.seller_webhook" />
309313

314+
<service id="sylius_paypal.creator.sandbox_payment_method" class="Sylius\PayPalPlugin\Creator\PayPalSandboxPaymentMethodCreator">
315+
<argument type="service" id="sylius.factory.gateway_config" />
316+
<argument type="service" id="sylius.factory.payment_method" />
317+
<argument type="service" id="doctrine.orm.entity_manager" />
318+
</service>
319+
310320
<service id="sylius_paypal.twig.extension.paypal" class="Sylius\PayPalPlugin\Twig\PayPalExtension">
321+
<argument>%sylius_paypal.sandbox%</argument>
311322
<tag name="twig.extension" />
312323
</service>
313324

314325
<service id="sylius_paypal.twig.extension.order_address" class="Sylius\PayPalPlugin\Twig\OrderAddressExtension">
315326
<tag name="twig.extension" />
316327
</service>
317328

329+
<service
330+
id="sylius_paypal.twig.component.paypal_sandbox_modal"
331+
class="Sylius\PayPalPlugin\Twig\Component\PayPalSandboxModalComponent"
332+
>
333+
<argument type="service" id="form.factory" />
334+
<argument type="service" id="sylius_paypal.creator.sandbox_payment_method" />
335+
<argument type="service" id="request_stack" />
336+
<argument type="service" id="logger" />
337+
<argument type="service" id="router" />
338+
339+
<tag
340+
name="sylius.live_component.admin"
341+
key="sylius_paypal:create_sandbox_modal"
342+
template="@SyliusPayPalPlugin/admin/shared/components/paypal_sandbox_modal.html.twig"
343+
/>
344+
</service>
345+
318346
<service id="sylius_paypal.verifier.payment_amount" class="Sylius\PayPalPlugin\Verifier\PaymentAmountVerifier" />
319347
<service id="Sylius\PayPalPlugin\Verifier\PaymentAmountVerifierInterface" alias="sylius_paypal.verifier.payment_amount" />
320348

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
4+
<class name="Sylius\PayPalPlugin\Model\PayPalSandboxCredentials">
5+
<property name="clientId">
6+
<constraint name="NotBlank">
7+
<option name="groups">sylius</option>
8+
</constraint>
9+
<constraint name="Length">
10+
<option name="min">5</option>
11+
<option name="max">255</option>
12+
<option name="groups">sylius</option>
13+
</constraint>
14+
</property>
15+
<property name="clientSecret">
16+
<constraint name="NotBlank">
17+
<option name="groups">sylius</option>
18+
</constraint>
19+
<constraint name="Length">
20+
<option name="min">5</option>
21+
<option name="max">255</option>
22+
<option name="groups">sylius</option>
23+
</constraint>
24+
</property>
25+
<property name="merchantId">
26+
<constraint name="NotBlank">
27+
<option name="groups">sylius</option>
28+
</constraint>
29+
<constraint name="Length">
30+
<option name="min">5</option>
31+
<option name="max">25</option>
32+
<option name="groups">sylius</option>
33+
</constraint>
34+
</property>
35+
</class>
36+
</constraint-mapping>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\PayPalPlugin\Creator;
15+
16+
use Doctrine\ORM\EntityManagerInterface;
17+
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
18+
use Sylius\Component\Core\Model\PaymentMethodInterface;
19+
use Sylius\Component\Resource\Factory\FactoryInterface;
20+
use Sylius\PayPalPlugin\DependencyInjection\SyliusPayPalExtension;
21+
22+
final readonly class PayPalSandboxPaymentMethodCreator implements PayPalSandboxPaymentMethodCreatorInterface
23+
{
24+
public function __construct(
25+
private FactoryInterface $gatewayFactory,
26+
private FactoryInterface $paymentMethodFactory,
27+
private EntityManagerInterface $entityManager,
28+
) {
29+
}
30+
31+
public function create(string $clientId, string $clientSecret, string $merchantId): PaymentMethodInterface
32+
{
33+
$gatewayConfig = $this->createGatewayConfig($clientId, $clientSecret, $merchantId);
34+
$paymentMethod = $this->createPaymentMethod($gatewayConfig);
35+
36+
$this->entityManager->persist($paymentMethod);
37+
$this->entityManager->flush();
38+
39+
return $paymentMethod;
40+
}
41+
42+
private function createGatewayConfig(string $clientId, string $clientSecret, string $merchantId): GatewayConfigInterface
43+
{
44+
/** @var GatewayConfigInterface $gatewayConfig */
45+
$gatewayConfig = $this->gatewayFactory->createNew();
46+
$gatewayConfig->setFactoryName(SyliusPayPalExtension::PAYPAL_FACTORY_NAME);
47+
$gatewayConfig->setGatewayName(self::GATEWAY_NAME);
48+
49+
$gatewayConfig->setConfig([
50+
'client_id' => $clientId,
51+
'client_secret' => $clientSecret,
52+
'merchant_id' => $merchantId,
53+
'use_authorize' => 1,
54+
'sylius_merchant_id' => self::SYLIUS_SANDBOX_MERCHANT_ID,
55+
'reports_sftp_password' => null,
56+
'reports_sftp_username' => null,
57+
'partner_attribution_id' => self::PARTNER_ATTRIBUTION_ID,
58+
]);
59+
60+
return $gatewayConfig;
61+
}
62+
63+
private function createPaymentMethod(GatewayConfigInterface $gatewayConfig): PaymentMethodInterface
64+
{
65+
/** @var PaymentMethodInterface $paymentMethod */
66+
$paymentMethod = $this->paymentMethodFactory->createNew();
67+
$paymentMethod->setGatewayConfig($gatewayConfig);
68+
$paymentMethod->setCode(self::PAYMENT_METHOD_CODE);
69+
$paymentMethod->setName(self::PAYMENT_METHOD_NAME);
70+
$paymentMethod->setDescription(self::PAYMENT_METHOD_DESCRIPTION);
71+
72+
return $paymentMethod;
73+
}
74+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\PayPalPlugin\Creator;
15+
16+
use Sylius\Component\Core\Model\PaymentMethodInterface;
17+
18+
interface PayPalSandboxPaymentMethodCreatorInterface
19+
{
20+
public const GATEWAY_NAME = 'sylius_paypal_sandbox';
21+
22+
public const PARTNER_ATTRIBUTION_ID = 'sylius-ppcp4p-bn-code';
23+
24+
public const PAYMENT_METHOD_CODE = 'PAYPAL';
25+
26+
public const PAYMENT_METHOD_NAME = 'PayPal';
27+
28+
public const PAYMENT_METHOD_DESCRIPTION = 'Pay with PayPal';
29+
30+
public const SYLIUS_SANDBOX_MERCHANT_ID = 'SYLIUS_SANDBOX_MERCHANT_ID';
31+
32+
public function create(string $clientId, string $clientSecret, string $merchantId): PaymentMethodInterface;
33+
}

src/Form/Type/PayPalConfigurationType.php

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,31 @@
1919
use Symfony\Component\Form\FormBuilderInterface;
2020
use Symfony\Component\Form\FormEvent;
2121
use Symfony\Component\Form\FormEvents;
22-
use Symfony\Component\Validator\Constraints\NotBlank;
2322

2423
final class PayPalConfigurationType extends AbstractType
2524
{
26-
private const SANDBOX_ATTRIBUTION_ID = 'sylius-ppcp4p-bn-code';
27-
28-
private const SANDBOX_SYLIUS_MERCHANT_ID = 'SYLIUS_SANDBOX_MERCHANT_ID';
29-
3025
private const HIDDEN_FIELDS = [
3126
'merchant_id',
3227
'sylius_merchant_id',
3328
'partner_attribution_id',
3429
'use_authorize',
3530
];
3631

37-
public function __construct(private bool $isSandbox = false)
38-
{
39-
}
40-
4132
public function buildForm(FormBuilderInterface $builder, array $options): void
4233
{
4334
$originalData = [];
4435

45-
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
46-
$form = $event->getForm();
47-
48-
if ($this->isSandbox) {
49-
$form
50-
->add('sylius_merchant_id', HiddenType::class, ['data' => self::SANDBOX_SYLIUS_MERCHANT_ID, 'attr' => ['readonly' => true]])
51-
->add('partner_attribution_id', HiddenType::class, ['data' => self::SANDBOX_ATTRIBUTION_ID, 'attr' => ['readonly' => true]])
52-
->add('client_id', TextType::class, [
53-
'label' => 'sylius_paypal.client_id',
54-
'constraints' => [new NotBlank(['groups' => 'sylius'])],
55-
])
56-
->add('client_secret', TextType::class, [
57-
'label' => 'sylius_paypal.client_secret',
58-
'constraints' => [new NotBlank(['groups' => 'sylius'])],
59-
])
60-
->add('merchant_id', TextType::class, [
61-
'label' => 'sylius_paypal.merchant_id',
62-
'constraints' => [new NotBlank(['groups' => 'sylius'])],
63-
])
64-
;
65-
} else {
66-
$form
67-
->add('sylius_merchant_id', HiddenType::class, ['attr' => ['readonly' => true]])
68-
->add('partner_attribution_id', HiddenType::class, ['attr' => ['readonly' => true]])
69-
->add('client_id', TextType::class, ['label' => 'sylius_paypal.client_id', 'attr' => ['readonly' => true]])
70-
->add('client_secret', TextType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]])
71-
->add('merchant_id', HiddenType::class, ['attr' => ['readonly' => true]])
72-
;
73-
}
74-
75-
$form
76-
// we need to force Sylius Payum integration to postpone creating an order, it's the easiest way
77-
->add('use_authorize', HiddenType::class, ['data' => true, 'attr' => ['readonly' => true]])
78-
->add('reports_sftp_username', TextType::class, ['label' => 'sylius_paypal.sftp_username', 'required' => false])
79-
->add('reports_sftp_password', TextType::class, ['label' => 'sylius_paypal.sftp_password', 'required' => false])
80-
;
81-
});
36+
$builder
37+
->add('client_id', TextType::class, ['label' => 'sylius_paypal.client_id', 'attr' => ['readonly' => true]])
38+
->add('client_secret', TextType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]])
39+
->add('merchant_id', HiddenType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]])
40+
->add('sylius_merchant_id', HiddenType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]])
41+
->add('partner_attribution_id', HiddenType::class, ['label' => 'sylius_paypal.partner_attribution_id', '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_paypal.sftp_username', 'required' => false])
45+
->add('reports_sftp_password', TextType::class, ['label' => 'sylius_paypal.sftp_password', 'required' => false])
46+
;
8247

8348
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use (&$originalData): void {
8449
$data = $event->getData();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\PayPalPlugin\Form\Type;
15+
16+
use Sylius\PayPalPlugin\Model\PayPalSandboxCredentials;
17+
use Symfony\Component\Form\AbstractType;
18+
use Symfony\Component\Form\Extension\Core\Type\TextType;
19+
use Symfony\Component\Form\FormBuilderInterface;
20+
use Symfony\Component\OptionsResolver\OptionsResolver;
21+
22+
final class PayPalSandboxCredentialsType extends AbstractType
23+
{
24+
public function buildForm(FormBuilderInterface $builder, array $options): void
25+
{
26+
$builder
27+
->add('clientId', TextType::class, [
28+
'label' => 'sylius_paypal.client_id',
29+
])
30+
->add('clientSecret', TextType::class, [
31+
'label' => 'sylius_paypal.client_secret',
32+
])
33+
->add('merchantId', TextType::class, [
34+
'label' => 'sylius_paypal.merchant_id',
35+
]);
36+
}
37+
38+
public function configureOptions(OptionsResolver $resolver): void
39+
{
40+
$resolver->setDefaults([
41+
'data_class' => PayPalSandboxCredentials::class,
42+
'csrf_protection' => true,
43+
'validation_groups' => 'sylius',
44+
]);
45+
}
46+
47+
public function getBlockPrefix(): string
48+
{
49+
return 'sylius_paypal_sandbox_credentials';
50+
}
51+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\PayPalPlugin\Model;
15+
16+
class PayPalSandboxCredentials
17+
{
18+
protected string $clientId;
19+
20+
protected string $clientSecret;
21+
22+
protected string $merchantId;
23+
24+
public function getClientId(): string
25+
{
26+
return $this->clientId;
27+
}
28+
29+
public function setClientId(string $clientId): void
30+
{
31+
$this->clientId = $clientId;
32+
}
33+
34+
public function getClientSecret(): string
35+
{
36+
return $this->clientSecret;
37+
}
38+
39+
public function setClientSecret(string $clientSecret): void
40+
{
41+
$this->clientSecret = $clientSecret;
42+
}
43+
44+
public function getMerchantId(): string
45+
{
46+
return $this->merchantId;
47+
}
48+
49+
public function setMerchantId(string $merchantId): void
50+
{
51+
$this->merchantId = $merchantId;
52+
}
53+
}

0 commit comments

Comments
 (0)