Skip to content

Commit

Permalink
Fix Visa Mobile-related services
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubtobiasz committed Nov 28, 2024
1 parent 492701d commit 2cb2631
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 75 deletions.
1 change: 0 additions & 1 deletion config/services/tpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@
service('commerce_weavers_sylius_tpay.tpay.provider.available_tpay_api_bank_list'),
service('sylius.repository.payment_method'),
service('sylius.context.channel'),
service('payum.dynamic_gateways.cypher')
])
->alias(ValidTpayChannelListProviderInterface::class, 'commerce_weavers_sylius_tpay.tpay.provider.validated_tpay_api_bank_list')
;
Expand Down
7 changes: 6 additions & 1 deletion src/Repository/PaymentMethodRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
namespace CommerceWeavers\SyliusTpayPlugin\Repository;

use Sylius\Component\Channel\Model\ChannelInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface as BasePaymentMethodRepositoryInterface;

interface PaymentMethodRepositoryInterface extends BasePaymentMethodRepositoryInterface
{
public function findByChannelAndGatewayConfigNameWithGatewayConfig(ChannelInterface $channel, string $gatewayConfigName): array;
/**
* @param array<string> $gatewayConfigNames
* @return array<PaymentMethodInterface>
*/
public function findByChannelAndGatewayConfigNameWithGatewayConfig(ChannelInterface $channel, array $gatewayConfigNames): array;
}
7 changes: 4 additions & 3 deletions src/Repository/PaymentMethodTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CommerceWeavers\SyliusTpayPlugin\Repository;

