Skip to content

Commit

Permalink
Introduce a separate gateway config for Pay-by-Link payment method
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Nov 20, 2024
1 parent f9bda50 commit fda22c7
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 50 deletions.
6 changes: 3 additions & 3 deletions config/config/sylius_fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@
'pbl' => [
'code' => 'tpay_pbl',
'name' => 'Pay by Link (Tpay)',
'gatewayFactory' => 'tpay',
'gatewayName' => 'tpay',
'gatewayConfig' => $tpayConfig + ['type' => PaymentType::PAY_BY_LINK],
'gatewayFactory' => 'tpay_pbl',
'gatewayName' => 'tpay_pbl',
'gatewayConfig' => $tpayConfig,
'channels' => [
'FASHION_WEB',
],
Expand Down
9 changes: 9 additions & 0 deletions config/services/pay_by_link_payment/form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function(ContainerConfigurator $container): void {
$container->import('form/**/*.php');
};
19 changes: 19 additions & 0 deletions config/services/pay_by_link_payment/form/type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;


use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Form\Type\GatewayConfigurationType;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GatewayFactory;

return static function(ContainerConfigurator $container): void {
$services = $container->services();

$services->set('commerce_weavers_sylius_tpay.pay_by_link_payment.form.type.gateway_configuration', GatewayConfigurationType::class)
->parent('commerce_weavers_sylius_tpay.form.type.abstract_tpay_gateway_configuration')
->tag('sylius.gateway_configuration_type', ['label' => 'commerce_weavers_sylius_tpay.admin.gateway_name.tpay_pbl', 'type' => GatewayFactory::NAME])
->tag('form.type')
;
};
6 changes: 3 additions & 3 deletions config/services/pay_by_link_payment/payum/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Action\CreatePayByLinkTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Action\GetTpayTransactionsChannelsAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GatewayFactory;

return static function(ContainerConfigurator $container): void {
$services = $container->services();
Expand All @@ -20,10 +20,10 @@
service('commerce_weavers_sylius_tpay.tpay.factory.create_pay_by_link_payment_payload'),
service('commerce_weavers_sylius_tpay.payum.factory.token.notify'),
])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.create_pay_by_link_transaction'])
->tag('payum.action', ['factory' => GatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.create_pay_by_link_transaction'])
;

$services->set(GetTpayTransactionsChannelsAction::class)
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.get_transactions_channels'])
->tag('payum.action', ['factory' => GatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.get_transactions_channels'])
;
};
9 changes: 9 additions & 0 deletions config/services/pay_by_link_payment/payum/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GetTpayTransactionsChannelsFactory;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GetTpayTransactionsChannelsFactoryInterface;
use Payum\Core\Bridge\Symfony\Builder\GatewayFactoryBuilder;

return static function(ContainerConfigurator $container): void {
$services = $container->services();

$services->set('commerce_weavers_sylius_tpay.pay_by_link_payment.payum.factory.gateway', GatewayFactoryBuilder::class)
->args([
GatewayFactory::class,
])
->tag('payum.gateway_factory_builder', ['factory' => GatewayFactory::NAME])
;

$services->set('commerce_weavers_sylius_tpay.pay_by_link_payment.payum.factory.get_tpay_transactions_channels', GetTpayTransactionsChannelsFactory::class)
->alias(GetTpayTransactionsChannelsFactoryInterface::class, 'commerce_weavers_sylius_tpay.pay_by_link_payment.payum.factory.get_tpay_transactions_channels')
;
Expand Down
7 changes: 7 additions & 0 deletions config/services/payum/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CommerceWeavers\SyliusTpayPlugin\BlikPayment\Payum\Factory\GatewayFactory as BlikGatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\CardPayment\Payum\Factory\GatewayFactory as CardGatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GatewayFactory as PayByLinkGatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateApplePayTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateGooglePayTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateRedirectBasedTransactionAction;
Expand All @@ -31,6 +32,7 @@
])
->tag('payum.action', ['factory' => BlikGatewayFactory::NAME, 'alias' => 'cw.tpay_blik.capture'])
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.capture'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.capture'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.capture'])
;

Expand Down Expand Up @@ -74,24 +76,28 @@
])
->tag('payum.action', ['factory' => BlikGatewayFactory::NAME, 'alias' => 'cw.tpay_blik.notify'])
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.notify'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.notify'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.notify'])
;

$services->set(GetStatusAction::class)
->tag('payum.action', ['factory' => BlikGatewayFactory::NAME, 'alias' => 'cw.tpay_blik.get_status'])
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.get_status'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.get_status'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.get_status'])
;

$services->set(PartialRefundAction::class)
->tag('payum.action', ['factory' => BlikGatewayFactory::NAME, 'alias' => 'cw.tpay_blik.partial_refund'])
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.partial_refund'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.partial_refund'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.partial_refund'])
;

