Skip to content

Commit

Permalink
Merge branch '2.0' into test-apip-4
Browse files Browse the repository at this point in the history
  • Loading branch information
GSadee authored Oct 25, 2024
2 parents 53b5dfa + f3349a4 commit 9850a4d
Show file tree
Hide file tree
Showing 55 changed files with 1,882 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ SYLIUS_MESSENGER_TRANSPORT_PAYMENT_REQUEST_FAILED_DSN=doctrine://default?queue_n
###> symfony/mailer ###
MAILER_DSN=null://null
###< symfony/mailer ###

SYLIUS_PAYMENT_ENCRYPTION_KEY_PATH=%kernel.project_dir%/config/encryption/dev.key
2 changes: 2 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ SYLIUS_MESSENGER_TRANSPORT_PAYMENT_REQUEST_FAILED_DSN=sync://
###< symfony/messenger ###

MAILER_DSN=null://null

SYLIUS_PAYMENT_ENCRYPTION_KEY_PATH=%kernel.project_dir%/config/encryption/test.key
2 changes: 2 additions & 0 deletions .env.test_cached
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ SYLIUS_MESSENGER_TRANSPORT_PAYMENT_REQUEST_FAILED_DSN=sync://
###< symfony/messenger ###

MAILER_DSN=null://null

SYLIUS_PAYMENT_ENCRYPTION_KEY_PATH=%kernel.project_dir%/config/encryption/test.key
2 changes: 2 additions & 0 deletions .env.test_cached_payum
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ SYLIUS_MESSENGER_TRANSPORT_PAYMENT_REQUEST_FAILED_DSN=sync://
###< symfony/messenger ###

MAILER_DSN=null://null

SYLIUS_PAYMENT_ENCRYPTION_KEY_PATH=%kernel.project_dir%/config/encryption/test.key
3 changes: 3 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@
"HWI\\Bundle\\OAuthBundle\\OAuth\\Response\\UserResponseInterface",
"HWI\\Bundle\\OAuthBundle\\Security\\Core\\User\\OAuthAwareUserProviderInterface",
"League\\Flysystem\\FilesystemOperator",
"ParagonIE\\ConstantTime\\Hex",
"ParagonIE\\HiddenString\\HiddenString",
"Payum\\Core\\Action\\ActionInterface",
"Payum\\Core\\Action\\GatewayAwareAction",
"Payum\\Core\\Bridge\\Doctrine\\Storage\\DoctrineStorage",
"Payum\\Core\\Bridge\\Spl\\ArrayObject",
"Payum\\Core\\Exception\\RequestNotSupportedException",
"Payum\\Core\\Extension\\Context",
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"ext-intl": "*",
"ext-json": "*",
"ext-simplexml": "*",
"ext-sodium": "*",
"api-platform/core": "^4.0.3",
"babdev/pagerfanta-bundle": "^4.4",
"behat/transliterator": "^1.5",
Expand Down Expand Up @@ -59,6 +60,7 @@
"lexik/jwt-authentication-bundle": "^3.1",
"liip/imagine-bundle": "^2.13",
"pagerfanta/pagerfanta": "^4.0",
"paragonie/halite": "^5.0",
"payum/offline": "^1.7.5",
"payum/payum-bundle": "^2.6",
"php-http/discovery": "^1.20",
Expand Down
Empty file added config/encryption/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions config/encryption/test.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
31400500d6649581d6ac178bc41c92acc686dd869e6aa8665b4dad27f8921075e8cbf34059793bf9a0c603cd870f0433fb817afdb68bd75445111f27fe36a3252c8bd26fdbd82801568e9c657b022fd39edabff90518a2e04377e4e813bf3bf7d9411e6e
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Context/Cli/InstallerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(
private readonly FactoryInterface $adminUserFactory,
private readonly UserRepositoryInterface $adminUserRepository,
private readonly ValidatorInterface $validator,
private readonly bool $publicDir,
private readonly string $publicDir,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ sylius_twig_hooks:
'sylius_admin.payment_method.create.content.form.sections.gateway_configuration':
type:
template: '@SyliusAdmin/payment_method/form/sections/gateway_configuration/type.html.twig'
priority: 100
use_payum:
template: '@SyliusAdmin/payment_method/form/sections/gateway_configuration/use_payum.html.twig'
priority: 0

'sylius_admin.payment_method.create.content.form.sections.translations':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ sylius_twig_hooks:
'sylius_admin.payment_method.update.content.form.sections.gateway_configuration':
type:
template: '@SyliusAdmin/payment_method/form/sections/gateway_configuration/type.html.twig'
priority: 100
use_payum:
template: '@SyliusAdmin/payment_method/form/sections/gateway_configuration/use_payum.html.twig'
priority: 0

'sylius_admin.payment_method.update.content.form.sections.translations':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% set form = hookable_metadata.context.form %}

<div class="col-12 col-md-6">
{{ form_row(form.gatewayConfig.factoryName, sylius_test_form_attribute('factory-name')|merge({ label_attr: { class: 'checkbox-switch' } })) }}
{{ form_row(form.gatewayConfig.factoryName, sylius_test_form_attribute('factory-name')) }}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% set form = hookable_metadata.context.form %}

