From 081f88354b5a0dbe38bb80ec1380bbb5fc2504f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Ludvik?= Date: Wed, 27 Nov 2024 18:10:23 +0100 Subject: [PATCH] added Convertim payment types --- .../ShopsysFrameworkExtension.php | 4 ++ src/Form/Admin/Payment/PaymentFormType.php | 11 ++--- src/Model/GoPay/GoPayRepository.php | 3 +- src/Model/Payment/AbstractPaymentTypeEnum.php | 28 ++++++++++++ src/Model/Payment/Payment.php | 10 ++--- src/Model/Payment/PaymentData.php | 2 +- src/Model/Payment/PaymentTypeEnum.php | 30 +++++++++++++ src/Model/Payment/PaymentTypeProvider.php | 44 +++++++++++++++++++ .../Payment/Service/PaymentServiceFacade.php | 4 +- src/Resources/config/services.yaml | 4 ++ 10 files changed, 125 insertions(+), 15 deletions(-) create mode 100644 src/Model/Payment/AbstractPaymentTypeEnum.php create mode 100644 src/Model/Payment/PaymentTypeEnum.php create mode 100644 src/Model/Payment/PaymentTypeProvider.php diff --git a/src/DependencyInjection/ShopsysFrameworkExtension.php b/src/DependencyInjection/ShopsysFrameworkExtension.php index 1007fc21f2..26fc329fc3 100644 --- a/src/DependencyInjection/ShopsysFrameworkExtension.php +++ b/src/DependencyInjection/ShopsysFrameworkExtension.php @@ -12,6 +12,7 @@ use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface; use Shopsys\FrameworkBundle\Model\Order\Processing\OrderProcessingStack; use Shopsys\FrameworkBundle\Model\Transport\AbstractTransportTypeEnum; +use Shopsys\FrameworkBundle\Model\Payment\AbstractPaymentTypeEnum; use Shopsys\FrameworkBundle\Twig\NoVarDumperExtension; use Shopsys\FrameworkBundle\Twig\VarDumperExtension; use Symfony\Component\Config\FileLocator; @@ -63,6 +64,9 @@ public function load(array $configs, ContainerBuilder $container): void $container->registerForAutoconfiguration(AbstractTransportTypeEnum::class) ->addTag('shopsys.transport_type_enum'); + $container->registerForAutoconfiguration(AbstractPaymentTypeEnum::class) + ->addTag('shopsys.payment_type_enum'); + $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); diff --git a/src/Form/Admin/Payment/PaymentFormType.php b/src/Form/Admin/Payment/PaymentFormType.php index c5783bcdc4..c86bc9460e 100644 --- a/src/Form/Admin/Payment/PaymentFormType.php +++ b/src/Form/Admin/Payment/PaymentFormType.php @@ -20,6 +20,8 @@ use Shopsys\FrameworkBundle\Model\Payment\Payment; use Shopsys\FrameworkBundle\Model\Payment\PaymentData; use Shopsys\FrameworkBundle\Model\Payment\PaymentFacade; +use Shopsys\FrameworkBundle\Model\Payment\PaymentTypeEnum; +use Shopsys\FrameworkBundle\Model\Payment\PaymentTypeProvider; use Shopsys\FrameworkBundle\Model\Transport\Transport; use Shopsys\FrameworkBundle\Model\Transport\TransportFacade; use Symfony\Component\Form\AbstractType; @@ -39,12 +41,14 @@ class PaymentFormType extends AbstractType * @param \Shopsys\FrameworkBundle\Model\Payment\PaymentFacade $paymentFacade * @param \Shopsys\FrameworkBundle\Model\GoPay\PaymentMethod\GoPayPaymentMethodFacade $goPayPaymentMethodFacade * @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain + * @param \Shopsys\FrameworkBundle\Model\Payment\PaymentTypeProvider $paymentTypeProvider */ public function __construct( private readonly TransportFacade $transportFacade, private readonly PaymentFacade $paymentFacade, private readonly GoPayPaymentMethodFacade $goPayPaymentMethodFacade, private readonly Domain $domain, + private readonly PaymentTypeProvider $paymentTypeProvider, ) { } @@ -101,10 +105,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ]) ->add('type', ChoiceType::class, [ 'label' => t('Type'), - 'choices' => [ - t('Basic') => Payment::TYPE_BASIC, - t('GoPay') => Payment::TYPE_GOPAY, - ], + 'choices' => $this->paymentTypeProvider->getAllIndexedByTranslations(), 'multiple' => false, 'expanded' => false, 'required' => true, @@ -260,7 +261,7 @@ public function configureOptions(OptionsResolver $resolver): void */ public function validateGopayPaymentMethod(PaymentData $paymentData, ExecutionContextInterface $context): void { - if ($paymentData->type !== Payment::TYPE_GOPAY) { + if ($paymentData->type !== PaymentTypeEnum::TYPE_GOPAY) { return; } diff --git a/src/Model/GoPay/GoPayRepository.php b/src/Model/GoPay/GoPayRepository.php index 614e3bf42f..de93f1f7cd 100644 --- a/src/Model/GoPay/GoPayRepository.php +++ b/src/Model/GoPay/GoPayRepository.php @@ -10,6 +10,7 @@ use GoPay\Definition\Response\PaymentStatus; use Shopsys\FrameworkBundle\Model\Order\OrderRepository; use Shopsys\FrameworkBundle\Model\Payment\Payment; +use Shopsys\FrameworkBundle\Model\Payment\PaymentTypeEnum; use Shopsys\FrameworkBundle\Model\Payment\Transaction\PaymentTransaction; class GoPayRepository @@ -39,7 +40,7 @@ public function getAllUnpaidGoPayOrders(DateTime $fromDate): array ->orderBy('o.createdAt', 'ASC') ->setParameter('fromDate', $fromDate) ->setParameter('paymentStatuses', [PaymentStatus::PAID, PaymentStatus::CANCELED, PaymentStatus::TIMEOUTED]) - ->setParameter('type', Payment::TYPE_GOPAY); + ->setParameter('type', PaymentTypeEnum::TYPE_GOPAY); return $queryBuilder->getQuery()->execute(); } diff --git a/src/Model/Payment/AbstractPaymentTypeEnum.php b/src/Model/Payment/AbstractPaymentTypeEnum.php new file mode 100644 index 0000000000..c6199d7d0c --- /dev/null +++ b/src/Model/Payment/AbstractPaymentTypeEnum.php @@ -0,0 +1,28 @@ +shouldBeDisplayedInDefaultEshopCart() ? $this->getAllCases() : []; + } + + /** + * @return bool + */ + abstract public function shouldBeDisplayedInDefaultEshopCart(): bool; + + /** + * @return array + */ + abstract public function getAllIndexedByTranslations(): array; +} diff --git a/src/Model/Payment/Payment.php b/src/Model/Payment/Payment.php index 89fd29c243..eef4e99f3e 100644 --- a/src/Model/Payment/Payment.php +++ b/src/Model/Payment/Payment.php @@ -25,9 +25,7 @@ */ class Payment extends AbstractTranslatableEntity implements OrderableEntityInterface { - protected const GEDMO_SORTABLE_LAST_POSITION = -1; - public const TYPE_GOPAY = 'goPay'; - public const TYPE_BASIC = 'basic'; + protected const int GEDMO_SORTABLE_LAST_POSITION = -1; /** * @var int @@ -135,7 +133,7 @@ protected function setData(PaymentData $paymentData): void $this->czkRounding = $paymentData->czkRounding; $this->type = $paymentData->type; - if ($paymentData->type !== self::TYPE_GOPAY) { + if ($paymentData->type !== PaymentTypeEnum::TYPE_GOPAY) { $this->resetGopayPaymentMethods(); } else { foreach ($this->domains as $paymentDomain) { @@ -354,7 +352,7 @@ public function isCzkRounding() */ public function isGoPay(): bool { - return $this->type === self::TYPE_GOPAY; + return $this->type === PaymentTypeEnum::TYPE_GOPAY; } /** @@ -496,7 +494,7 @@ public function getUuid() protected function getGatewayPayments(): array { return [ - self::TYPE_GOPAY, + PaymentTypeEnum::TYPE_GOPAY, ]; } diff --git a/src/Model/Payment/PaymentData.php b/src/Model/Payment/PaymentData.php index 4d9cbdc56e..dbba91bce8 100644 --- a/src/Model/Payment/PaymentData.php +++ b/src/Model/Payment/PaymentData.php @@ -89,6 +89,6 @@ public function __construct() $this->vatsIndexedByDomainId = []; $this->goPayPaymentMethodByDomainId = []; $this->hiddenByGoPay = []; - $this->type = Payment::TYPE_BASIC; + $this->type = PaymentTypeEnum::TYPE_BASIC; } } diff --git a/src/Model/Payment/PaymentTypeEnum.php b/src/Model/Payment/PaymentTypeEnum.php new file mode 100644 index 0000000000..8e1087be07 --- /dev/null +++ b/src/Model/Payment/PaymentTypeEnum.php @@ -0,0 +1,30 @@ + + */ + public function getAllIndexedByTranslations(): array + { + return [ + t('Basic') => self::TYPE_BASIC, + t('GoPay') => self::TYPE_GOPAY, + ]; + } + + /** + * @return bool + */ + public function shouldBeDisplayedInDefaultEshopCart(): bool + { + return true; + } +} diff --git a/src/Model/Payment/PaymentTypeProvider.php b/src/Model/Payment/PaymentTypeProvider.php new file mode 100644 index 0000000000..2703255865 --- /dev/null +++ b/src/Model/Payment/PaymentTypeProvider.php @@ -0,0 +1,44 @@ + $paymentTypeEnums + */ + public function __construct( + protected readonly iterable $paymentTypeEnums, + ) { + } + + /** + * @return array + */ + public function getAllIndexedByTranslations(): array + { + $allIndexedByTranslations = []; + + foreach ($this->paymentTypeEnums as $transportTypeEnum) { + $allIndexedByTranslations = array_merge($allIndexedByTranslations, $transportTypeEnum->getAllIndexedByTranslations()); + } + + return $allIndexedByTranslations; + } + + /** + * @return array + */ + public function getAllEnabledInCartIndexedByTranslations(): array + { + $enabledInCartCases = []; + + foreach ($this->paymentTypeEnums as $transportTypeEnum) { + $enabledInCartCases = array_merge($enabledInCartCases, $transportTypeEnum->getEnabledInCartCases()); + } + + return $enabledInCartCases; + } +} diff --git a/src/Model/Payment/Service/PaymentServiceFacade.php b/src/Model/Payment/Service/PaymentServiceFacade.php index 433587b59b..c0538352f6 100644 --- a/src/Model/Payment/Service/PaymentServiceFacade.php +++ b/src/Model/Payment/Service/PaymentServiceFacade.php @@ -12,9 +12,9 @@ use Shopsys\FrameworkBundle\Model\GoPay\Exception\GoPayPaymentDownloadException; use Shopsys\FrameworkBundle\Model\GoPay\GoPayFacade; use Shopsys\FrameworkBundle\Model\Order\Order; -use Shopsys\FrameworkBundle\Model\Payment\Payment; use Shopsys\FrameworkBundle\Model\Payment\PaymentSetupCreationData; use Shopsys\FrameworkBundle\Model\Payment\PaymentSetupCreationDataFactory; +use Shopsys\FrameworkBundle\Model\Payment\PaymentTypeEnum; use Shopsys\FrameworkBundle\Model\Payment\Service\Exception\PaymentServiceFacadeNotRegisteredException; use Shopsys\FrameworkBundle\Model\Payment\Transaction\Exception\PaymentTransactionHasNoAssignedPayment; use Shopsys\FrameworkBundle\Model\Payment\Transaction\PaymentTransaction; @@ -48,7 +48,7 @@ public function __construct( protected readonly PaymentSetupCreationDataFactory $paymentSetupCreationDataFactory, ) { $this->paymentServices = []; - $this->paymentServices[Payment::TYPE_GOPAY] = $goPayFacade; + $this->paymentServices[PaymentTypeEnum::TYPE_GOPAY] = $goPayFacade; } /** diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 30f00bc63a..7dbdf525e5 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -1129,3 +1129,7 @@ services: Shopsys\FrameworkBundle\Model\Transport\TransportTypeProvider: arguments: $transportTypeEnums: !tagged shopsys.transport_type_enum + + Shopsys\FrameworkBundle\Model\Payment\PaymentTypeProvider: + arguments: + $paymentTypeEnums: !tagged shopsys.payment_type_enum