Skip to content

Commit

Permalink
Extract Tpay Redirect as a separate payment method
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Nov 25, 2024
1 parent 75ed3e2 commit 0796792
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 76 deletions.
10 changes: 5 additions & 5 deletions config/config/sylius_fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@
'options' => [
'custom' => [
'tpay' => [
'code' => 'tpay',
'name' => 'Tpay',
'gatewayFactory' => 'tpay',
'gatewayName' => 'tpay',
'gatewayConfig' => $tpayConfig + ['type' => PaymentType::REDIRECT],
'code' => 'tpay_redirect',
'name' => 'Tpay (Redirect)',
'gatewayFactory' => 'tpay_redirect',
'gatewayName' => 'tpay_redirect',
'gatewayConfig' => $tpayConfig,
'channels' => [
'FASHION_WEB',
],
Expand Down
3 changes: 0 additions & 3 deletions config/services/api/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
;

$services->set('commerce_weavers_sylius_tpay.api.factory.next_command.pay_by_redirect', PayByRedirectFactory::class)
->args([
service('payum.dynamic_gateways.cypher'),
])
->tag('commerce_weavers_sylius_tpay.api.factory.next_command')
;

Expand Down
16 changes: 7 additions & 9 deletions config/services/payum/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
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\RedirectPayment\Payum\Factory\GatewayFactory as RedirectGatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateApplePayTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateGooglePayTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateRedirectBasedTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateVisaMobileTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\InitializeApplePayPaymentAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\NotifyAction;
Expand All @@ -33,6 +33,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' => RedirectGatewayFactory::NAME, 'alias' => 'cw.tpay_redirect.capture'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.capture'])
;

Expand All @@ -52,14 +53,6 @@
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_google_pay_transaction'])
;

$services->set(CreateRedirectBasedTransactionAction::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.factory.create_redirect_based_payment_payload'),
service('commerce_weavers_sylius_tpay.payum.factory.token.notify'),
])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.create_redirect_based_transaction'])
;

$services->set(CreateVisaMobileTransactionAction::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.factory.create_visa_mobile_payment_payload'),
Expand All @@ -77,27 +70,31 @@
->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' => RedirectGatewayFactory::NAME, 'alias' => 'cw.tpay_redirect.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' => RedirectGatewayFactory::NAME, 'alias' => 'cw.tpay_redirect.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' => RedirectGatewayFactory::NAME, 'alias' => 'cw.tpay_redirect.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' => RedirectGatewayFactory::NAME, 'alias' => 'cw.tpay_redirect.refund'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.refund'])
;

Expand All @@ -112,6 +109,7 @@
->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' => RedirectGatewayFactory::NAME, 'alias' => 'cw.tpay_redirect.resolve_next_route'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.resolve_next_route'])
;
};
9 changes: 9 additions & 0 deletions config/services/redirect_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 function(ContainerConfigurator $container): void {
$container->import('form/**/*.php');
};
19 changes: 19 additions & 0 deletions config/services/redirect_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\RedirectPayment\Form\Type\GatewayConfigurationType;
use CommerceWeavers\SyliusTpayPlugin\RedirectPayment\Payum\Factory\GatewayFactory;

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

$services->set('commerce_weavers_sylius_tpay.redirect_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_redirect', 'type' => GatewayFactory::NAME])
->tag('form.type')
;
};
9 changes: 9 additions & 0 deletions config/services/redirect_payment/payum.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 function(ContainerConfigurator $container): void {
$container->import('payum/**/*.php');
};
24 changes: 24 additions & 0 deletions config/services/redirect_payment/payum/action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\RedirectPayment\Payum\Action\CreateRedirectBasedTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\RedirectPayment\Payum\Factory\GatewayFactory;

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

$services->defaults()
->public()
;

$services->set('commerce_weavers_sylius_tpay.redirect_payment.payum.action.create_redirect_based_transaction', CreateRedirectBasedTransactionAction::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.factory.create_redirect_based_payment_payload'),
service('commerce_weavers_sylius_tpay.payum.factory.token.notify'),
])
->tag('payum.action', ['factory' => GatewayFactory::NAME, 'alias' => 'cw.tpay_redirect.create_redirect_based_transaction'])
;
};
19 changes: 19 additions & 0 deletions config/services/redirect_payment/payum/factory.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\RedirectPayment\Payum\Factory\GatewayFactory;
use Payum\Core\Bridge\Symfony\Builder\GatewayFactoryBuilder;

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

$services->set('commerce_weavers_sylius_tpay.redirect_payment.payum.factory.gateway', GatewayFactoryBuilder::class)
->args([
GatewayFactory::class,
])
->tag('payum.gateway_factory_builder', ['factory' => GatewayFactory::NAME])
;
};
29 changes: 3 additions & 26 deletions src/Api/Factory/NextCommand/PayByRedirectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@
use CommerceWeavers\SyliusTpayPlugin\Api\Command\PayByRedirect;
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\Exception\UnsupportedNextCommandFactory;
use CommerceWeavers\SyliusTpayPlugin\Api\Factory\NextCommandFactoryInterface;
use Payum\Core\Model\GatewayConfigInterface;
use Payum\Core\Security\CryptedInterface;
use Payum\Core\Security\CypherInterface;
use CommerceWeavers\SyliusTpayPlugin\RedirectPayment\Payum\Factory\GatewayFactory;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;

