Skip to content

Commit

Permalink
[shopsys] GoPay configuration is based on domains instead of locale (…
Browse files Browse the repository at this point in the history
…#3308)
  • Loading branch information
grossmannmartin committed Sep 3, 2024
2 parents be5dd25 + 9913bc7 commit d29d0ef
Show file tree
Hide file tree
Showing 31 changed files with 591 additions and 170 deletions.
6 changes: 3 additions & 3 deletions assets/js/admin/components/Payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

const onPaymentChange = function () {
const selectedType = $paymentType.val();
const $goPayPaymentMethodFormLine = $('.js-payment-gopay-payment-method').closest('.form-line');
const $goPayPaymentMethodDiv = $('.js-payment-gopay-payment-method');

if (selectedType === 'goPay') {
$goPayPaymentMethodFormLine.show();
$goPayPaymentMethodDiv.show();
} else {
$goPayPaymentMethodFormLine.hide();
$goPayPaymentMethodDiv.hide();
}
};

Expand Down
1 change: 1 addition & 0 deletions assets/js/admin/validation/form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import './validationMailTemplate';
import './validationAdvert';
import './validationOrder';
import './validationParameterName';
import './validationPayment';
import './validationPromoCode';
import './validationHreflangSetting';
import './validationStore';
13 changes: 13 additions & 0 deletions assets/js/admin/validation/form/validationPayment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Register from '../../../common/utils/Register';

export default function gopayPaymentMethodValidator ($container) {
window.$('form[name="payment_form"]').jsFormValidator({
callbacks: {
validateGopayPaymentMethod: function () {
// JS validation is not necessary
}
}
});
}

(new Register()).registerCallback(gopayPaymentMethodValidator, 'gopayPaymentMethodValidator');
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"endroid/qr-code": "^3.9.6",
"fakerphp/faker": "^1.19.0",
"friendsofsymfony/ckeditor-bundle": "^2.1",
"gopay/payments-sdk-php": ">=1.4 <1.7",
"gopay/payments-sdk-php": "^1.7",
"gedmo/doctrine-extensions": "^3.5",
"gordalina/cachetool": "^8.5",
"guzzlehttp/guzzle": "^7.5",
Expand Down
117 changes: 95 additions & 22 deletions src/Form/Admin/Payment/PaymentFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
namespace Shopsys\FrameworkBundle\Form\Admin\Payment;

use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Shopsys\FormTypesBundle\MultidomainType;
use Shopsys\FormTypesBundle\YesNoType;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Form\DisplayOnlyType;
use Shopsys\FrameworkBundle\Form\DomainsType;
use Shopsys\FrameworkBundle\Form\GroupType;
use Shopsys\FrameworkBundle\Form\ImageUploadType;
use Shopsys\FrameworkBundle\Form\Locale\LocalizedType;
use Shopsys\FrameworkBundle\Form\PriceAndVatTableByDomainsType;
use Shopsys\FrameworkBundle\Form\WarningMessageType;
use Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethod;
use Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethodFacade;
use Shopsys\FrameworkBundle\Model\Payment\Payment;
use Shopsys\FrameworkBundle\Model\Payment\PaymentData;
Expand All @@ -24,18 +28,22 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

class PaymentFormType extends AbstractType
{
/**
* @param \Shopsys\FrameworkBundle\Model\Transport\TransportFacade $transportFacade
* @param \Shopsys\FrameworkBundle\Model\Payment\PaymentFacade $paymentFacade
* @param \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethodFacade $goPayPaymentMethodFacade
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
*/
public function __construct(
private readonly TransportFacade $transportFacade,
private readonly PaymentFacade $paymentFacade,
private readonly GoPayPaymentMethodFacade $goPayPaymentMethodFacade,
private readonly Domain $domain,
) {
}

Expand Down Expand Up @@ -75,7 +83,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'required' => false,
'label' => t('Display on'),
])
->add('hidden', YesNoType::class, $this->getHiddenFieldOptions($payment))
->add('hidden', YesNoType::class, [
'required' => false,
'label' => t('Hidden'),
])
->add('transports', ChoiceType::class, [
'required' => false,
'choices' => $this->transportFacade->getAll(),
Expand All @@ -99,19 +110,35 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'class' => 'js-payment-type',
],
])
->add('goPayPaymentMethod', ChoiceType::class, [
->add('goPayPaymentMethodByDomainId', MultidomainType::class, [
'entry_type' => ChoiceType::class,
'options_by_domain_id' => $this->getGopayPaymentMethodOptionsByDomainId(),
'entry_options' => [
'placeholder' => '---',
'choice_label' => 'name',
'choice_value' => 'id',
'multiple' => false,
'expanded' => false,
'required' => true,
],
'label' => t('GoPay payment method'),
'choices' => $this->goPayPaymentMethodFacade->getAll(),
'choice_label' => 'name',
'choice_value' => 'id',
'multiple' => false,
'expanded' => false,
'required' => true,
'macro' => [
'name' => 'seoFormRowMacros.multidomainRow',
'recommended_length' => null,
],
'attr' => [
'class' => 'js-payment-gopay-payment-method',
],
]);

if ($payment !== null) {
$this->addHiddenByGoPayWarning(
$options['data'],
$builderBasicInformationGroup,
);
}

$builderPriceGroup = $builder->create('prices', GroupType::class, [
'label' => t('Prices'),
]);
Expand Down Expand Up @@ -178,6 +205,27 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('save', SubmitType::class);
}

/**
* @return array
*/
private function getGopayPaymentMethodOptionsByDomainId(): array
{
$allGoPayPaymentMethods = $this->goPayPaymentMethodFacade->getAll();
$optionsByDomainId = [];

foreach ($allGoPayPaymentMethods as $goPayPaymentMethod) {
$optionsByDomainId[$goPayPaymentMethod->getDomainId()]['choices'][] = $goPayPaymentMethod;
}

foreach ($optionsByDomainId as $domainId => $options) {
$optionsByDomainId[$domainId]['group_by'] = function (GoPayPaymentMethod $goPayPaymentMethod): string {
return $goPayPaymentMethod->isAvailable() ? t('Available') : t('Hidden in GoPay');
};
}

return $optionsByDomainId;
}

/**
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
*/
Expand All @@ -188,28 +236,53 @@ public function configureOptions(OptionsResolver $resolver): void
->setDefaults([
'data_class' => PaymentData::class,
'attr' => ['novalidate' => 'novalidate'],
'constraints' => [
new Callback([$this, 'validateGopayPaymentMethod']),
],
]);
}

