Skip to content

Commit

Permalink
Extract a service to check pause pay payment method availability
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaGallinari committed Sep 18, 2024
1 parent efc3db8 commit 599c607
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 28 deletions.
13 changes: 13 additions & 0 deletions config/services/checker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Webgriffe\SyliusPausePayPlugin\Checker\PausePayAvailabilityChecker;

return static function (ContainerConfigurator $containerConfigurator) {
$services = $containerConfigurator->services();

$services->set('webgriffe_sylius_pausepay.checker.pause_pay_availability', PausePayAvailabilityChecker::class);
};
7 changes: 6 additions & 1 deletion config/services/resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
49 changes: 49 additions & 0 deletions src/Checker/PausePayAvailabilityChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusPausePayPlugin\Checker;

use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Payment\Model\PaymentInterface as BasePaymentInterface;
use Webgriffe\SyliusPausePayPlugin\Payum\PausePayApi;
use Webmozart\Assert\Assert;

final class PausePayAvailabilityChecker implements PaymentMethodAvailabilityCheckerInterface
{
public function isAvailable(BasePaymentInterface $subject, PaymentMethodInterface $paymentMethod): bool
{
$gatewayConfig = $paymentMethod->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;
}
}
13 changes: 13 additions & 0 deletions src/Checker/PaymentMethodAvailabilityCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Webgriffe\SyliusPausePayPlugin\Checker;

use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Payment\Model\PaymentInterface as BasePaymentInterface;

interface PaymentMethodAvailabilityCheckerInterface
{
public function isAvailable(BasePaymentInterface $subject, PaymentMethodInterface $paymentMethod): bool;
}
30 changes: 4 additions & 26 deletions src/Resolver/PausePayPaymentMethodsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
use Sylius\Component\Payment\Model\PaymentInterface as BasePaymentInterface;
use Sylius\Component\Payment\Resolver\PaymentMethodsResolverInterface;
use Webgriffe\SyliusPausePayPlugin\Payum\PausePayApi;
use Webgriffe\SyliusPausePayPlugin\Checker\PaymentMethodAvailabilityCheckerInterface;
use Webmozart\Assert\Assert;

final class PausePayPaymentMethodsResolver implements PaymentMethodsResolverInterface
{
public function __construct(
private PaymentMethodRepositoryInterface $paymentMethodRepository,
private PaymentMethodAvailabilityCheckerInterface $paymentMethodAvailabilityChecker,
) {
}

Expand All @@ -35,42 +36,19 @@ public function getSupportedMethods(BasePaymentInterface $subject): array
Assert::isInstanceOf($order, OrderInterface::class);
$channel = $order->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);
},
);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/Resolver/PausePayPaymentMethodsResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down

0 comments on commit 599c607

Please sign in to comment.