Skip to content

Commit

Permalink
added Convertim payment types
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik committed Dec 10, 2024
1 parent 8bee1dc commit 081f883
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/DependencyInjection/ShopsysFrameworkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
11 changes: 6 additions & 5 deletions src/Form/Admin/Payment/PaymentFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Model/GoPay/GoPayRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
28 changes: 28 additions & 0 deletions src/Model/Payment/AbstractPaymentTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Model\Payment;

use Shopsys\FrameworkBundle\Component\Enum\AbstractEnum;

abstract class AbstractPaymentTypeEnum extends AbstractEnum
{
/**
* @return string[]
*/
public function getEnabledInCartCases(): array
{
return $this->shouldBeDisplayedInDefaultEshopCart() ? $this->getAllCases() : [];
}

/**
* @return bool
*/
abstract public function shouldBeDisplayedInDefaultEshopCart(): bool;

/**
* @return array<string, string>
*/
abstract public function getAllIndexedByTranslations(): array;
}
10 changes: 4 additions & 6 deletions src/Model/Payment/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -354,7 +352,7 @@ public function isCzkRounding()
*/
public function isGoPay(): bool
{
return $this->type === self::TYPE_GOPAY;
return $this->type === PaymentTypeEnum::TYPE_GOPAY;
}

/**
Expand Down Expand Up @@ -496,7 +494,7 @@ public function getUuid()
protected function getGatewayPayments(): array
{
return [
self::TYPE_GOPAY,
PaymentTypeEnum::TYPE_GOPAY,
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Model/Payment/PaymentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ public function __construct()
$this->vatsIndexedByDomainId = [];
$this->goPayPaymentMethodByDomainId = [];
$this->hiddenByGoPay = [];
$this->type = Payment::TYPE_BASIC;
$this->type = PaymentTypeEnum::TYPE_BASIC;
}
}
30 changes: 30 additions & 0 deletions src/Model/Payment/PaymentTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Model\Payment;

class PaymentTypeEnum extends AbstractPaymentTypeEnum
{
public const string TYPE_BASIC = 'basic';
public const string TYPE_GOPAY = 'goPay';

/**
* @return array<string, string>
*/
public function getAllIndexedByTranslations(): array
{
return [
t('Basic') => self::TYPE_BASIC,
t('GoPay') => self::TYPE_GOPAY,
];
}

/**
* @return bool
*/
public function shouldBeDisplayedInDefaultEshopCart(): bool
{
return true;
}
}
44 changes: 44 additions & 0 deletions src/Model/Payment/PaymentTypeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Model\Payment;

class PaymentTypeProvider
{
/**
* @param iterable<\Shopsys\FrameworkBundle\Model\Payment\AbstractPaymentTypeEnum> $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<string, string>
*/
public function getAllEnabledInCartIndexedByTranslations(): array
{
$enabledInCartCases = [];

foreach ($this->paymentTypeEnums as $transportTypeEnum) {
$enabledInCartCases = array_merge($enabledInCartCases, $transportTypeEnum->getEnabledInCartCases());
}

return $enabledInCartCases;
}
}
4 changes: 2 additions & 2 deletions src/Model/Payment/Service/PaymentServiceFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 081f883

Please sign in to comment.