use CommerceWeavers\SyliusTpayPlugin\Tpay\GatewayName;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join;
use Sylius\Component\Channel\Model\ChannelInterface;
Expand All @@ -13,16 +14,16 @@
*/
trait PaymentMethodTrait
{
public function findByChannelAndGatewayConfigNameWithGatewayConfig(ChannelInterface $channel, string $gatewayConfigName): array
public function findByChannelAndGatewayConfigNameWithGatewayConfig(ChannelInterface $channel, array $gatewayConfigNames): array
{
return $this->createQueryBuilder('o')
->addSelect('gatewayConfig')
->innerJoin('o.gatewayConfig', 'gatewayConfig', Join::WITH, 'gatewayConfig.gatewayName = :gatewayName')
->innerJoin('o.gatewayConfig', 'gatewayConfig', Join::WITH, 'gatewayConfig.gatewayName IN (:gatewayNames)')
->where('o.enabled = :enabled')
->andWhere(':channel MEMBER OF o.channels')
->setParameter('enabled', true)
->setParameter('channel', $channel)
->setParameter('gatewayName', $gatewayConfigName)
->setParameter('gatewayNames', GatewayName::all())
->getQuery()
->getResult()
;
Expand Down
38 changes: 38 additions & 0 deletions src/Tpay/GatewayName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Tpay;

class GatewayName
{
public const APPLE_PAY = 'tpay_apple_pay';

public const BLIK = 'tpay_blik';

public const CARD = 'tpay_card';

public const GOOGLE_PAY = 'tpay_google_pay';

public const PAY_BY_LINK = 'tpay_pay_by_link';

public const REDIRECT = 'tpay_redirect';

public const VISA_MOBILE = 'tpay_visa_mobile';

/**
* @return string[]
*/
public static function all(): array
{
return [
self::APPLE_PAY,
self::BLIK,
self::CARD,
self::GOOGLE_PAY,
self::PAY_BY_LINK,
self::REDIRECT,
self::VISA_MOBILE,
];
}
}
32 changes: 10 additions & 22 deletions src/Tpay/Provider/ValidTpayChannelListProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Provider;

use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Exception\UnableToGetBankListException;
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Factory\GatewayFactory;
use CommerceWeavers\SyliusTpayPlugin\Repository\PaymentMethodRepositoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\GatewayName;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PayGroup;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use Payum\Core\Security\CryptedInterface;
use Payum\Core\Security\CypherInterface;
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
Expand All @@ -26,7 +24,6 @@ public function __construct(
private readonly AvailableTpayChannelListProviderInterface $availableTpayApiBankListProvider,
private readonly PaymentMethodRepositoryInterface $paymentMethodRepository,
private readonly ChannelContextInterface $channelContext,
private readonly CypherInterface $cypher,
) {
}

Expand All @@ -35,19 +32,15 @@ public function provide(): array
/** @var PaymentMethodInterface[] $paymentMethods */
$paymentMethods = $this->paymentMethodRepository->findByChannelAndGatewayConfigNameWithGatewayConfig(
$this->channelContext->getChannel(),
'tpay',
GatewayName::all(),
);

if ([] === $paymentMethods) {
return [];
}

Assert::notEmpty($paymentMethods, 'There is no payment method of Tpay type available');

/** @var PaymentMethodInterface[] $payByLinkPaymentMethods */
$payByLinkPaymentMethods = $this->paymentMethodRepository->findByChannelAndGatewayConfigNameWithGatewayConfig(
$this->channelContext->getChannel(),
GatewayFactory::NAME,
[GatewayName::PAY_BY_LINK],
);

if ([] === $payByLinkPaymentMethods) {
Expand All @@ -66,23 +59,18 @@ public function provide(): array
continue;
}

$tpayGatewayConfig->decrypt($this->cypher);
$config = $tpayGatewayConfig->getConfig();

if (!array_key_exists('type', $config)) {
continue;
}
$gatewayName = $tpayGatewayConfig->getGatewayName();

match ($config['type']) {
PaymentType::VISA_MOBILE => array_push(
match ($gatewayName) {
GatewayName::VISA_MOBILE => array_push(
$paymentMethodsToRemoveByGroupId,
PayGroup::VISA_MOBILE,
PayGroup::VISA_MOBILE_ON_SITE,
),
PaymentType::APPLE_PAY => $paymentMethodsToRemoveByGroupId[] = PayGroup::APPLE_PAY,
PaymentType::GOOGLE_PAY => $paymentMethodsToRemoveByGroupId[] = PayGroup::GOOGLE_PAY,
PaymentType::BLIK => $paymentMethodsToRemoveByGroupId[] = PayGroup::BLIK,
PaymentType::CARD => $paymentMethodsToRemoveByGroupId[] = PayGroup::CARD,
GatewayName::APPLE_PAY => $paymentMethodsToRemoveByGroupId[] = PayGroup::APPLE_PAY,
GatewayName::GOOGLE_PAY => $paymentMethodsToRemoveByGroupId[] = PayGroup::GOOGLE_PAY,
GatewayName::BLIK => $paymentMethodsToRemoveByGroupId[] = PayGroup::BLIK,
GatewayName::CARD => $paymentMethodsToRemoveByGroupId[] = PayGroup::CARD,
default => null,
};
}
Expand Down
71 changes: 23 additions & 48 deletions tests/Unit/Tpay/Provider/ValidTpayChannelListProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Payum\Exception\UnableToGetBankListException;
use CommerceWeavers\SyliusTpayPlugin\Repository\PaymentMethodRepositoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\GatewayName;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\AvailableTpayChannelListProviderInterface;
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\ValidTpayChannelListProvider;
use Payum\Core\Security\CypherInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
Expand Down Expand Up @@ -86,14 +86,11 @@ final class ValidTpayChannelListProviderTest extends TestCase

private ChannelContextInterface|ObjectProphecy $channelContext;

private CypherInterface|ObjectProphecy $cypher;

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

public function test_it_throws_exception_if_there_is_no_tpay_based_payment(): void
Expand All @@ -108,7 +105,7 @@ public function test_it_throws_exception_if_there_is_no_tpay_based_payment(): vo

$this->paymentMethodRepository->findByChannelAndGatewayConfigNameWithGatewayConfig(
$channel,
'tpay'
GatewayName::all(),
)->willReturn([]);

$this->createTestSubject()->provide();
Expand All @@ -125,13 +122,13 @@ public function test_it_throws_exception_if_there_is_no_gateway_config_that_is_p
$this->channelContext->getChannel()->willReturn($channel->reveal());

$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay')
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, GatewayName::all())
->willReturn([
$notTpayBasedPaymentMethod
])
;
$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay_pbl')
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, [GatewayName::PAY_BY_LINK])
->willReturn([])
;

Expand All @@ -142,27 +139,22 @@ public function test_it_returns_all_available_payments_if_only_tpay_payment_meth
{
$tpayBasedPaymentMethod = $this->prophesize(PaymentMethodInterface::class);
$tpayPblGatewayConfig = $this->prophesize(GatewayConfig::class);
$tpayPblGatewayConfigConfig = [
'type' => 'pay_by_link',
];
$tpayPblGatewayConfig->getGatewayName()->willReturn(GatewayName::PAY_BY_LINK);
$tpayBasedPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfig);

$tpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled();
$tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig);

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

$channel = $this->prophesize(ChannelInterface::class);
$this->channelContext->getChannel()->willReturn($channel->reveal());

