diff --git a/config/services/context_provider.php b/config/services/context_provider.php index 1acb4a01..70a4775e 100644 --- a/config/services/context_provider.php +++ b/config/services/context_provider.php @@ -5,18 +5,10 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; use CommerceWeavers\SyliusTpayPlugin\ContextProvider\RegulationsUrlContextProvider; -use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\ContextProvider\BankListContextProvider; return static function(ContainerConfigurator $container): void { $services = $container->services(); - $services->set(BankListContextProvider::class) - ->args([ - service('commerce_weavers_sylius_tpay.tpay.provider.validated_tpay_api_bank_list'), - ]) - ->tag('sylius.ui.template_event.context_provider') - ; - $services->set(RegulationsUrlContextProvider::class) ->args([ service('sylius.context.locale'), diff --git a/config/services/pay_by_link_payment/context_provider.php b/config/services/pay_by_link_payment/context_provider.php index 9e511ce3..a57a6fa7 100644 --- a/config/services/pay_by_link_payment/context_provider.php +++ b/config/services/pay_by_link_payment/context_provider.php @@ -12,6 +12,7 @@ $services->set('commerce_weavers_sylius_tpay.pay_by_link_payment.context_provider.bank_list', BankListContextProvider::class) ->args([ service('commerce_weavers_sylius_tpay.tpay.provider.validated_tpay_api_bank_list'), + service('payum.dynamic_gateways.cypher'), ]) ->tag('sylius.ui.template_event.context_provider') ; diff --git a/src/PayByLinkPayment/ContextProvider/BankListContextProvider.php b/src/PayByLinkPayment/ContextProvider/BankListContextProvider.php index c7d32c1b..d629b09b 100644 --- a/src/PayByLinkPayment/ContextProvider/BankListContextProvider.php +++ b/src/PayByLinkPayment/ContextProvider/BankListContextProvider.php @@ -4,7 +4,11 @@ namespace CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\ContextProvider; +use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GatewayFactory as PayByLinkGatewayFactory; use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\ValidTpayChannelListProviderInterface; +use Payum\Core\Model\GatewayConfigInterface; +use Payum\Core\Security\CryptedInterface; +use Payum\Core\Security\CypherInterface; use Sylius\Bundle\UiBundle\ContextProvider\ContextProviderInterface; use Sylius\Bundle\UiBundle\Registry\TemplateBlock; use Sylius\Component\Core\Model\OrderInterface; @@ -19,6 +23,7 @@ final class BankListContextProvider implements ContextProviderInterface public function __construct( private readonly ValidTpayChannelListProviderInterface $validTpayChannelListProvider, + private readonly CypherInterface $cypher, ) { } @@ -28,21 +33,24 @@ public function provide(array $templateContext, TemplateBlock $templateBlock): a $templateContext['banks'] = []; $paymentMethod = $this->resolvePaymentMethod($templateContext); - $gatewayConfig = $paymentMethod?->getGatewayConfig()?->getConfig() ?? []; + $gatewayConfig = $paymentMethod?->getGatewayConfig(); if ( null === $paymentMethod || - 'pay_by_link' !== ($gatewayConfig['type'] ?? null) + null === $gatewayConfig || + PayByLinkGatewayFactory::NAME !== $gatewayConfig->getFactoryName() ) { return $templateContext; } + $decryptedGatewayConfig = $this->getEncryptedGatewayConfig($gatewayConfig); + /** * @phpstan-ignore-next-line * * @var string|null $tpayChannelId */ - $tpayChannelId = $gatewayConfig['tpay_channel_id'] ?? null; + $tpayChannelId = $decryptedGatewayConfig['tpay_channel_id'] ?? null; $templateContext['defaultTpayChannelId'] = $tpayChannelId; $templateContext['banks'] = null === $tpayChannelId ? $this->validTpayChannelListProvider->provide() : []; @@ -82,4 +90,13 @@ private function resolvePaymentMethod(array $templateContext): ?PaymentMethodInt return $paymentMethod; } + + private function getEncryptedGatewayConfig(GatewayConfigInterface $gatewayConfig): array + { + if ($gatewayConfig instanceof CryptedInterface) { + $gatewayConfig->decrypt($this->cypher); + } + + return $gatewayConfig->getConfig(); + } } diff --git a/src/PayByLinkPayment/Form/Type/GatewayConfigurationType.php b/src/PayByLinkPayment/Form/Type/GatewayConfigurationType.php index 92134556..07c99c63 100644 --- a/src/PayByLinkPayment/Form/Type/GatewayConfigurationType.php +++ b/src/PayByLinkPayment/Form/Type/GatewayConfigurationType.php @@ -5,7 +5,26 @@ namespace CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Form\Type; use CommerceWeavers\SyliusTpayPlugin\Form\Type\AbstractTpayGatewayConfigurationType; +use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\FormBuilderInterface; final class GatewayConfigurationType extends AbstractTpayGatewayConfigurationType { + 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', + 'help' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.tpay_channel_id_help', + 'required' => false, + ], + ) + ; + } } diff --git a/templates/admin/payment_method/test_connection.html.twig b/templates/admin/payment_method/test_connection.html.twig index e989d3e6..938ac992 100644 --- a/templates/admin/payment_method/test_connection.html.twig +++ b/templates/admin/payment_method/test_connection.html.twig @@ -1,4 +1,4 @@ -{% if resource.gatewayConfig.factoryName == 'tpay' %} +{% if resource.gatewayConfig.factoryName == 'tpay_pbl' %}
{{ 'commerce_weavers_sylius_tpay.admin.gateway_configuration.test_connection'|trans }}
diff --git a/tests/Unit/PayByLinkPayment/ContextProvider/BankListContextProviderTest.php b/tests/Unit/PayByLinkPayment/ContextProvider/BankListContextProviderTest.php index 7f79ce5b..156bd32f 100644 --- a/tests/Unit/PayByLinkPayment/ContextProvider/BankListContextProviderTest.php +++ b/tests/Unit/PayByLinkPayment/ContextProvider/BankListContextProviderTest.php @@ -5,11 +5,13 @@ namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\PayByLinkPayment\ContextProvider; use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\ContextProvider\BankListContextProvider; +use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GatewayFactory as PayByLinkGatewayFactory; use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\ValidTpayChannelListProviderInterface; +use Payum\Core\Security\CypherInterface; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; -use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; +use Sylius\Bundle\PayumBundle\Model\GatewayConfig; use Sylius\Bundle\UiBundle\Registry\TemplateBlock; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Core\Model\PaymentInterface; @@ -21,9 +23,12 @@ class BankListContextProviderTest extends TestCase private ValidTpayChannelListProviderInterface|ObjectProphecy $validTpayChannelListProvider; + private CypherInterface|ObjectProphecy $cypher; + protected function setUp(): void { $this->validTpayChannelListProvider = $this->prophesize(ValidTpayChannelListProviderInterface::class); + $this->cypher = $this->prophesize(CypherInterface::class); } public function test_it_does_not_support_some_template_event_and_pay_by_link_template_name(): void @@ -56,9 +61,10 @@ public function test_it_supports_select_payment_choice_item_form_template_event_ public function test_it_provides_banks_in_template_context_if_method_is_present_in_context(): void { $paymentMethod = $this->prophesize(PaymentMethodInterface::class); - $gatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $gatewayConfig = $this->prophesize(GatewayConfig::class); $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); - $gatewayConfig->getConfig()->willReturn(['type' => 'pay_by_link']); + $gatewayConfig->getFactoryName()->willReturn(PayByLinkGatewayFactory::NAME); + $gatewayConfig->getConfig()->willReturn([]); $this->validTpayChannelListProvider->provide()->willReturn(['3' => 'somebank']); $result = $this @@ -69,6 +75,7 @@ public function test_it_provides_banks_in_template_context_if_method_is_present_ ) ; + $gatewayConfig->decrypt($this->cypher)->shouldBeCalled(); $this->assertSame( [ 'method' => $paymentMethod->reveal(), @@ -84,11 +91,12 @@ public function test_it_provides_banks_in_template_context_if_order_is_present_i $order = $this->prophesize(OrderInterface::class); $payment = $this->prophesize(PaymentInterface::class); $paymentMethod = $this->prophesize(PaymentMethodInterface::class); - $gatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $gatewayConfig = $this->prophesize(GatewayConfig::class); $order->getLastPayment()->willReturn($payment); $payment->getMethod()->willReturn($paymentMethod); $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); - $gatewayConfig->getConfig()->willReturn(['type' => 'pay_by_link']); + $gatewayConfig->getFactoryName()->willReturn(PayByLinkGatewayFactory::NAME); + $gatewayConfig->getConfig()->willReturn([]); $this->validTpayChannelListProvider->provide()->willReturn(['3' => 'somebank']); $result = $this @@ -99,6 +107,7 @@ public function test_it_provides_banks_in_template_context_if_order_is_present_i ) ; + $gatewayConfig->decrypt($this->cypher)->shouldBeCalled(); $this->assertSame( [ 'order' => $order->reveal(), @@ -179,9 +188,10 @@ public function test_it_provides_empty_banks_and_channel_id_in_template_context_ public function test_it_provides_channel_id_and_empty_banks_in_template_context_if_tpay_channel_id_is_specified(): void { $paymentMethod = $this->prophesize(PaymentMethodInterface::class); - $gatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $gatewayConfig = $this->prophesize(GatewayConfig::class); $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); - $gatewayConfig->getConfig()->willReturn(['type' => 'pay_by_link', 'tpay_channel_id' => '3']); + $gatewayConfig->getFactoryName()->willReturn(PayByLinkGatewayFactory::NAME); + $gatewayConfig->getConfig()->willReturn(['tpay_channel_id' => '3']); $result = $this ->createTestObject() @@ -191,6 +201,7 @@ public function test_it_provides_channel_id_and_empty_banks_in_template_context_ ) ; + $gatewayConfig->decrypt($this->cypher)->shouldBeCalled(); $this->validTpayChannelListProvider->provide()->shouldNotBeCalled(); $this->assertSame( [ @@ -204,6 +215,9 @@ public function test_it_provides_channel_id_and_empty_banks_in_template_context_ private function createTestObject(): BankListContextProvider { - return new BankListContextProvider($this->validTpayChannelListProvider->reveal()); + return new BankListContextProvider( + $this->validTpayChannelListProvider->reveal(), + $this->cypher->reveal(), + ); } }