Skip to content

Commit

Permalink
Extract Tpay Single Channel Pay-by-link as a separate payment method (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lchrusciel authored Jan 26, 2025
2 parents ee7c7d1 + f857f97 commit 6791b36
Show file tree
Hide file tree
Showing 31 changed files with 682 additions and 55 deletions.
7 changes: 0 additions & 7 deletions assets/admin/js/test_payment_method_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,6 @@ function convertTpayChannelIdInputIntoSelect(channels) {

tpayChannelIdFormType.innerHTML = '';

const displayAllOption = document.createElement('option');
displayAllOption.value = '';
console.log(tpayChannelIdFormType.dataset);
displayAllOption.text = tpayChannelIdFormType.dataset.displayAllLabel;

tpayChannelIdFormType.appendChild(displayAllOption);

for (const [id, name] of Object.entries(channels)) {
const option = document.createElement('option');
option.value = id;
Expand Down
6 changes: 3 additions & 3 deletions config/config/sylius_fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@
],
'pbl_channel' => [
'code' => 'tpay_pbl_channel',
'name' => 'Pay-by-link one channel (Tpay)',
'gatewayFactory' => 'tpay_pbl',
'gatewayName' => 'tpay_pbl',
'name' => 'Pay-by-link single channel (Tpay)',
'gatewayFactory' => 'tpay_pbl_channel',
'gatewayName' => 'tpay_pbl_channel',
'gatewayConfig' => $tpayConfig + [
'tpay_channel_id' => '%env(string:TPAY_PBL_CHANNEL_ID)%',
],
Expand Down
8 changes: 8 additions & 0 deletions config/config/sylius_template_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
'template' => '@CommerceWeaversSyliusTpayPlugin/shop/order/pay/_payByLink.html.twig',
'priority' => 10,
],
'pay_by_link_channel' => [
'template' => '@CommerceWeaversSyliusTpayPlugin/shop/order/pay/_payByLinkChannel.html.twig',
'priority' => 10,
],
'google_pay' => [
'template' => '@CommerceWeaversSyliusTpayPlugin/shop/order/pay/_google_pay_regulations.html.twig',
'priority' => 5,
Expand Down Expand Up @@ -69,6 +73,10 @@
'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_payByLink.html.twig',
'priority' => 5,
],
'pay_by_link_channel' => [
'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_payByLinkChannel.html.twig',
'priority' => 5,
],
'visa_mobile' => [
'template' => '@CommerceWeaversSyliusTpayPlugin/shop/cart/complete/_visaMobile.html.twig',
'priority' => 5,
Expand Down
9 changes: 9 additions & 0 deletions config/services/pay_by_link_channel_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');
};
22 changes: 22 additions & 0 deletions config/services/pay_by_link_channel_payment/form/type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;


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

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

$services->set('commerce_weavers_sylius_tpay.pay_by_link_channel_payment.form.type.gateway_configuration', GatewayConfigurationType::class)
->args([
service('translator'),
])
->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_channel_pbl', 'type' => GatewayFactory::NAME])
->tag('form.type')
;
};
9 changes: 9 additions & 0 deletions config/services/pay_by_link_channel_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 static function(ContainerConfigurator $container): void {
$container->import('payum/**/*.php');
};
24 changes: 24 additions & 0 deletions config/services/pay_by_link_channel_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\PayByLinkChannelPayment\Payum\Action\CreatePayByLinkTransactionAction;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkChannelPayment\Payum\Factory\GatewayFactory;

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

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

$services->set(CreatePayByLinkTransactionAction::class)
->args([
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' => GatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.create_pay_by_link_channel_transaction'])
;
};
19 changes: 19 additions & 0 deletions config/services/pay_by_link_channel_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\PayByLinkChannelPayment\Payum\Factory\GatewayFactory;
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_channel_payment.payum.factory.gateway', GatewayFactoryBuilder::class)
->args([
GatewayFactory::class,
])
->tag('payum.gateway_factory_builder', ['factory' => GatewayFactory::NAME])
;
};
3 changes: 0 additions & 3 deletions config/services/pay_by_link_payment/form/type.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
$services = $container->services();

