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
3 changes: 3 additions & 0 deletions config/app/twig_hooks/admin/payment_method/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ sylius_twig_hooks:
confirmation_modal_create_paypal:
template: '@SyliusPayPalPlugin/admin/payment_method/grid/confirmation_modal_create_paypal.html.twig'
priority: -100
confirmation_modal_create_paypal_sandbox:
template: '@SyliusPayPalPlugin/admin/payment_method/grid/confirmation_modal_create_paypal_sandbox.html.twig'
priority: -200
28 changes: 28 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<tag name="sylius.gateway_configuration_type" type="sylius_paypal" label="sylius_paypal.label" />
</service>

<service id="sylius_paypal.form.type.paypal_sandbox_credentials" class="Sylius\PayPalPlugin\Form\Type\PayPalSandboxCredentialsType">
<tag name="form.type" />
</service>

<service
id="sylius_paypal.generator.paypal_auth_assertion"
class="Sylius\PayPalPlugin\Generator\PayPalAuthAssertionGenerator"
Expand Down Expand Up @@ -307,14 +311,38 @@
</service>
<service id="Sylius\PayPalPlugin\Registrar\SellerWebhookRegistrarInterface" alias="sylius_paypal.registrar.seller_webhook" />

<service id="sylius_paypal.creator.sandbox_payment_method" class="Sylius\PayPalPlugin\Creator\PayPalSandboxPaymentMethodCreator">
<argument type="service" id="sylius.factory.gateway_config" />
<argument type="service" id="sylius.factory.payment_method" />
<argument type="service" id="doctrine.orm.entity_manager" />
</service>

<service id="sylius_paypal.twig.extension.paypal" class="Sylius\PayPalPlugin\Twig\PayPalExtension">
<argument>%sylius_paypal.sandbox%</argument>
<tag name="twig.extension" />
</service>

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

<service
id="sylius_paypal.twig.component.paypal_sandbox_modal"
class="Sylius\PayPalPlugin\Twig\Component\PayPalSandboxModalComponent"
>
<argument type="service" id="form.factory" />
<argument type="service" id="sylius_paypal.creator.sandbox_payment_method" />
<argument type="service" id="request_stack" />
<argument type="service" id="logger" />
<argument type="service" id="router" />

<tag
name="sylius.live_component.admin"
key="sylius_paypal:create_sandbox_modal"
template="@SyliusPayPalPlugin/admin/shared/components/paypal_sandbox_modal.html.twig"
/>
</service>

<service id="sylius_paypal.verifier.payment_amount" class="Sylius\PayPalPlugin\Verifier\PaymentAmountVerifier" />
<service id="Sylius\PayPalPlugin\Verifier\PaymentAmountVerifierInterface" alias="sylius_paypal.verifier.payment_amount" />

Expand Down
36 changes: 36 additions & 0 deletions config/validation/PayPalSandboxCredentials.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>

<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">
<class name="Sylius\PayPalPlugin\Model\PayPalSandboxCredentials">
<property name="clientId">
<constraint name="NotBlank">
<option name="groups">sylius</option>
</constraint>
<constraint name="Length">
<option name="min">5</option>
<option name="max">255</option>
<option name="groups">sylius</option>
</constraint>
</property>
<property name="clientSecret">
<constraint name="NotBlank">
<option name="groups">sylius</option>
</constraint>
<constraint name="Length">
<option name="min">5</option>
<option name="max">255</option>
<option name="groups">sylius</option>
</constraint>
</property>
<property name="merchantId">
<constraint name="NotBlank">
<option name="groups">sylius</option>
</constraint>
<constraint name="Length">
<option name="min">5</option>
<option name="max">25</option>
<option name="groups">sylius</option>
</constraint>
</property>
</class>
</constraint-mapping>
74 changes: 74 additions & 0 deletions src/Creator/PayPalSandboxPaymentMethodCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\PayPalPlugin\Creator;

use Doctrine\ORM\EntityManagerInterface;
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Sylius\PayPalPlugin\DependencyInjection\SyliusPayPalExtension;

