-
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.
Respect Tpay payment channel constraints when displaying available pa…
…yment methods (#226)
- Loading branch information
Showing
17 changed files
with
714 additions
and
3 deletions.
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
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
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
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); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Tpay/Provider/OrderAwareValidTpayChannelListProvider.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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Provider; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
|
||
final class OrderAwareValidTpayChannelListProvider implements OrderAwareValidTpayChannelListProviderInterface | ||
{ | ||
private const FLOAT_AMOUNT_VALUE_TO_INT_MULTIPLIER = 100; | ||
|
||
public function __construct(private readonly ValidTpayChannelListProviderInterface $validTpayChannelListProvider) | ||
{ | ||
} | ||
|
||
public function provide(OrderInterface $order): array | ||
{ | ||
$orderTotal = $order->getTotal(); | ||
$channelList = $this->validTpayChannelListProvider->provide(); | ||
|
||
foreach ($channelList as $key => $channel) { | ||
foreach ($channel['constraints'] ?? [] as $constraint) { | ||
$constraintField = $constraint['field'] ?? null; | ||
$constraintValue = $constraint['value'] ?? null; | ||
$constraintType = $constraint['type'] ?? null; | ||
|
||
if ('amount' !== $constraintField || null == $constraintValue || null === $constraintType) { | ||
continue; | ||
} | ||
|
||
$constraintIntValue = (int) ($constraintValue * self::FLOAT_AMOUNT_VALUE_TO_INT_MULTIPLIER); | ||
|
||
if ( | ||
('min' === $constraintType && $orderTotal < $constraintIntValue) || | ||
('max' === $constraintType && $orderTotal > $constraintIntValue) | ||
) { | ||
unset($channelList[$key]); | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
return $channelList; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Tpay/Provider/OrderAwareValidTpayChannelListProviderInterface.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Provider; | ||
|
||
use Sylius\Component\Core\Model\OrderInterface; | ||
|
||
interface OrderAwareValidTpayChannelListProviderInterface | ||
{ | ||
public function provide(OrderInterface $order): array; | ||
} |
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
Oops, something went wrong.