$services->set('commerce_weavers_sylius_tpay.pay_by_link_payment.form.type.gateway_configuration', GatewayConfigurationType::class)
->args([
service('translator'),
])
->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')
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 @@ -9,6 +9,7 @@
use CommerceWeavers\SyliusTpayPlugin\CardPayment\Payum\Factory\GatewayFactory as CardGatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\GooglePayPayment\Payum\Factory\GatewayFactory as GooglePayGatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GatewayFactory as PayByLinkGatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkChannelPayment\Payum\Factory\GatewayFactory as PayByLinkChannelGatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\NotifyAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\CaptureAction;
use CommerceWeavers\SyliusTpayPlugin\Payum\Action\GetStatusAction;
Expand All @@ -35,6 +36,7 @@
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.capture'])
->tag('payum.action', ['factory' => GooglePayGatewayFactory::NAME, 'alias' => 'cw.tpay_google_pay.capture'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.capture'])
->tag('payum.action', ['factory' => PayByLinkChannelGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl_channel.capture'])
->tag('payum.action', ['factory' => RedirectGatewayFactory::NAME, 'alias' => 'cw.tpay_redirect.capture'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.capture'])
;
Expand All @@ -50,6 +52,7 @@
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.notify'])
->tag('payum.action', ['factory' => GooglePayGatewayFactory::NAME, 'alias' => 'cw.tpay_google_pay.notify'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.notify'])
->tag('payum.action', ['factory' => PayByLinkChannelGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl_channel.notify'])
->tag('payum.action', ['factory' => RedirectGatewayFactory::NAME, 'alias' => 'cw.tpay_redirect.notify'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.notify'])
;
Expand All @@ -60,6 +63,7 @@
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.get_status'])
->tag('payum.action', ['factory' => GooglePayGatewayFactory::NAME, 'alias' => 'cw.tpay_google_pay.get_status'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.get_status'])
->tag('payum.action', ['factory' => PayByLinkChannelGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl_channel.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'])
;
Expand All @@ -70,6 +74,7 @@
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.partial_refund'])
->tag('payum.action', ['factory' => GooglePayGatewayFactory::NAME, 'alias' => 'cw.tpay_google_pay.partial_refund'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.partial_refund'])
->tag('payum.action', ['factory' => PayByLinkChannelGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl_channel.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'])
;
Expand All @@ -80,6 +85,7 @@
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.refund'])
->tag('payum.action', ['factory' => GooglePayGatewayFactory::NAME, 'alias' => 'cw.tpay_google_pay.refund'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.refund'])
->tag('payum.action', ['factory' => PayByLinkChannelGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl_channel.refund'])
->tag('payum.action', ['factory' => RedirectGatewayFactory::NAME, 'alias' => 'cw.tpay_redirect.refund'])
->tag('payum.action', ['factory' => TpayGatewayFactory::NAME, 'alias' => 'cw.tpay.refund'])
;
Expand All @@ -90,6 +96,7 @@
->tag('payum.action', ['factory' => CardGatewayFactory::NAME, 'alias' => 'cw.tpay_card.resolve_next_route'])
->tag('payum.action', ['factory' => GooglePayGatewayFactory::NAME, 'alias' => 'cw.tpay_google_pay.resolve_next_route'])
->tag('payum.action', ['factory' => PayByLinkGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl.resolve_next_route'])
->tag('payum.action', ['factory' => PayByLinkChannelGatewayFactory::NAME, 'alias' => 'cw.tpay_pbl_channel.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'])
;
Expand Down
43 changes: 43 additions & 0 deletions migrations/Version20250120115438.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace CommerceWeaversSyliusTpayMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20250120115438 extends AbstractMigration
{
public function getDescription(): string
{
return 'Migrate channel based tpay_pbl gateway to tpay_pbl_channel';
}

public function up(Schema $schema): void
{
$this->addSql("
UPDATE sylius_gateway_config
SET
gateway_name = 'tpay_pbl_channel',
factory_name = 'tpay_pbl_channel'
WHERE gateway_name = 'tpay_pbl'
AND JSON_EXTRACT(config, '$.tpay_channel_id') IS NOT NULL
");
}

public function down(Schema $schema): void
{
$this->addSql("
UPDATE sylius_gateway_config
SET
gateway_name = 'tpay_pbl',
factory_name = 'tpay_pbl'
WHERE gateway_name = 'tpay_pbl_channel'
AND JSON_EXTRACT(config, '$.tpay_channel_id') IS NOT NULL
");
}
}
44 changes: 44 additions & 0 deletions src/PayByLinkChannelPayment/Form/Type/GatewayConfigurationType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\PayByLinkChannelPayment\Form\Type;

use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\DecryptGatewayConfigListenerInterface;
use CommerceWeavers\SyliusTpayPlugin\Form\EventListener\EncryptGatewayConfigListenerInterface;
use CommerceWeavers\SyliusTpayPlugin\Form\Type\AbstractTpayGatewayConfigurationType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

final class GatewayConfigurationType extends AbstractTpayGatewayConfigurationType
{
public function __construct(
DecryptGatewayConfigListenerInterface $decryptGatewayConfigListener,
EncryptGatewayConfigListenerInterface $encryptGatewayConfigListener,
) {
parent::__construct($decryptGatewayConfigListener, $encryptGatewayConfigListener);
}

public function buildForm(FormBuilderInterface $builder, array $options): void
{
parent::buildForm($builder, $options);

$builder
->add(
'tpay_channel_id',
TextType::class,
[
'empty_data' => '',
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.tpay_channel_id',
'validation_groups' => ['sylius'],
'constraints' => [
new NotBlank(allowNull: false, groups: ['sylius']),
],
'help' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.tpay_channel_id_help',
'required' => false,
],
)
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\PayByLinkChannelPayment\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\Tpay\Factory\CreatePayByLinkPayloadFactoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Reply\HttpRedirect;
use Payum\Core\Request\Generic;
use Sylius\Component\Core\Model\PaymentInterface;

final class CreatePayByLinkTransactionAction extends BasePaymentAwareAction implements GatewayAwareInterface
{
use GatewayAwareTrait;

public function __construct(
private readonly CreatePayByLinkPayloadFactoryInterface $createPayByLinkPayloadFactory,
private readonly NotifyTokenFactoryInterface $notifyTokenFactory,
) {
parent::__construct();
}

protected function doExecute(Generic $request, PaymentInterface $model, PaymentDetails $paymentDetails, string $gatewayName, string $localeCode): void
{
$notifyToken = $this->notifyTokenFactory->create($model, $gatewayName, $localeCode);

$this->do(
fn () => $this->api->transactions()->createTransaction(
$this->createPayByLinkPayloadFactory->createFrom($model, $notifyToken->getTargetUrl(), $localeCode),
),
onSuccess: function (array $response) use ($paymentDetails) {
$paymentDetails->setTransactionId($response['transactionId']);
$paymentDetails->setStatus($response['status']);
$paymentDetails->setPaymentUrl($response['transactionPaymentUrl']);
},
onFailure: fn () => $paymentDetails->setStatus(PaymentInterface::STATE_FAILED),
);
}

protected function postExecute(PaymentInterface $model, PaymentDetails $paymentDetails, string $gatewayName, string $localeCode): void
{
if ($paymentDetails->getPaymentUrl() !== null) {
throw new HttpRedirect($paymentDetails->getPaymentUrl());
}
}

public function supports($request): bool
{
if (!$request instanceof CreateTransaction) {
return false;
}

$model = $request->getModel();

if (!$model instanceof PaymentInterface) {
return false;
}

$paymentDetails = PaymentDetails::fromArray($model->getDetails());

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

use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\TpayGatewayFactory;

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

public function getName(): string
{
return self::NAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function isSupportedForOrder(PaymentMethodInterface $paymentMethod, Order
/** @var GatewayConfigInterface|null $gatewayConfig */
$gatewayConfig = $paymentMethod->getGatewayConfig();

if (null === $gatewayConfig || GatewayName::PAY_BY_LINK !== $gatewayConfig->getFactoryName()) {
if (null === $gatewayConfig || (GatewayName::PAY_BY_LINK !== $gatewayConfig->getFactoryName() && GatewayName::PAY_BY_LINK_CHANNEL !== $gatewayConfig->getFactoryName())) {
return true;
}

Expand Down
Loading

0 comments on commit 6791b36

Please sign in to comment.