final readonly class PayPalSandboxPaymentMethodCreator implements PayPalSandboxPaymentMethodCreatorInterface
{
public function __construct(
private FactoryInterface $gatewayFactory,
private FactoryInterface $paymentMethodFactory,
private EntityManagerInterface $entityManager,
) {
}

public function create(string $clientId, string $clientSecret, string $merchantId): PaymentMethodInterface
{
$gatewayConfig = $this->createGatewayConfig($clientId, $clientSecret, $merchantId);
$paymentMethod = $this->createPaymentMethod($gatewayConfig);

$this->entityManager->persist($paymentMethod);
$this->entityManager->flush();

return $paymentMethod;
}

private function createGatewayConfig(string $clientId, string $clientSecret, string $merchantId): GatewayConfigInterface
{
/** @var GatewayConfigInterface $gatewayConfig */
$gatewayConfig = $this->gatewayFactory->createNew();
$gatewayConfig->setFactoryName(SyliusPayPalExtension::PAYPAL_FACTORY_NAME);
$gatewayConfig->setGatewayName(self::GATEWAY_NAME);

$gatewayConfig->setConfig([
'client_id' => $clientId,
'client_secret' => $clientSecret,
'merchant_id' => $merchantId,
'use_authorize' => 1,
'sylius_merchant_id' => self::SYLIUS_SANDBOX_MERCHANT_ID,
'reports_sftp_password' => null,
'reports_sftp_username' => null,
'partner_attribution_id' => self::PARTNER_ATTRIBUTION_ID,
]);

return $gatewayConfig;
}

private function createPaymentMethod(GatewayConfigInterface $gatewayConfig): PaymentMethodInterface
{
/** @var PaymentMethodInterface $paymentMethod */
$paymentMethod = $this->paymentMethodFactory->createNew();
$paymentMethod->setGatewayConfig($gatewayConfig);
$paymentMethod->setCode(self::PAYMENT_METHOD_CODE);
$paymentMethod->setName(self::PAYMENT_METHOD_NAME);
$paymentMethod->setDescription(self::PAYMENT_METHOD_DESCRIPTION);

return $paymentMethod;
}
}
33 changes: 33 additions & 0 deletions src/Creator/PayPalSandboxPaymentMethodCreatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\PayPalPlugin\Creator;

use Sylius\Component\Core\Model\PaymentMethodInterface;

interface PayPalSandboxPaymentMethodCreatorInterface
{
public const GATEWAY_NAME = 'sylius_paypal_sandbox';

public const PARTNER_ATTRIBUTION_ID = 'sylius-ppcp4p-bn-code';

public const PAYMENT_METHOD_CODE = 'PAYPAL';

public const PAYMENT_METHOD_NAME = 'PayPal';

public const PAYMENT_METHOD_DESCRIPTION = 'Pay with PayPal';

public const SYLIUS_SANDBOX_MERCHANT_ID = 'SYLIUS_SANDBOX_MERCHANT_ID';

public function create(string $clientId, string $clientSecret, string $merchantId): PaymentMethodInterface;
}
57 changes: 11 additions & 46 deletions src/Form/Type/PayPalConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,31 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Validator\Constraints\NotBlank;