/**
* @param \Shopsys\FrameworkBundle\Model\Payment\Payment|null $payment
* @return array
* @param \Shopsys\FrameworkBundle\Model\Payment\PaymentData $paymentData
* @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
*/
public function validateGopayPaymentMethod(PaymentData $paymentData, ExecutionContextInterface $context): void
{
if ($paymentData->type !== Payment::TYPE_GOPAY) {
return;
}

foreach ($paymentData->enabled as $domainId => $enabled) {
if ($enabled && $paymentData->goPayPaymentMethodByDomainId[$domainId] === null) {
$context->buildViolation('Please select GoPay payment method for enabled domain ' . $this->domain->getDomainConfigById($domainId)->getName())
->atPath('goPayPaymentMethodByDomainId[1]')
->addViolation();
}
}
}

/**
* @param \Shopsys\FrameworkBundle\Model\Payment\PaymentData $paymentData
* @param \Symfony\Component\Form\FormBuilderInterface $builder
*/
private function getHiddenFieldOptions(?Payment $payment): array
public function addHiddenByGoPayWarning(PaymentData $paymentData, FormBuilderInterface $builder): void
{
$hiddenFieldOptions = [
'required' => false,
'label' => t('Hidden'),
];

if ($payment !== null && $payment->isHiddenByGoPay()) {
$hiddenFieldOptions['attr'] = [
'disabled' => true,
'icon' => true,
'iconTitle' => t('This payment method is hidden by GoPay.'),
];
$domainIdsWithHiddenByGoPay = array_keys(array_filter($paymentData->hiddenByGoPay));
$domainNames = [];

if (count($domainIdsWithHiddenByGoPay) === 0) {
return;
}

foreach ($domainIdsWithHiddenByGoPay as $domainId) {
$domainNames[] = $this->domain->getDomainConfigById($domainId)->getName();
}

return $hiddenFieldOptions;
$builder->add('hiddenByGoPay', WarningMessageType::class, [
'data' => t('This payment method is hidden by GoPay on domains: %domains%', [
'%domains%' => implode(', ', $domainNames),
]),
'position' => ['after' => 'enabled'],
]);
}
}
66 changes: 66 additions & 0 deletions src/Migrations/Version20240803005343.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;