$services->set(RefundAction::class)
->tag('payum.action', ['factory' => BlikGatewayFactory::NAME, 'alias' => 'cw.tpay_blik.refund'])
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.refund'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.refund'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.refund'])
;

Expand All @@ -105,6 +111,7 @@
$services->set(ResolveNextRouteAction::class)
->tag('payum.action', ['factory' => BlikGatewayFactory::NAME, 'alias' => 'cw.tpay_blik.resolve_next_route'])
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.resolve_next_route'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.resolve_next_route'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.resolve_next_route'])
;
};
11 changes: 11 additions & 0 deletions src/PayByLinkPayment/Form/Type/GatewayConfigurationType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Form\Type;

use CommerceWeavers\SyliusTpayPlugin\Form\Type\AbstractTpayGatewayConfigurationType;

final class GatewayConfigurationType extends AbstractTpayGatewayConfigurationType
{
}
17 changes: 17 additions & 0 deletions src/PayByLinkPayment/Payum/Factory/GatewayFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory;

use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory;

final class GatewayFactory extends TpayGatewayFactory
{
public const NAME = 'tpay_pbl';

public function getName(): string
{
return self::NAME;
}
}
27 changes: 14 additions & 13 deletions src/Tpay/Provider/ValidTpayChannelListProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Provider;

