Skip to content

Commit

Permalink
Fix PBL with channel ID after moving PBL to the spearate context
Browse files Browse the repository at this point in the history
  • Loading branch information
coldic3 committed Nov 26, 2024
1 parent c112bc0 commit f38faa5
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 20 deletions.
8 changes: 0 additions & 8 deletions config/services/context_provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
1 change: 1 addition & 0 deletions config/services/pay_by_link_payment/context_provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
;
Expand Down
23 changes: 20 additions & 3 deletions src/PayByLinkPayment/ContextProvider/BankListContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +23,7 @@ final class BankListContextProvider implements ContextProviderInterface

public function __construct(
private readonly ValidTpayChannelListProviderInterface $validTpayChannelListProvider,
private readonly CypherInterface $cypher,
) {
}

Expand All @@ -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() : [];
Expand Down Expand Up @@ -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();
}
}
19 changes: 19 additions & 0 deletions src/PayByLinkPayment/Form/Type/GatewayConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
)
;
}
}
2 changes: 1 addition & 1 deletion templates/admin/payment_method/test_connection.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% if resource.gatewayConfig.factoryName == 'tpay' %}
{% if resource.gatewayConfig.factoryName == 'tpay_pbl' %}
<div class="ui compact segment" style="display: flex; align-items: center;">
<div class="ui button" id="test-connection-button" style="margin-right: 10px;">{{ 'commerce_weavers_sylius_tpay.admin.gateway_configuration.test_connection'|trans }}</div>
<div class="ui message" id="test-connection-message" style="flex-grow: 1; margin: 0;"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand All @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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()
Expand All @@ -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(
[
Expand All @@ -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(),
);
}
}

0 comments on commit f38faa5

Please sign in to comment.