final class PayPalConfigurationType extends AbstractType
{
private const SANDBOX_ATTRIBUTION_ID = 'sylius-ppcp4p-bn-code';

private const SANDBOX_SYLIUS_MERCHANT_ID = 'SYLIUS_SANDBOX_MERCHANT_ID';

private const HIDDEN_FIELDS = [
'merchant_id',
'sylius_merchant_id',
'partner_attribution_id',
'use_authorize',
];

public function __construct(private bool $isSandbox = false)
{
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$originalData = [];

$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
$form = $event->getForm();

if ($this->isSandbox) {
$form
->add('sylius_merchant_id', HiddenType::class, ['data' => self::SANDBOX_SYLIUS_MERCHANT_ID, 'attr' => ['readonly' => true]])
->add('partner_attribution_id', HiddenType::class, ['data' => self::SANDBOX_ATTRIBUTION_ID, 'attr' => ['readonly' => true]])
->add('client_id', TextType::class, [
'label' => 'sylius_paypal.client_id',
'constraints' => [new NotBlank(['groups' => 'sylius'])],
])
->add('client_secret', TextType::class, [
'label' => 'sylius_paypal.client_secret',
'constraints' => [new NotBlank(['groups' => 'sylius'])],
])
->add('merchant_id', TextType::class, [
'label' => 'sylius_paypal.merchant_id',
'constraints' => [new NotBlank(['groups' => 'sylius'])],
])
;
} else {
$form
->add('sylius_merchant_id', HiddenType::class, ['attr' => ['readonly' => true]])
->add('partner_attribution_id', HiddenType::class, ['attr' => ['readonly' => true]])
->add('client_id', TextType::class, ['label' => 'sylius_paypal.client_id', 'attr' => ['readonly' => true]])
->add('client_secret', TextType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]])
->add('merchant_id', HiddenType::class, ['attr' => ['readonly' => true]])
;
}

$form
// we need to force Sylius Payum integration to postpone creating an order, it's the easiest way
->add('use_authorize', HiddenType::class, ['data' => true, 'attr' => ['readonly' => true]])
->add('reports_sftp_username', TextType::class, ['label' => 'sylius_paypal.sftp_username', 'required' => false])
->add('reports_sftp_password', TextType::class, ['label' => 'sylius_paypal.sftp_password', 'required' => false])
;
});
$builder
->add('client_id', TextType::class, ['label' => 'sylius_paypal.client_id', 'attr' => ['readonly' => true]])
->add('client_secret', TextType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]])
->add('merchant_id', HiddenType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]])
->add('sylius_merchant_id', HiddenType::class, ['label' => 'sylius_paypal.client_secret', 'attr' => ['readonly' => true]])
->add('partner_attribution_id', HiddenType::class, ['label' => 'sylius_paypal.partner_attribution_id', 'attr' => ['readonly' => true]])
// we need to force Sylius Payum integration to postpone creating an order, it's the easiest way
->add('use_authorize', HiddenType::class, ['data' => true, 'attr' => ['readonly' => true]])
->add('reports_sftp_username', TextType::class, ['label' => 'sylius_paypal.sftp_username', 'required' => false])
->add('reports_sftp_password', TextType::class, ['label' => 'sylius_paypal.sftp_password', 'required' => false])
;

$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use (&$originalData): void {
$data = $event->getData();
Expand Down
51 changes: 51 additions & 0 deletions src/Form/Type/PayPalSandboxCredentialsType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\PayPalPlugin\Form\Type;

use Sylius\PayPalPlugin\Model\PayPalSandboxCredentials;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class PayPalSandboxCredentialsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('clientId', TextType::class, [
'label' => 'sylius_paypal.client_id',
])
->add('clientSecret', TextType::class, [
'label' => 'sylius_paypal.client_secret',
])
->add('merchantId', TextType::class, [
'label' => 'sylius_paypal.merchant_id',
]);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => PayPalSandboxCredentials::class,
'csrf_protection' => true,
'validation_groups' => 'sylius',
]);
}

public function getBlockPrefix(): string
{
return 'sylius_paypal_sandbox_credentials';
}
}
53 changes: 53 additions & 0 deletions src/Model/PayPalSandboxCredentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\PayPalPlugin\Model;

class PayPalSandboxCredentials
{
protected string $clientId;

protected string $clientSecret;

protected string $merchantId;

public function getClientId(): string
{
return $this->clientId;
}

public function setClientId(string $clientId): void
{
$this->clientId = $clientId;
}

public function getClientSecret(): string
{
return $this->clientSecret;
}

public function setClientSecret(string $clientSecret): void
{
$this->clientSecret = $clientSecret;
}

public function getMerchantId(): string
{
return $this->merchantId;
}

public function setMerchantId(string $merchantId): void
{
$this->merchantId = $merchantId;
}
}
Loading