From 599c607982b54bcd8798c4e841504b3de206e4b3 Mon Sep 17 00:00:00 2001 From: Luca Gallinari Date: Wed, 18 Sep 2024 12:19:10 +0200 Subject: [PATCH] Extract a service to check pause pay payment method availability --- config/services/checker.php | 13 +++++ config/services/resolver.php | 7 ++- src/Checker/PausePayAvailabilityChecker.php | 49 +++++++++++++++++++ ...mentMethodAvailabilityCheckerInterface.php | 13 +++++ .../PausePayPaymentMethodsResolver.php | 30 ++---------- .../PausePayPaymentMethodsResolverTest.php | 3 +- 6 files changed, 87 insertions(+), 28 deletions(-) create mode 100644 config/services/checker.php create mode 100644 src/Checker/PausePayAvailabilityChecker.php create mode 100644 src/Checker/PaymentMethodAvailabilityCheckerInterface.php diff --git a/config/services/checker.php b/config/services/checker.php new file mode 100644 index 0000000..44cbcb7 --- /dev/null +++ b/config/services/checker.php @@ -0,0 +1,13 @@ +services(); + + $services->set('webgriffe_sylius_pausepay.checker.pause_pay_availability', PausePayAvailabilityChecker::class); +}; diff --git a/config/services/resolver.php b/config/services/resolver.php index 477c540..c1f3b61 100644 --- a/config/services/resolver.php +++ b/config/services/resolver.php @@ -17,7 +17,12 @@ ->args([service('webgriffe_sylius_pausepay.provider.configuration'),]); $services->set('webgriffe_sylius_pausepay.resolver.payment_methods', PausePayPaymentMethodsResolver::class) - ->args([service('sylius.repository.payment_method'),]) + ->args( + [ + service('sylius.repository.payment_method'), + service('webgriffe_sylius_pausepay.checker.pause_pay_availability'), + ] + ) ->tag('sylius.payment_method_resolver', [ 'type' => 'pausepay', 'label' => 'PausePay', diff --git a/src/Checker/PausePayAvailabilityChecker.php b/src/Checker/PausePayAvailabilityChecker.php new file mode 100644 index 0000000..84e18d9 --- /dev/null +++ b/src/Checker/PausePayAvailabilityChecker.php @@ -0,0 +1,49 @@ +getGatewayConfig(); + if ($gatewayConfig === null) { // we cannot be sure that it's PausePay payment method, so it is available + return true; + } + + /** @psalm-suppress DeprecatedMethod */ + if ($gatewayConfig->getFactoryName() !== PausePayApi::GATEWAY_CODE) { + return true; + } + + Assert::isInstanceOf($subject, PaymentInterface::class); + $order = $subject->getOrder(); + Assert::isInstanceOf($order, OrderInterface::class); + + $billingAddress = $order->getBillingAddress(); + Assert::isInstanceOf($billingAddress, AddressInterface::class); + if ($billingAddress->getCountryCode() !== 'IT') { + return false; + } + + $currencyCode = $order->getCurrencyCode(); + Assert::notNull($currencyCode); + if ($currencyCode !== 'EUR') { + return false; + } + + $orderAmount = $order->getTotal(); + + return $orderAmount >= 50000 && $orderAmount <= 2000000; + } +} diff --git a/src/Checker/PaymentMethodAvailabilityCheckerInterface.php b/src/Checker/PaymentMethodAvailabilityCheckerInterface.php new file mode 100644 index 0000000..35dc190 --- /dev/null +++ b/src/Checker/PaymentMethodAvailabilityCheckerInterface.php @@ -0,0 +1,13 @@ +getChannel(); Assert::isInstanceOf($channel, ChannelInterface::class); - $billingAddress = $order->getBillingAddress(); - Assert::isInstanceOf($billingAddress, AddressInterface::class); - // todo: should we check the shipping address too? - $currencyCode = $order->getCurrencyCode(); - Assert::notNull($currencyCode); - $orderAmount = $order->getTotal(); /** @var PaymentMethodInterface[] $paymentMethods */ $paymentMethods = $this->paymentMethodRepository->findEnabledForChannel($channel); return array_filter( $paymentMethods, - static function (PaymentMethodInterface $paymentMethod) use ( - $billingAddress, - $currencyCode, - $orderAmount - ) { + function (PaymentMethodInterface $paymentMethod) use ($subject) { $gatewayConfig = $paymentMethod->getGatewayConfig(); if ($gatewayConfig === null) { return false; } - /** @psalm-suppress DeprecatedMethod */ - if ($gatewayConfig->getFactoryName() !== PausePayApi::GATEWAY_CODE) { - return true; - } - if ($billingAddress->getCountryCode() !== 'IT') { - return false; - } - if ($currencyCode !== 'EUR') { - return false; - } - if ($orderAmount < 50000 || $orderAmount > 2000000) { - return false; - } - return true; + return $this->paymentMethodAvailabilityChecker->isAvailable($subject, $paymentMethod); }, ); } diff --git a/tests/Unit/Resolver/PausePayPaymentMethodsResolverTest.php b/tests/Unit/Resolver/PausePayPaymentMethodsResolverTest.php index de0a48e..2d40b12 100644 --- a/tests/Unit/Resolver/PausePayPaymentMethodsResolverTest.php +++ b/tests/Unit/Resolver/PausePayPaymentMethodsResolverTest.php @@ -18,6 +18,7 @@ use Sylius\Component\Core\Model\PaymentMethodInterface; use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface; use Sylius\Component\Payment\Resolver\PaymentMethodsResolverInterface; +use Webgriffe\SyliusPausePayPlugin\Checker\PausePayAvailabilityChecker; use Webgriffe\SyliusPausePayPlugin\Payum\PausePayApi; use Webgriffe\SyliusPausePayPlugin\Resolver\PausePayPaymentMethodsResolver; @@ -37,7 +38,7 @@ protected function setUp(): void $this->getPaymentMethod(PausePayApi::GATEWAY_CODE), ] ); - $this->resolver = new PausePayPaymentMethodsResolver($repoMock); + $this->resolver = new PausePayPaymentMethodsResolver($repoMock, new PausePayAvailabilityChecker()); } public function test_it_supports_payment(): void