$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay')
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, GatewayName::all())
->willReturn([
$tpayBasedPaymentMethod->reveal(),
])
;
$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay_pbl')
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, [GatewayName::PAY_BY_LINK])
->willReturn([
$this->prophesize(PaymentMethodInterface::class)->reveal(),
])
Expand All @@ -176,37 +168,29 @@ public function test_it_returns_all_available_payments_if_only_tpay_payment_meth
public function test_it_returns_valid_payments_according_to_available_tpay_payment_methods(): void
{
$tpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class);
$tpayPblGatewayConfig = $this->prophesize(GatewayConfig::class);
$tpayPblGatewayConfigConfig = [
'type' => 'pay_by_link',
];
$tpayPblPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfig);
$tpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled();
$tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig);
$tpayPblPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfig = $this->prophesize(GatewayConfig::class));

$tpayPblGatewayConfig->getGatewayName()->willReturn(GatewayName::PAY_BY_LINK);

$anotherTpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class);
$anotherTpayPblGatewayConfig = $this->prophesize(GatewayConfig::class);
$anotherTpayPblGatewayConfigConfig = [
'type' => 'visa_mobile',
];
$anotherTpayPblPaymentMethod->getGatewayConfig()->willReturn($anotherTpayPblGatewayConfig);
$anotherTpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled();
$anotherTpayPblGatewayConfig->getConfig()->willReturn($anotherTpayPblGatewayConfigConfig);
$anotherTpayPblPaymentMethod->getGatewayConfig()->willReturn($anotherTpayPblGatewayConfig = $this->prophesize(GatewayConfig::class));

$anotherTpayPblGatewayConfig->getGatewayName()->willReturn(GatewayName::VISA_MOBILE);

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

$channel = $this->prophesize(ChannelInterface::class);
$this->channelContext->getChannel()->willReturn($channel->reveal());

$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay')
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, GatewayName::all())
->willReturn([
$tpayPblPaymentMethod->reveal(),
$anotherTpayPblPaymentMethod->reveal(),
])
;
$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay_pbl')
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, [GatewayName::PAY_BY_LINK])
->willReturn([
$this->prophesize(PaymentMethodInterface::class)->reveal(),
])
Expand All @@ -224,37 +208,29 @@ public function test_it_returns_valid_payments_according_to_available_tpay_payme
public function test_it_returns_valid_payments_even_if_gateway_config_lacks_type(): void
{
$tpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class);
$tpayPblGatewayConfig = $this->prophesize(GatewayConfig::class);
$tpayPblGatewayConfigConfig = [
'type' => 'pay_by_link',
];
$tpayPblPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfig);
$tpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled();
$tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig);
$tpayPblPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfig = $this->prophesize(GatewayConfig::class));

$tpayPblGatewayConfig->getGatewayName()->willReturn(GatewayName::PAY_BY_LINK);

$anotherTpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class);
$anotherTpayPblGatewayConfig = $this->prophesize(GatewayConfig::class);
$anotherTpayPblGatewayConfigConfig = [
'i_have_no_type' => 'i_should_still_work',
];
$anotherTpayPblPaymentMethod->getGatewayConfig()->willReturn($anotherTpayPblGatewayConfig);
$anotherTpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled();
$anotherTpayPblGatewayConfig->getConfig()->willReturn($anotherTpayPblGatewayConfigConfig);
$anotherTpayPblPaymentMethod->getGatewayConfig()->willReturn($anotherTpayPblGatewayConfig = $this->prophesize(GatewayConfig::class));

$anotherTpayPblGatewayConfig->getGatewayName()->willReturn('i_should_still_work');

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

$channel = $this->prophesize(ChannelInterface::class);
$this->channelContext->getChannel()->willReturn($channel->reveal());

$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay')
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, GatewayName::all())
->willReturn([
$tpayPblPaymentMethod->reveal(),
$anotherTpayPblPaymentMethod->reveal(),
])
;
$this->paymentMethodRepository
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay_pbl')
->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, [GatewayName::PAY_BY_LINK])
->willReturn([
$this->prophesize(PaymentMethodInterface::class)->reveal(),
])
Expand All @@ -271,7 +247,6 @@ private function createTestSubject(): ValidTpayChannelListProvider
$this->availableTpayApiBankListProvider->reveal(),
$this->paymentMethodRepository->reveal(),
$this->channelContext->reveal(),
$this->cypher->reveal(),
);
}
}

0 comments on commit 2cb2631

Please sign in to comment.