final class PayByRedirectFactory implements NextCommandFactoryInterface
{
public function __construct(
private readonly CypherInterface $cypher,
) {
}

public function create(Pay $command, PaymentInterface $payment): PayByRedirect
{
if (!$this->supports($command, $payment)) {
Expand All @@ -39,26 +32,10 @@ public function supports(Pay $command, PaymentInterface $payment): bool
return false;
}

$gatewayConfig = $this->getGatewayConfig($payment);

if (null === $gatewayConfig) {
return false;
}

if ($gatewayConfig instanceof CryptedInterface) {
$gatewayConfig->decrypt($this->cypher);
}

$config = $gatewayConfig->getConfig();

return isset($config['type']) && $config['type'] === 'redirect';
}

private function getGatewayConfig(PaymentInterface $payment): ?GatewayConfigInterface
{
/** @var PaymentMethodInterface|null $paymentMethod */
$paymentMethod = $payment->getMethod();
$gatewayName = $paymentMethod?->getGatewayConfig()?->getGatewayName();

return $paymentMethod?->getGatewayConfig();
return $gatewayName === GatewayFactory::NAME;
}
}
11 changes: 11 additions & 0 deletions src/RedirectPayment/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\RedirectPayment\Form\Type;

use CommerceWeavers\SyliusTpayPlugin\Form\Type\AbstractTpayGatewayConfigurationType;

final class GatewayConfigurationType extends AbstractTpayGatewayConfigurationType
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api;
namespace CommerceWeavers\SyliusTpayPlugin\RedirectPayment\Payum\Action;

use CommerceWeavers\SyliusTpayPlugin\Model\PaymentDetails;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\BasePaymentAwareAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\Token\NotifyTokenFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction;
use CommerceWeavers\SyliusTpayPlugin\RedirectPayment\Payum\Factory\GatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateRedirectBasedPaymentPayloadFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use Payum\Core\Reply\HttpRedirect;
use Payum\Core\Request\Generic;
use Payum\Core\Security\GenericTokenFactoryAwareTrait;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;

final class CreateRedirectBasedTransactionAction extends BasePaymentAwareAction
{
Expand Down Expand Up @@ -61,8 +63,10 @@ public function supports($request): bool
return false;
}

$paymentDetails = PaymentDetails::fromArray($model->getDetails());
/** @var PaymentMethodInterface|null $paymentMethod */
$paymentMethod = $model->getMethod();
$gatewayName = $paymentMethod?->getGatewayConfig()?->getGatewayName();

return $paymentDetails->getType() === PaymentType::REDIRECT;
return $gatewayName === GatewayFactory::NAME;
}
}
17 changes: 17 additions & 0 deletions src/RedirectPayment/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\RedirectPayment\Payum\Factory;

use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory;

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

public function getName(): string
{
return self::NAME;
}
}
5 changes: 2 additions & 3 deletions tests/Api/DataFixtures/shop/blik_payment_method.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ Sylius\Component\Core\Model\PaymentMethod:

Sylius\Bundle\PayumBundle\Model\GatewayConfig:
gateway_tpay:
gatewayName: 'tpay'
factoryName: 'tpay'
gatewayName: 'tpay_redirect'
factoryName: 'tpay_redirect'
config:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
type: 'redirect'
production_mode: false
encrypted: true
gateway_tpay_card:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ Sylius\Component\Core\Model\PaymentMethod:

Sylius\Bundle\PayumBundle\Model\GatewayConfig:
gateway_tpay:
gatewayName: 'tpay'
factoryName: 'tpay'
gatewayName: 'tpay_redirect'
factoryName: 'tpay_redirect'
config:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
type: 'redirect'
production_mode: false
encrypted: true
gateway_tpay_card:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ Sylius\Component\Core\Model\PaymentMethod:

Sylius\Bundle\PayumBundle\Model\GatewayConfig:
gateway_tpay:
gatewayName: 'tpay'
factoryName: 'tpay'
gatewayName: 'tpay_redirect'
factoryName: 'tpay_redirect'
config:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
type: 'redirect'
production_mode: false
encrypted: true
gateway_tpay_card:
Expand Down
5 changes: 2 additions & 3 deletions tests/E2E/Resources/fixtures/common/payment_method.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ Sylius\Component\Core\Model\PaymentMethod:

Sylius\Bundle\PayumBundle\Model\GatewayConfig:
gateway_tpay:
gatewayName: 'tpay'
factoryName: 'tpay'
gatewayName: 'tpay_redirect'
factoryName: 'tpay_redirect'
config:
client_id: 'encrypted_<getenv("TPAY_CLIENT_ID")>'
client_secret: 'encrypted_<getenv("TPAY_CLIENT_SECRET")>'
type: 'redirect'
production_mode: false
encrypted: true
gateway_tpay_card:
Expand Down
Loading

0 comments on commit 0796792

Please sign in to comment.