-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract a service to check pause pay payment method availability
- Loading branch information
1 parent
efc3db8
commit 599c607
Showing
6 changed files
with
87 additions
and
28 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,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); | ||
}; |
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,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; | ||
} | ||
} |
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 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; | ||
} |
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