class Version20240803005343 extends AbstractMigration implements ContainerAwareInterface
{
use MultidomainMigrationTrait;

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function up(Schema $schema): void
{
$this->sql('DROP INDEX gopay_payment_method_unique');
$this->sql('ALTER TABLE gopay_payment_methods ADD domain_id INT DEFAULT NULL');
$this->sql('CREATE UNIQUE INDEX gopay_payment_method_unique ON gopay_payment_methods (domain_id, identifier)');

foreach ($this->getAllDomainIds() as $domainId) {
$locale = $this->getDomainLocale($domainId);

$this->sql(
'UPDATE gopay_payment_methods SET domain_id = :domainId WHERE LOWER(name) LIKE \'[' . strtolower($locale) . ']%\'',
['domainId' => $domainId],
);
}
$this->sql('ALTER TABLE gopay_payment_methods ALTER domain_id SET NOT NULL');

$this->sql('ALTER TABLE payment_domains ADD go_pay_payment_method_id INT DEFAULT NULL');
$this->sql('
ALTER TABLE
payment_domains
ADD
CONSTRAINT FK_9532B177B1E3A4E9 FOREIGN KEY (go_pay_payment_method_id) REFERENCES gopay_payment_methods (id) ON DELETE
SET
NULL NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->sql('CREATE INDEX IDX_9532B177B1E3A4E9 ON payment_domains (go_pay_payment_method_id)');
$this->sql('UPDATE payment_domains
SET go_pay_payment_method_id = payments.go_pay_payment_method_id
FROM payments
WHERE payment_domains.payment_id = payments.id AND payment_domains.enabled = TRUE
');
$this->sql('ALTER TABLE payments DROP COLUMN go_pay_payment_method_id');

$this->sql('ALTER TABLE payment_domains ADD hidden_by_go_pay BOOLEAN NOT NULL DEFAULT FALSE');
$this->sql('UPDATE payment_domains
SET hidden_by_go_pay = payments.hidden_by_go_pay
FROM payments
WHERE payment_domains.payment_id = payments.id AND payment_domains.enabled = TRUE
');
$this->sql('ALTER TABLE payment_domains ALTER COLUMN hidden_by_go_pay DROP DEFAULT ');
$this->sql('ALTER TABLE payments DROP COLUMN hidden_by_go_pay');
}

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function down(Schema $schema): void
{
}
}
27 changes: 27 additions & 0 deletions src/Migrations/Version20240822144131.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;

class Version20240822144131 extends AbstractMigration
{
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function up(Schema $schema): void
{
$this->sql('ALTER TABLE gopay_payment_methods ADD available BOOLEAN NOT NULL DEFAULT TRUE');
$this->sql('ALTER TABLE gopay_payment_methods ALTER available DROP DEFAULT');
}

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function down(Schema $schema): void
{
}
}
11 changes: 11 additions & 0 deletions src/Model/GoPay/Exception/GoPayNotEnabledOnDomainException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Model\GoPay\Exception;

use Exception;

class GoPayNotEnabledOnDomainException extends Exception
{
}
Loading

0 comments on commit d29d0ef

Please sign in to comment.