Skip to content

Commit

Permalink
Do not filter out payment methods if they exist but are disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed Oct 30, 2024
1 parent 9a96ab2 commit c88bfbe
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/services/tpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
->args([
service('commerce_weavers_sylius_tpay.tpay.provider.available_tpay_api_bank_list'),
service('sylius.repository.gateway_config'),
service('sylius.repository.payment_method'),
service('payum.dynamic_gateways.cypher')
])
->alias(AvailableTpayChannelListProviderInterface::class, 'commerce_weavers_sylius_tpay.tpay.provider.validated_tpay_api_bank_list')
Expand Down
7 changes: 7 additions & 0 deletions src/Tpay/Provider/ValidTpayChannelListProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ final class ValidTpayChannelListProvider implements ValidTpayChannelListProvider
public function __construct(
private readonly AvailableTpayChannelListProviderInterface $availableTpayApiBankListProvider,
private readonly RepositoryInterface $gatewayRepository,
private readonly RepositoryInterface $paymentMethodRepository,
private readonly CypherInterface $cypher,
) {
}
Expand Down Expand Up @@ -67,6 +68,12 @@ public function provide(): array
);
}

$disabledPaymentMethods = $this->paymentMethodRepository->findBy(['enabled' => false]);

foreach ($disabledPaymentMethods as $disabledPaymentMethod) {
$paymentMethodsToRemoveByGroupId[] = $disabledPaymentMethod->getId();
}

return array_filter($availableChannels, static function (array $channel) use ($paymentMethodsToRemoveByGroupId): bool {
$groupId = (int) $channel['groups'][0]['id'];

Expand Down
53 changes: 53 additions & 0 deletions tests/Unit/Tpay/Provider/ValidTpayChannelListProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;

final class ValidTpayChannelListProviderTest extends TestCase
Expand Down Expand Up @@ -73,12 +74,15 @@ final class ValidTpayChannelListProviderTest extends TestCase

private RepositoryInterface|ObjectProphecy $gatewayMethodRepository;

private RepositoryInterface|ObjectProphecy $paymentMethodRepository;

private CypherInterface|ObjectProphecy $cypher;

protected function setUp(): void
{
$this->availableTpayApiBankListProvider = $this->prophesize(AvailableTpayChannelListProviderInterface::class);
$this->gatewayMethodRepository = $this->prophesize(RepositoryInterface::class);
$this->paymentMethodRepository = $this->prophesize(RepositoryInterface::class);
$this->cypher = $this->prophesize(CypherInterface::class);
}

Expand Down Expand Up @@ -166,6 +170,8 @@ public function test_it_returns_valid_payments_according_to_available_tpay_payme
])
;

$this->paymentMethodRepository->findBy(['enabled' => false])->willReturn([]);

$result = $this->createTestSubject()->provide();

$expected = self::BANK_LIST;
Expand All @@ -192,6 +198,8 @@ public function test_it_returns_valid_payments_even_if_gateway_config_lacks_type

$this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST);

$this->paymentMethodRepository->findBy(['enabled' => false])->willReturn([]);

$this->gatewayMethodRepository
->findBy(['gatewayName' => 'tpay'])
->willReturn([
Expand All @@ -205,11 +213,56 @@ public function test_it_returns_valid_payments_even_if_gateway_config_lacks_type
$this->assertSame(self::BANK_LIST, $result);
}

public function test_it_returns_valid_payments_even_without_disabled_payments(): void
{
$tpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class);
$tpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class);
$tpayPblGatewayConfigConfig = [
'type' => 'pay_by_link',
];
$tpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled();
$tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig);
$tpayPblPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfigConfig);

$disabledTpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class);
$disabledTpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class);
$disabledTpayPblGatewayConfigConfig = [
'type' => 'visa_mobile',
];

$disabledTpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled();
$disabledTpayPblGatewayConfig->getConfig()->willReturn($disabledTpayPblGatewayConfigConfig);
$disabledTpayPblPaymentMethod->getGatewayConfig()->willReturn($disabledTpayPblGatewayConfigConfig);
$disabledTpayPblPaymentMethod->getId()->willReturn('6');

$this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST);

$this->paymentMethodRepository->findBy(['enabled' => false])->willReturn([
$disabledTpayPblPaymentMethod->reveal(),
]);

$this->gatewayMethodRepository
->findBy(['gatewayName' => 'tpay'])
->willReturn([
$tpayPblGatewayConfig->reveal(),
$disabledTpayPblGatewayConfig->reveal(),
])
;

$result = $this->createTestSubject()->provide();

$expected = self::BANK_LIST;
unset($expected['6']);

$this->assertSame($expected, $result);
}

private function createTestSubject(): ValidTpayChannelListProvider
{
return new ValidTpayChannelListProvider(
$this->availableTpayApiBankListProvider->reveal(),
$this->gatewayMethodRepository->reveal(),
$this->paymentMethodRepository->reveal(),
$this->cypher->reveal(),
);
}
Expand Down

0 comments on commit c88bfbe

Please sign in to comment.