-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OrderBasedPaymentMethodsResolver
- Loading branch information
Showing
10 changed files
with
445 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Checker\PaymentMethodSupportedForOrderChecker; | ||
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Checker\PaymentMethodSupportedForOrderCheckerInterface; | ||
|
||
return static function(ContainerConfigurator $container): void { | ||
$services = $container->services(); | ||
|
||
$services->set('commerce_weavers_sylius_tpay.pay_by_link_payment.checker.payment_method_supported_for_order', PaymentMethodSupportedForOrderChecker::class) | ||
->args([ | ||
service('payum.dynamic_gateways.cypher'), | ||
service('commerce_weavers_sylius_tpay.tpay.provider.order_aware_validated_tpay_api_bank_list'), | ||
]) | ||
->alias(PaymentMethodSupportedForOrderCheckerInterface::class, 'commerce_weavers_sylius_tpay.pay_by_link_payment.checker.payment_method_supported_for_order') | ||
; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/PayByLinkPayment/Checker/PaymentMethodSupportedForOrderChecker.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Checker; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Tpay\GatewayName; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\OrderAwareValidTpayChannelListProviderInterface; | ||
use Payum\Core\Security\CryptedInterface; | ||
use Payum\Core\Security\CypherInterface; | ||
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
|
||
final class PaymentMethodSupportedForOrderChecker implements PaymentMethodSupportedForOrderCheckerInterface | ||
{ | ||
public function __construct( | ||
private readonly CypherInterface $cypher, | ||
private readonly OrderAwareValidTpayChannelListProviderInterface $orderAwareValidTpayChannelListProvider, | ||
) { | ||
} | ||
|
||
public function isSupportedForOrder(PaymentMethodInterface $paymentMethod, OrderInterface $order): bool | ||
{ | ||
/** @var GatewayConfigInterface|null $gatewayConfig */ | ||
$gatewayConfig = $paymentMethod->getGatewayConfig(); | ||
|
||
if (null === $gatewayConfig || GatewayName::PAY_BY_LINK !== $gatewayConfig->getFactoryName()) { | ||
return true; | ||
} | ||
|
||
if ($gatewayConfig instanceof CryptedInterface) { | ||
$gatewayConfig->decrypt($this->cypher); | ||
} | ||
|
||
$tpayChannelId = $gatewayConfig->getConfig()['tpay_channel_id'] ?? null; | ||
|
||
if (null === $tpayChannelId) { | ||
return true; | ||
} | ||
|
||
$validTpayChannelList = $this->orderAwareValidTpayChannelListProvider->provide($order); | ||
|
||
return isset($validTpayChannelList[$tpayChannelId]); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/PayByLinkPayment/Checker/PaymentMethodSupportedForOrderCheckerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Checker; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
|
||
interface PaymentMethodSupportedForOrderCheckerInterface | ||
{ | ||
public function isSupportedForOrder(PaymentMethodInterface $paymentMethod, OrderInterface $order): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Payment\Resolver; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Checker\PaymentMethodSupportedForOrderCheckerInterface; | ||
use Sylius\Component\Core\Model\PaymentInterface as CorePaymentInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\Component\Payment\Model\PaymentInterface; | ||
use Sylius\Component\Payment\Resolver\PaymentMethodsResolverInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class OrderBasedPaymentMethodsResolver implements PaymentMethodsResolverInterface | ||
{ | ||
public function __construct( | ||
private readonly PaymentMethodsResolverInterface $paymentMethodsResolver, | ||
private readonly PaymentMethodSupportedForOrderCheckerInterface $paymentMethodSupportedForOrderChecker, | ||
) { | ||
} | ||
|
||
public function getSupportedMethods(PaymentInterface $subject): array | ||
{ | ||
Assert::isInstanceOf($subject, CorePaymentInterface::class); | ||
Assert::true($this->supports($subject), 'This payment method is not support by resolver'); | ||
|
||
/** @var PaymentMethodInterface[] $supportedMethods */ | ||
$supportedMethods = $this->paymentMethodsResolver->getSupportedMethods($subject); | ||
|
||
foreach ($supportedMethods as $key => $supportedMethod) { | ||
$order = $subject->getOrder(); | ||
Assert::notNull($order); | ||
|
||
if ($this->paymentMethodSupportedForOrderChecker->isSupportedForOrder($supportedMethod, $order)) { | ||
continue; | ||
} | ||
|
||
unset($supportedMethods[$key]); | ||
} | ||
|
||
return array_values($supportedMethods); | ||
} | ||
|
||
public function supports(PaymentInterface $subject): bool | ||
{ | ||
return $this->paymentMethodsResolver->supports($subject); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
tests/Unit/PayByLinkPayment/Checker/PaymentMethodSupportedForOrderCheckerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\CommerceWeavers\SyliusTpayPlugin\Unit\PayByLinkPayment\Checker; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\PayByLinkPayment\Checker\PaymentMethodSupportedForOrderChecker; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\GatewayName; | ||
use CommerceWeavers\SyliusTpayPlugin\Tpay\Provider\OrderAwareValidTpayChannelListProviderInterface; | ||
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\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
|
||
final class PaymentMethodSupportedForOrderCheckerTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private CypherInterface|ObjectProphecy $cypher; | ||
|
||
private OrderAwareValidTpayChannelListProviderInterface|ObjectProphecy $orderAwareValidTpayChannelListProvider; | ||
|
||
private PaymentMethodInterface|ObjectProphecy $paymentMethod; | ||
|
||
private OrderInterface|ObjectProphecy $order; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->cypher = $this->prophesize(CypherInterface::class); | ||
$this->orderAwareValidTpayChannelListProvider = $this->prophesize(OrderAwareValidTpayChannelListProviderInterface::class); | ||
$this->paymentMethod = $this->prophesize(PaymentMethodInterface::class); | ||
$this->order = $this->prophesize(OrderInterface::class); | ||
} | ||
|
||
public function test_it_returns_true_if_gateway_config_is_null(): void | ||
{ | ||
$this->paymentMethod->getGatewayConfig()->willReturn(null); | ||
|
||
$result = $this | ||
->createTestSubject() | ||
->isSupportedForOrder($this->paymentMethod->reveal(), $this->order->reveal()) | ||
; | ||
|
||
$this->assertTrue($result); | ||
} | ||
|
||
public function test_it_returns_true_if_gateway_config_factory_name_is_not_pay_by_link(): void | ||
{ | ||
$gatewayConfig = $this->prophesize(GatewayConfigInterface::class); | ||
$this->paymentMethod->getGatewayConfig()->willReturn($gatewayConfig->reveal()); | ||
$gatewayConfig->getFactoryName()->willReturn('i_am_not_pay_by_link'); | ||
|
||
$result = $this | ||
->createTestSubject() | ||
->isSupportedForOrder($this->paymentMethod->reveal(), $this->order->reveal()) | ||
; | ||
|
||
$this->assertTrue($result); | ||
} | ||
|
||
public function test_it_returns_true_if_gateway_config_does_not_have_tpay_channel_id(): void | ||
{ | ||
$gatewayConfig = $this->prophesize(GatewayConfigInterface::class); | ||
$this->paymentMethod->getGatewayConfig()->willReturn($gatewayConfig->reveal()); | ||
$gatewayConfig->getFactoryName()->willReturn(GatewayName::PAY_BY_LINK); | ||
$gatewayConfig->getConfig()->willReturn([]); | ||
|
||
$result = $this | ||
->createTestSubject() | ||
->isSupportedForOrder($this->paymentMethod->reveal(), $this->order->reveal()) | ||
; | ||
|
||
$this->assertTrue($result); | ||
} | ||
|
||
public function test_it_returns_true_if_tpay_channel_id_is_valid(): void | ||
{ | ||
$gatewayConfig = $this->prophesize(GatewayConfigInterface::class); | ||
$this->paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); | ||
$gatewayConfig->getFactoryName()->willReturn(GatewayName::PAY_BY_LINK); | ||
$gatewayConfig->getConfig()->willReturn(['tpay_channel_id' => 21]); | ||
$this->orderAwareValidTpayChannelListProvider | ||
->provide($this->order) | ||
->willReturn([19 => [], 21 => [], 22 => []]) | ||
; | ||
|
||
$result = $this | ||
->createTestSubject() | ||
->isSupportedForOrder($this->paymentMethod->reveal(), $this->order->reveal()) | ||
; | ||
|
||
$this->assertTrue($result); | ||
} | ||
|
||
public function test_it_returns_false_if_tpay_channel_id_is_not_valid(): void | ||
{ | ||
$gatewayConfig = $this->prophesize(GatewayConfigInterface::class); | ||
$this->paymentMethod->getGatewayConfig()->willReturn($gatewayConfig->reveal()); | ||
$gatewayConfig->getFactoryName()->willReturn(GatewayName::PAY_BY_LINK); | ||
$gatewayConfig->getConfig()->willReturn(['tpay_channel_id' => 21]); | ||
$this->orderAwareValidTpayChannelListProvider | ||
->provide($this->order) | ||
->willReturn([19 => [], 22 => []]) | ||
; | ||
|
||
$result = $this | ||
->createTestSubject() | ||
->isSupportedForOrder($this->paymentMethod->reveal(), $this->order->reveal()) | ||
; | ||
|
||
$this->assertFalse($result); | ||
} | ||
|
||
private function createTestSubject(): PaymentMethodSupportedForOrderChecker | ||
{ | ||
return new PaymentMethodSupportedForOrderChecker( | ||
$this->cypher->reveal(), | ||
$this->orderAwareValidTpayChannelListProvider->reveal(), | ||
); | ||
} | ||
} |
Oops, something went wrong.