Skip to content

Commit

Permalink
Provide user with available payment channels according to used paymen…
Browse files Browse the repository at this point in the history
…t methods
  • Loading branch information
arti0090 committed Oct 30, 2024
1 parent 2a21040 commit 9a96ab2
Show file tree
Hide file tree
Showing 14 changed files with 365 additions and 173 deletions.
8 changes: 8 additions & 0 deletions config/services/validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\EncodedGooglePayTokenValidator;
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ForAuthorizedUserOnlyValidator;
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ValidTpayChannelValidator;

return static function(ContainerConfigurator $container): void {
$services = $container->services();
Expand All @@ -20,4 +21,11 @@
])
->tag('validator.constraint_validator')
;

$services->set('commerce_weavers_sylius_tpay.validator.constraint.valid_tpay_channel', ValidTpayChannelValidator::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.provider.validated_tpay_api_bank_list'),
])
->tag('validator.constraint_validator')
;
};
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ parameters:
- '#Comparison operation "<=" between int<1, max> and \(array\|float\|int\) results in an error\.#'
- '#Parameter \#1 \$request \(CommerceWeavers\\SyliusTpayPlugin\\Payum\\Request\\Api\\InitializeApplePayPayment\) of method CommerceWeavers\\SyliusTpayPlugin\\Payum\\Action\\Api\\InitializeApplePayPaymentAction::doExecute\(\) should be contravariant with parameter \$request \(Payum\\Core\\Request\\Generic\) of method CommerceWeavers\\SyliusTpayPlugin\\Payum\\Action\\Api\\BasePaymentAwareAction::doExecute\(\)#'
- '#Parameter \#1 \$request \(CommerceWeavers\\SyliusTpayPlugin\\Payum\\Request\\Api\\Notify\) of method CommerceWeavers\\SyliusTpayPlugin\\Payum\\Action\\Api\\NotifyAction::doExecute\(\) should be contravariant with parameter \$request \(Payum\\Core\\Request\\Generic\) of method CommerceWeavers\\SyliusTpayPlugin\\Payum\\Action\\Api\\BasePaymentAwareAction::doExecute\(\)#'
- '#Parameter \#2 \$callback of function array_filter expects \(callable\(array\|bool\|object\|string\): bool\)\|null, Closure\(array\): bool given#'
6 changes: 2 additions & 4 deletions src/ContextProvider/BankListContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
final class BankListContextProvider implements ContextProviderInterface
{
public function __construct(
private readonly ValidTpayChannelListProviderInterface $validatedTpayApiBankListProvider,
private readonly ValidTpayChannelListProviderInterface $validTpayChannelListProvider,
) {
}

public function provide(array $templateContext, TemplateBlock $templateBlock): array
{
// TODO this is runned few many times when on checkout/complete/choice-item
// propably there it should not be runned totaly but on summary step only
if (isset($templateContext['order'])) {
/** @var OrderInterface $order */
$order = $templateContext['order'];
Expand All @@ -29,7 +27,7 @@ public function provide(array $templateContext, TemplateBlock $templateBlock): a
}
}

$templateContext['banks'] = $this->validatedTpayApiBankListProvider->provide();
$templateContext['banks'] = $this->validTpayChannelListProvider->provide();

return $templateContext;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Form/Type/TpayPaymentDetailsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace CommerceWeavers\SyliusTpayPlugin\Form\Type;

use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\EncodedGooglePayToken;
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ValidTpayApiChannel;
use CommerceWeavers\SyliusTpayPlugin\Validator\Constraint\ValidTpayChannel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
Expand Down Expand Up @@ -71,13 +71,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
)
->add(
'tpay_channel_id',

HiddenType::class,
[
'property_path' => '[tpay_channel_id]',
'validation_groups' => ['sylius_checkout_complete'],
'constraints' => [
new ValidTpayApiChannel(groups: ['sylius_checkout_complete']),
new ValidTpayChannel(groups: ['sylius_checkout_complete']),
],
],
)
Expand Down
12 changes: 12 additions & 0 deletions src/Model/GatewayConfigInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Model;

use Payum\Core\Model\GatewayConfigInterface as BaseGatewayConfigInterface;
use Payum\Core\Security\CryptedInterface;

interface GatewayConfigInterface extends BaseGatewayConfigInterface, CryptedInterface
{
}
16 changes: 7 additions & 9 deletions src/Tpay/Provider/ValidTpayChannelListProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Provider;

use CommerceWeavers\SyliusTpayPlugin\Model\GatewayConfigInterface;
use CommerceWeavers\SyliusTpayPlugin\Payum\Exception\UnableToGetBankListException;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PayGroup;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use Payum\Core\Model\GatewayConfigInterface;
use Payum\Core\Security\CypherInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -56,23 +56,21 @@ public function provide(): array
PaymentType::APPLE_PAY => $paymentMethodsToRemoveByGroupId[] = PayGroup::APPLE_PAY,
PaymentType::GOOGLE_PAY => $paymentMethodsToRemoveByGroupId[] = PayGroup::GOOGLE_PAY,
PaymentType::BLIK => $paymentMethodsToRemoveByGroupId[] = PayGroup::BLIK,
PaymentType::CARD => $paymentMethodsToRemoveByGroupId[] = PayGroup::CARD,
default => null,
};
}