<div class="col-12 col-md-6 mt-5">
{{ form_row(form.gatewayConfig.usePayum, sylius_test_form_attribute('use-payum')|merge({ label_attr: { class: 'checkbox-switch' } })) }}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ final class InstallCommand extends Command
'command' => 'sylius:install:jwt-setup',
'message' => 'Configuring JWT token.',
],
[
'command' => 'sylius:payment:generate-key',
'message' => 'Generating payment encryption key.',
],
[
'command' => 'sylius:install:assets',
'message' => 'Installing assets.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class InstallSampleDataCommand extends AbstractInstallCommand
public function __construct(
protected readonly EntityManagerInterface $entityManager,
protected readonly CommandDirectoryChecker $commandDirectoryChecker,
protected readonly bool $publicDir,
protected readonly string $publicDir,
) {
parent::__construct($this->entityManager, $this->commandDirectoryChecker);
}
Expand Down
35 changes: 35 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Migrations/Version20241024174728.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\Bundle\CoreBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Sylius\Bundle\CoreBundle\Doctrine\Migrations\AbstractMigration;

final class Version20241024174728 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add a way to distinguish PaymentRequest gateway from Payum one.';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE sylius_gateway_config ADD use_payum TINYINT(1) DEFAULT 1 NOT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE sylius_gateway_config DROP use_payum');
}
}
35 changes: 35 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Migrations/Version20241024174729.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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\Bundle\CoreBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Sylius\Bundle\CoreBundle\Doctrine\Migrations\AbstractPostgreSQLMigration;

final class Version20241024174729 extends AbstractPostgreSQLMigration
{
public function getDescription(): string
{
return 'Add a way to distinguish PaymentRequest gateway from Payum one.';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE sylius_gateway_config ADD use_payum BOOLEAN DEFAULT true NOT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE sylius_gateway_config DROP use_payum');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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\Bundle\PaymentBundle\Console\Command;

use ParagonIE\Halite\Alerts\CannotPerformOperation;
use ParagonIE\Halite\Alerts\InvalidKey;
use ParagonIE\Halite\KeyFactory;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;

#[AsCommand(
name: 'sylius:payment:generate-key',
description: 'Generate a key for Sylius payment encryption.',
)]
final class GenerateEncryptionKeyCommand extends Command
{
protected SymfonyStyle $io;

public function __construct(
private readonly Filesystem $filesystem,
private readonly string $keyPath,
) {
parent::__construct();
}

protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->io = new SymfonyStyle($input, $output);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->io->writeln('Generating encryption key for Sylius payment encryption');

if (false === $input->getOption('overwrite') && $this->filesystem->exists($this->keyPath)) {
$this->io->writeln(sprintf('Key file "%s" already exists.', $this->keyPath));

$answer = $this->io->confirm('Do you want to overwrite it?', false);
if (false === $answer) {
$this->io->info('Key generation has been canceled');

return Command::SUCCESS;
}
}

try {
$generatedKey = KeyFactory::generateEncryptionKey();
} catch (CannotPerformOperation|InvalidKey|\TypeError) {
$this->io->error('Key could not be generated. Please, make sure that PHP supports libsodium');

return Command::FAILURE;
}

try {
$this->filesystem->mkdir(\dirname($this->keyPath));
$this->filesystem->touch($this->keyPath);
$saved = KeyFactory::save($generatedKey, $this->keyPath);
} catch (IOException) {
$saved = false;
}

if (false === $saved) {
$this->io->error(sprintf(
'Key could not be saved. Please, make sure that the directory "%s" is writable',
\dirname($this->keyPath),
));

return Command::FAILURE;
}

$this->io->success(sprintf('Key has been generated and saved in "%s"', $this->keyPath));

return Command::SUCCESS;
}

protected function configure(): void
{
$this->addOption('overwrite', null, InputOption::VALUE_NONE, 'Overwrites an existing key file');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode
->addDefaultsIfNotSet()
->children()
->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
->arrayNode('encryption')
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')->defaultTrue()->end()
->arrayNode('disabled_for_factories')
->scalarPrototype()->end()
->end()
->end()
->end()
->arrayNode('gateways')
->useAttributeAsKey('name')
->scalarPrototype()->end()
->end()
->arrayNode('gateway_config')
->addDefaultsIfNotSet()
->children()
Expand All @@ -66,11 +80,6 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end()
->arrayNode('gateways')
->useAttributeAsKey('name')
->scalarPrototype()
->end()
->end()
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('sylius.gateway_config.validation_groups', $config['gateway_config']['validation_groups']);
$container->setParameter('sylius.payment_request.states_to_be_cancelled_when_payment_method_changed', $config['payment_request']['states_to_be_cancelled_when_payment_method_changed']);

$this->configureEncryption($config['encryption'], $container);

$this->registerAutoconfiguration($container);
}

Expand Down Expand Up @@ -73,4 +75,21 @@ static function (ChildDefinition $definition, AsNotifyPaymentProvider $attribute
},
);
}

/** @param array<string, mixed> $encryptionConfig */
private function configureEncryption(
array $encryptionConfig,
ContainerBuilder $container,
): void {
$container->setParameter('sylius.encryption.enabled', $encryptionConfig['enabled']);
if (false === $encryptionConfig['enabled']) {
return;
}

$container->setParameter('sylius.encryption.disabled_for_factories', $encryptionConfig['disabled_for_factories']);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config/services/encryption'));

$loader->load('encryption.xml');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Webmozart\Assert\Assert;

final class GatewayConfigType extends AbstractResourceType
{
Expand All @@ -33,21 +34,21 @@ public function __construct(

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$factoryName = $options['data']->getFactoryName();

$builder
->add('factoryName', TextType::class, [
'label' => 'sylius.form.gateway_config.type',
'disabled' => true,
'data' => $factoryName,
])
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($factoryName) {
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$gatewayConfig = $event->getData();

if (!$gatewayConfig instanceof GatewayConfigInterface) {
return;
}

$factoryName = $gatewayConfig->getFactoryName();
Assert::notNull($factoryName, 'A factory name is required.');

if (!$this->gatewayConfigurationTypeRegistry->has('gateway_config', $factoryName)) {
return;
}
Expand Down
Loading

0 comments on commit 9850a4d

Please sign in to comment.