use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Exception\UnableToGetBankListException;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\Repository\PaymentMethodRepositoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PayGroup;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
Expand All @@ -31,8 +32,6 @@ public function __construct(

public function provide(): array
{
$availableChannels = $this->availableTpayApiBankListProvider->provide();

/** @var PaymentMethodInterface[] $paymentMethods */
$paymentMethods = $this->paymentMethodRepository->findByChannelAndGatewayConfigNameWithGatewayConfig(
$this->channelContext->getChannel(),
Expand All @@ -41,8 +40,20 @@ public function provide(): array

Assert::notEmpty($paymentMethods, 'There is no payment method of Tpay type available');

/** @var PaymentMethodInterface[] $payByLinkPaymentMethods */
$payByLinkPaymentMethods = $this->paymentMethodRepository->findByChannelAndGatewayConfigNameWithGatewayConfig(
$this->channelContext->getChannel(),
GatewayFactory::NAME,
);

if ([] === $payByLinkPaymentMethods) {
throw new UnableToGetBankListException(
'Bank list cannot be retrieved if there is no payment method with PayByLink type configured',
);
}

$availableChannels = $this->availableTpayApiBankListProvider->provide();
$paymentMethodsToRemoveByGroupId = [];
$hasPblPaymentAvailable = false;
foreach ($paymentMethods as $paymentMethod) {
/** @var (GatewayConfigInterface&CryptedInterface)|null $tpayGatewayConfig */
$tpayGatewayConfig = $paymentMethod->getGatewayConfig();
Expand All @@ -58,10 +69,6 @@ public function provide(): array
continue;
}

if ($hasPblPaymentAvailable === false && $config['type'] === PaymentType::PAY_BY_LINK) {
$hasPblPaymentAvailable = true;
}

match ($config['type']) {
PaymentType::VISA_MOBILE => array_push(
$paymentMethodsToRemoveByGroupId,
Expand All @@ -76,12 +83,6 @@ public function provide(): array
};
}

if (!$hasPblPaymentAvailable) {
throw new UnableToGetBankListException(
'Bank list cannot be retrieved if there is no payment method with PayByLink type configured',
);
}

return array_filter($availableChannels, static function (array $channel) use ($paymentMethodsToRemoveByGroupId): bool {
$groupId = (int) $channel['groups'][0]['id'];

Expand Down
3 changes: 2 additions & 1 deletion src/Tpay/Resolver/TpayTransactionChannelResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Resolver;

use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GetTpayTransactionsChannelsFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Request\GetTpayTransactionsChannels;
use Payum\Core\Model\ArrayObject;
Expand All @@ -30,7 +31,7 @@ public function __construct(

public function resolve(): array
{
$gateway = $this->payum->getGateway('tpay');
$gateway = $this->payum->getGateway(GatewayFactory::NAME);

$value = $this->getTpayTransactionsChannelsFactory?->createNewEmpty()
?? new GetTpayTransactionsChannels(new ArrayObject());
Expand Down
3 changes: 1 addition & 2 deletions templates/shop/cart/complete/_payByLink.html.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{% set payment = order.lastCartPayment() %}

{% if payment is not null and
cw_tpay_get_gateway_config_value(payment.method.gatewayConfig, 'type') == constant('CommerceWeavers\\SyliusTpayPlugin\\Tpay\\PaymentType::PAY_BY_LINK')
{% if payment is not null and payment.method.gatewayConfig.gatewayName == 'tpay_pbl'
%}
<div class="bank-container">
{% for bank in banks %}
Expand Down
4 changes: 1 addition & 3 deletions templates/shop/order/pay/_payByLink.html.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{% if cw_tpay_get_gateway_config_value(method.gatewayConfig, 'type') == constant('CommerceWeavers\\SyliusTpayPlugin\\Tpay\\PaymentType::PAY_BY_LINK')
and form.vars.value.state != 'cart'
%}
{% if method.gatewayConfig.gatewayName == 'tpay_pbl' and form.vars.value.state != 'cart' %}
<div class="ui stackable grid" style="display: none;" data-payment-details="pay_by_link">
{% include '@CommerceWeaversSyliusTpayPlugin/shop/payment/_payByLink.html.twig' %}
</div>
Expand Down
6 changes: 2 additions & 4 deletions tests/Api/DataFixtures/shop/blik_payment_method.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Sylius\Bundle\PayumBundle\Model\GatewayConfig:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
cards_api: 'encrypted_<getenv("TPAY_CARDS_API")>'
type: 'card'
production_mode: false
encrypted: true
gateway_tpay_blik:
Expand All @@ -62,12 +61,11 @@ Sylius\Bundle\PayumBundle\Model\GatewayConfig:
production_mode: false
encrypted: true
gateway_tpay_pbl:
gatewayName: 'tpay'
factoryName: 'tpay'
gatewayName: 'tpay_pbl'
factoryName: 'tpay_pbl'
config:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
type: 'pay_by_link'
production_mode: false
encrypted: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ Sylius\Bundle\PayumBundle\Model\GatewayConfig:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
cards_api: 'encrypted_<getenv("TPAY_CARDS_API")>'
type: 'card'
production_mode: false
encrypted: true
gateway_tpay_blik:
Expand All @@ -64,12 +63,11 @@ Sylius\Bundle\PayumBundle\Model\GatewayConfig:
production_mode: false
encrypted: true
gateway_tpay_pbl:
gatewayName: 'tpay'
factoryName: 'tpay'
gatewayName: 'tpay_pbl'
factoryName: 'tpay_pbl'
config:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
type: 'pay_by_link'
production_mode: false
encrypted: true
gateway_tpay_visa_mobile:
Expand Down
4 changes: 2 additions & 2 deletions tests/E2E/Resources/fixtures/common/payment_method.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ Sylius\Bundle\PayumBundle\Model\GatewayConfig:
production_mode: false
encrypted: true
gateway_tpay_pbl:
gatewayName: 'tpay'
factoryName: 'tpay'
gatewayName: 'tpay_pbl'
factoryName: 'tpay_pbl'
config:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
Expand Down
32 changes: 22 additions & 10 deletions tests/Unit/Tpay/Provider/ValidTpayChannelListProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,20 @@ public function test_it_throws_exception_if_there_is_no_gateway_config_that_is_p
$this->expectExceptionMessage('Bank list cannot be retrieved if there is no payment method with PayByLink type configured');

$notTpayBasedPaymentMethod = $this->prophesize(PaymentMethodInterface::class);
$notTpayBasedGatewayConfig = $this->prophesize(GatewayConfig::class);
$notTpayBasedGatewayConfigConfig = [
'type' => 'visa_mobile',
];
$notTpayBasedPaymentMethod->getGatewayConfig()->willReturn($notTpayBasedGatewayConfig);

$this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST);

$channel = $this->prophesize(ChannelInterface::class);
$this->channelContext->getChannel()->willReturn($channel->reveal());

$notTpayBasedGatewayConfig->decrypt($this->cypher)->shouldBeCalled();
$notTpayBasedGatewayConfig->getConfig()->willReturn($notTpayBasedGatewayConfigConfig);

$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay')
->willReturn([
$notTpayBasedPaymentMethod
])
;
$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay_pbl')
->willReturn([])
;

$this->createTestSubject()->provide();
}
Expand Down Expand Up @@ -167,6 +161,12 @@ public function test_it_returns_all_available_payments_if_only_tpay_payment_meth
$tpayBasedPaymentMethod->reveal(),
])
;
$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay_pbl')
->willReturn([
$this->prophesize(PaymentMethodInterface::class)->reveal(),
])
;

$result = $this->createTestSubject()->provide();

Expand Down Expand Up @@ -205,6 +205,12 @@ public function test_it_returns_valid_payments_according_to_available_tpay_payme
$anotherTpayPblPaymentMethod->reveal(),
])
;
$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay_pbl')
->willReturn([
$this->prophesize(PaymentMethodInterface::class)->reveal(),
])
;

$result = $this->createTestSubject()->provide();

Expand Down Expand Up @@ -247,6 +253,12 @@ public function test_it_returns_valid_payments_even_if_gateway_config_lacks_type
$anotherTpayPblPaymentMethod->reveal(),
])
;
$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay_pbl')
->willReturn([
$this->prophesize(PaymentMethodInterface::class)->reveal(),
])
;

$result = $this->createTestSubject()->provide();

Expand Down
Loading

0 comments on commit fda22c7

Please sign in to comment.