if (!$hasPblPaymentAvailable) {
throw new UnableToGetBankListException(
'Bank list cannot be retrieved if there is no payment method with PayByLink type configured'
'Bank list cannot be retrieved if there is no payment method with PayByLink type configured',
);
}

foreach ($availableChannels as $availableChannel) {
$channelId = (int) $availableChannel['id'];
if (in_array($channelId, $paymentMethodsToRemoveByGroupId, true)) {
unset($availableChannels[$channelId]);
}
}
return array_filter($availableChannels, static function (array $channel) use ($paymentMethodsToRemoveByGroupId): bool {
$groupId = (int) $channel['groups'][0]['id'];

return $availableChannels;
return !in_array($groupId, $paymentMethodsToRemoveByGroupId, true);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Symfony\Component\Validator\Constraint;

final class ValidTpayApiChannel extends Constraint
final class ValidTpayChannel extends Constraint
{
public string $message = 'commerce_weavers_sylius_tpay.shop.pay.tpay_channel.not_valid';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\ValidTpayChannelListProviderInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;

final class ValidTpayApiChannelValidator extends ConstraintValidator
final class ValidTpayChannelValidator extends ConstraintValidator
{
public function __construct(
private readonly ValidTpayChannelListProviderInterface $validatedTpayApiBankListProvider,
) {
}

public function validate($value, Constraint $constraint): void
public function validate(mixed $value, Constraint $constraint): void
{
if (null === $value) {
return;
}

Assert::string($value);

Assert::isInstanceOf($constraint, ValidTpayChannel::class);

$validChannels = $this->validatedTpayApiBankListProvider->provide();

if (array_key_exists($value, $validChannels)) {
Expand Down
20 changes: 10 additions & 10 deletions tests/Unit/ContextProvider/BankListContextProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\ContextProvider;

use CommerceWeavers\SyliusTpayPlugin\ContextProvider\BankListContextProvider;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\AvailableTpayChannelListProviderInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\ValidTpayChannelListProviderInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
Expand All @@ -17,10 +17,10 @@ class BankListContextProviderTest extends TestCase
{
use ProphecyTrait;

private AvailableTpayChannelListProviderInterface|ObjectProphecy $bankListProvider;
private ValidTpayChannelListProviderInterface|ObjectProphecy $validTpayChannelListProvider;
protected function setUp(): void
{
$this->bankListProvider = $this->prophesize(AvailableTpayChannelListProviderInterface::class);
$this->validTpayChannelListProvider = $this->prophesize(ValidTpayChannelListProviderInterface::class);
}

public function test_it_does_not_support_some_template_event_and_pay_by_link_template_name(): void
Expand Down Expand Up @@ -54,8 +54,8 @@ public function test_it_provides_banks_for_context_if_there_is_no_order_in_templ
{
$templateBlock = new TemplateBlock('pay_by_link', 'cw.tpay.shop.select_payment.choice_item_form', null, null, null, null);

$this->bankListProvider->provide()->shouldBeCalled();
$this->bankListProvider->provide()->willReturn(['1' => 'some bank']);
$this->validTpayChannelListProvider->provide()->shouldBeCalled();
$this->validTpayChannelListProvider->provide()->willReturn(['1' => 'some bank']);

$context = $this->createTestObject()->provide(
['i_am_not_an_order' => 'some_context'],
Expand All @@ -72,8 +72,8 @@ public function test_it_provides_nothing_new_for_context_if_order_has_no_payment
$order = $this->prophesize(OrderInterface::class);
$order->getLastPayment()->willReturn(null);

$this->bankListProvider->provide()->shouldNotBeCalled();
$this->bankListProvider->provide()->willReturn(['1' => 'some bank']);
$this->validTpayChannelListProvider->provide()->shouldNotBeCalled();
$this->validTpayChannelListProvider->provide()->willReturn(['1' => 'some bank']);

$context = $this->createTestObject()->provide(
['order' => $order->reveal()],
Expand All @@ -90,8 +90,8 @@ public function test_it_provides_bank_list_as_context_if_order_has_payment(): vo
$payment = $this->prophesize(PaymentInterface::class);
$order->getLastPayment()->willReturn($payment);

$this->bankListProvider->provide()->shouldBeCalled();
$this->bankListProvider->provide()->willReturn(['1' => 'some bank']);
$this->validTpayChannelListProvider->provide()->shouldBeCalled();
$this->validTpayChannelListProvider->provide()->willReturn(['1' => 'some bank']);

$context = $this->createTestObject()->provide(
['order' => $order->reveal()],
Expand All @@ -104,6 +104,6 @@ public function test_it_provides_bank_list_as_context_if_order_has_payment(): vo

private function createTestObject(): BankListContextProvider
{
return new BankListContextProvider($this->bankListProvider->reveal());
return new BankListContextProvider($this->validTpayChannelListProvider->reveal());
}
}
Loading

0 comments on commit 9a96ab2

Please sign in to comment.