From 07967926e33a7a3e151547b451cf3a4b1d5637a0 Mon Sep 17 00:00:00 2001 From: Jacob Tobiasz Date: Mon, 25 Nov 2024 11:11:43 +0100 Subject: [PATCH] Extract Tpay Redirect as a separate payment method --- config/config/sylius_fixtures.php | 10 +++--- config/services/api/factory.php | 3 -- config/services/payum/action.php | 16 ++++----- config/services/redirect_payment/form.php | 9 +++++ .../services/redirect_payment/form/type.php | 19 +++++++++++ config/services/redirect_payment/payum.php | 9 +++++ .../redirect_payment/payum/action.php | 24 +++++++++++++ .../redirect_payment/payum/factory.php | 19 +++++++++++ .../NextCommand/PayByRedirectFactory.php | 29 ++-------------- .../Form/Type/GatewayConfigurationType.php | 11 ++++++ .../CreateRedirectBasedTransactionAction.php | 12 ++++--- .../Payum/Factory/GatewayFactory.php | 17 ++++++++++ .../DataFixtures/shop/blik_payment_method.yml | 5 ++- .../cancelling_payment/payment_method.yml | 5 ++- .../payment_method.yml | 5 ++- .../fixtures/common/payment_method.yaml | 5 ++- ...eateRedirectBasedTransactionActionTest.php | 34 +++++++++---------- translations/messages.en.yaml | 1 + translations/messages.pl.yaml | 1 + 19 files changed, 158 insertions(+), 76 deletions(-) create mode 100644 config/services/redirect_payment/form.php create mode 100644 config/services/redirect_payment/form/type.php create mode 100644 config/services/redirect_payment/payum.php create mode 100644 config/services/redirect_payment/payum/action.php create mode 100644 config/services/redirect_payment/payum/factory.php create mode 100644 src/RedirectPayment/Form/Type/GatewayConfigurationType.php rename src/{Payum/Action/Api => RedirectPayment/Payum/Action}/CreateRedirectBasedTransactionAction.php (81%) create mode 100644 src/RedirectPayment/Payum/Factory/GatewayFactory.php rename tests/Unit/{Payum/Action/Api => RedirectPayment/Payum/Action}/CreateRedirectBasedTransactionActionTest.php (91%) diff --git a/config/config/sylius_fixtures.php b/config/config/sylius_fixtures.php index 636296a4..528377a9 100644 --- a/config/config/sylius_fixtures.php +++ b/config/config/sylius_fixtures.php @@ -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', ], diff --git a/config/services/api/factory.php b/config/services/api/factory.php index 209a7df2..57844ddf 100644 --- a/config/services/api/factory.php +++ b/config/services/api/factory.php @@ -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') ; diff --git a/config/services/payum/action.php b/config/services/payum/action.php index 19dc8e99..f9da2129 100644 --- a/config/services/payum/action.php +++ b/config/services/payum/action.php @@ -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; @@ -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']) ; @@ -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'), @@ -77,6 +70,7 @@ ->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']) ; @@ -84,6 +78,7 @@ ->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']) ; @@ -91,6 +86,7 @@ ->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']) ; @@ -98,6 +94,7 @@ ->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']) ; @@ -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']) ; }; diff --git a/config/services/redirect_payment/form.php b/config/services/redirect_payment/form.php new file mode 100644 index 00000000..0c78f855 --- /dev/null +++ b/config/services/redirect_payment/form.php @@ -0,0 +1,9 @@ +import('form/**/*.php'); +}; diff --git a/config/services/redirect_payment/form/type.php b/config/services/redirect_payment/form/type.php new file mode 100644 index 00000000..6f4a317b --- /dev/null +++ b/config/services/redirect_payment/form/type.php @@ -0,0 +1,19 @@ +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') + ; +}; diff --git a/config/services/redirect_payment/payum.php b/config/services/redirect_payment/payum.php new file mode 100644 index 00000000..32161d65 --- /dev/null +++ b/config/services/redirect_payment/payum.php @@ -0,0 +1,9 @@ +import('payum/**/*.php'); +}; diff --git a/config/services/redirect_payment/payum/action.php b/config/services/redirect_payment/payum/action.php new file mode 100644 index 00000000..75d9738c --- /dev/null +++ b/config/services/redirect_payment/payum/action.php @@ -0,0 +1,24 @@ +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']) + ; +}; diff --git a/config/services/redirect_payment/payum/factory.php b/config/services/redirect_payment/payum/factory.php new file mode 100644 index 00000000..d900fb4a --- /dev/null +++ b/config/services/redirect_payment/payum/factory.php @@ -0,0 +1,19 @@ +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]) + ; +}; diff --git a/src/Api/Factory/NextCommand/PayByRedirectFactory.php b/src/Api/Factory/NextCommand/PayByRedirectFactory.php index 6cc9f625..7a00f179 100644 --- a/src/Api/Factory/NextCommand/PayByRedirectFactory.php +++ b/src/Api/Factory/NextCommand/PayByRedirectFactory.php @@ -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)) { @@ -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; } } diff --git a/src/RedirectPayment/Form/Type/GatewayConfigurationType.php b/src/RedirectPayment/Form/Type/GatewayConfigurationType.php new file mode 100644 index 00000000..c1f3b85e --- /dev/null +++ b/src/RedirectPayment/Form/Type/GatewayConfigurationType.php @@ -0,0 +1,11 @@ +getDetails()); + /** @var PaymentMethodInterface|null $paymentMethod */ + $paymentMethod = $model->getMethod(); + $gatewayName = $paymentMethod?->getGatewayConfig()?->getGatewayName(); - return $paymentDetails->getType() === PaymentType::REDIRECT; + return $gatewayName === GatewayFactory::NAME; } } diff --git a/src/RedirectPayment/Payum/Factory/GatewayFactory.php b/src/RedirectPayment/Payum/Factory/GatewayFactory.php new file mode 100644 index 00000000..06b56804 --- /dev/null +++ b/src/RedirectPayment/Payum/Factory/GatewayFactory.php @@ -0,0 +1,17 @@ +' client_secret: 'encrypted_' - type: 'redirect' production_mode: false encrypted: true gateway_tpay_card: diff --git a/tests/Api/DataFixtures/shop/cancelling_payment/payment_method.yml b/tests/Api/DataFixtures/shop/cancelling_payment/payment_method.yml index 546e3f65..f52f8923 100644 --- a/tests/Api/DataFixtures/shop/cancelling_payment/payment_method.yml +++ b/tests/Api/DataFixtures/shop/cancelling_payment/payment_method.yml @@ -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_' client_secret: 'encrypted_' - type: 'redirect' production_mode: false encrypted: true gateway_tpay_card: diff --git a/tests/Api/DataFixtures/shop/paying_for_orders_by_card/payment_method.yml b/tests/Api/DataFixtures/shop/paying_for_orders_by_card/payment_method.yml index 39c156ce..c33d1bbe 100644 --- a/tests/Api/DataFixtures/shop/paying_for_orders_by_card/payment_method.yml +++ b/tests/Api/DataFixtures/shop/paying_for_orders_by_card/payment_method.yml @@ -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_' client_secret: 'encrypted_' - type: 'redirect' production_mode: false encrypted: true gateway_tpay_card: diff --git a/tests/E2E/Resources/fixtures/common/payment_method.yaml b/tests/E2E/Resources/fixtures/common/payment_method.yaml index 2e6541db..1db6f663 100644 --- a/tests/E2E/Resources/fixtures/common/payment_method.yaml +++ b/tests/E2E/Resources/fixtures/common/payment_method.yaml @@ -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_' client_secret: 'encrypted_' - type: 'redirect' production_mode: false encrypted: true gateway_tpay_card: diff --git a/tests/Unit/Payum/Action/Api/CreateRedirectBasedTransactionActionTest.php b/tests/Unit/RedirectPayment/Payum/Action/CreateRedirectBasedTransactionActionTest.php similarity index 91% rename from tests/Unit/Payum/Action/Api/CreateRedirectBasedTransactionActionTest.php rename to tests/Unit/RedirectPayment/Payum/Action/CreateRedirectBasedTransactionActionTest.php index 8b87fa32..6f48d4c1 100644 --- a/tests/Unit/Payum/Action/Api/CreateRedirectBasedTransactionActionTest.php +++ b/tests/Unit/RedirectPayment/Payum/Action/CreateRedirectBasedTransactionActionTest.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\Payum\Action\Api; +namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\RedirectPayment\Payum\Action; -use CommerceWeavers\SyliusTpayPlugin\Payum\Action\Api\CreateRedirectBasedTransactionAction; use CommerceWeavers\SyliusTpayPlugin\Payum\Factory\Token\NotifyTokenFactoryInterface; use CommerceWeavers\SyliusTpayPlugin\Payum\Request\Api\CreateTransaction; +use CommerceWeavers\SyliusTpayPlugin\RedirectPayment\Payum\Action\CreateRedirectBasedTransactionAction; use CommerceWeavers\SyliusTpayPlugin\Tpay\Factory\CreateRedirectBasedPaymentPayloadFactoryInterface; use CommerceWeavers\SyliusTpayPlugin\Tpay\TpayApi; use Payum\Core\Model\GatewayConfigInterface; @@ -48,8 +48,14 @@ protected function setUp(): void public function test_it_supports_create_transaction_requests_with_a_valid_payment_model(): void { + $gatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $gatewayConfig->getGatewayName()->willReturn('tpay_redirect'); + + $paymentMethod = $this->prophesize(PaymentMethodInterface::class); + $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); + $payment = $this->prophesize(PaymentInterface::class); - $payment->getDetails()->willReturn([]); + $payment->getMethod()->willReturn($paymentMethod); $request = $this->prophesize(CreateTransaction::class); $request->getModel()->willReturn($payment); @@ -84,11 +90,16 @@ public function test_it_does_not_support_requests_with_non_payment_model(): void $this->assertFalse($isSupported); } - /** @dataProvider notSupportedPaymentDetailsDataProvider */ - public function test_it_does_not_support_requests_with_not_eligible_payment_model(array $paymentDetails): void + public function test_it_does_not_support_requests_with_not_eligible_payment_model(): void { + $gatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $gatewayConfig->getGatewayName()->willReturn('tpay_card'); + + $paymentMethod = $this->prophesize(PaymentMethodInterface::class); + $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); + $payment = $this->prophesize(PaymentInterface::class); - $payment->getDetails()->willReturn($paymentDetails); + $payment->getMethod()->willReturn($paymentMethod); $request = $this->prophesize(CreateTransaction::class); $request->getModel()->willReturn($payment); @@ -262,17 +273,6 @@ public function test_it_throws_an_exception_if_a_gateway_name_cannot_be_determin $this->createTestSubject()->execute($request->reveal()); } - private function notSupportedPaymentDetailsDataProvider(): array - { - return [ - [['tpay' => ['card' => 'some_value']]], - [['tpay' => ['blik_token' => 'some_value']]], - [['tpay' => ['tpay_channel_id' => 'some_value']]], - [['tpay' => ['google_pay_token' => 'some_value']]], - [['tpay' => ['apple_pay_token' => 'some_value']]], - ]; - } - private function createTestSubject(): CreateRedirectBasedTransactionAction { $action = new CreateRedirectBasedTransactionAction( diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml index d0682a09..718e1454 100644 --- a/translations/messages.en.yaml +++ b/translations/messages.en.yaml @@ -5,6 +5,7 @@ commerce_weavers_sylius_tpay: tpay_blik: 'BLIK (Tpay)' tpay_card: 'Card (Tpay)' tpay_pbl: 'Pay-by-Link (Tpay)' + tpay_redirect: 'Redirect to Tpay' gateway_configuration: cards_api: 'RSA key' client_id: 'Client ID' diff --git a/translations/messages.pl.yaml b/translations/messages.pl.yaml index 12c82c0c..8a580e10 100644 --- a/translations/messages.pl.yaml +++ b/translations/messages.pl.yaml @@ -5,6 +5,7 @@ commerce_weavers_sylius_tpay: tpay_blik: 'BLIK (Tpay)' tpay_card: 'Karta (Tpay)' tpay_pbl: 'Pay-by-Link (Tpay)' + tpay_redirect: 'Przekierowanie do Tpay' gateway_configuration: cards_api: 'Klucz RSA' client_id: 'Client ID'