From d913fa75a6538bcaa92a1458b00a7760375200d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jib=C3=A9=20Barth?= Date: Wed, 4 Dec 2024 16:03:58 +0100 Subject: [PATCH 1/2] PPSYL-106 - Switch amount to authorized_amount when deferred capture is on --- src/Checker/CanSaveCardChecker.php | 23 +++++-------- src/Checker/PayplugFeatureChecker.php | 42 +++++++++++++++++++++++ src/Creator/PayPlugPaymentDataCreator.php | 27 +++++++++++---- 3 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 src/Checker/PayplugFeatureChecker.php diff --git a/src/Checker/CanSaveCardChecker.php b/src/Checker/CanSaveCardChecker.php index 53583a74..8d258fba 100644 --- a/src/Checker/CanSaveCardChecker.php +++ b/src/Checker/CanSaveCardChecker.php @@ -12,12 +12,15 @@ class CanSaveCardChecker implements CanSaveCardCheckerInterface { - /** @var CustomerContextInterface */ - private $customerContext; + private CustomerContextInterface $customerContext; + private PayplugFeatureChecker $payplugFeatureChecker; - public function __construct(CustomerContextInterface $customerContext) - { + public function __construct( + CustomerContextInterface $customerContext, + PayplugFeatureChecker $payplugFeatureChecker, + ) { $this->customerContext = $customerContext; + $this->payplugFeatureChecker = $payplugFeatureChecker; } public function isAllowed(PaymentMethodInterface $paymentMethod): bool @@ -26,16 +29,6 @@ public function isAllowed(PaymentMethodInterface $paymentMethod): bool return false; } - $gatewayConfiguration = $paymentMethod->getGatewayConfig(); - - if (!$gatewayConfiguration instanceof GatewayConfigInterface) { - return false; - } - - if (!\array_key_exists(PayPlugGatewayFactory::ONE_CLICK, $gatewayConfiguration->getConfig())) { - return false; - } - - return (bool) $gatewayConfiguration->getConfig()[PayPlugGatewayFactory::ONE_CLICK] ?? false; + $this->payplugFeatureChecker->isOneClickEnabled($paymentMethod); } } diff --git a/src/Checker/PayplugFeatureChecker.php b/src/Checker/PayplugFeatureChecker.php new file mode 100644 index 00000000..99cbed3b --- /dev/null +++ b/src/Checker/PayplugFeatureChecker.php @@ -0,0 +1,42 @@ +getConfigCheckboxValue($paymentMethod, PayPlugGatewayFactory::DEFERRED_CAPTURE); + } + + public function isIntegratedPaymentEnabled(PaymentMethodInterface $paymentMethod): bool + { + return $this->getConfigCheckboxValue($paymentMethod, PayPlugGatewayFactory::INTEGRATED_PAYMENT); + } + + public function isOneClickEnabled(PaymentMethodInterface $paymentMethod): bool + { + return $this->getConfigCheckboxValue($paymentMethod, PayPlugGatewayFactory::ONE_CLICK); + } + + private function getConfigCheckboxValue(PaymentMethodInterface $paymentMethod, string $configKey): bool + { + $gatewayConfiguration = $paymentMethod->getGatewayConfig(); + + if (!$gatewayConfiguration instanceof GatewayConfigInterface) { + return false; + } + + if (!\array_key_exists($configKey, $gatewayConfiguration->getConfig())) { + return false; + } + + return (bool) ($gatewayConfiguration->getConfig()[$configKey] ?? false); + } +} diff --git a/src/Creator/PayPlugPaymentDataCreator.php b/src/Creator/PayPlugPaymentDataCreator.php index ae99e267..ab5d2935 100644 --- a/src/Creator/PayPlugPaymentDataCreator.php +++ b/src/Creator/PayPlugPaymentDataCreator.php @@ -11,6 +11,7 @@ use libphonenumber\PhoneNumberType; use libphonenumber\PhoneNumberUtil as PhoneNumberUtil; use PayPlug\SyliusPayPlugPlugin\Checker\CanSaveCardCheckerInterface; +use PayPlug\SyliusPayPlugPlugin\Checker\PayplugFeatureChecker; use PayPlug\SyliusPayPlugPlugin\Entity\Card; use PayPlug\SyliusPayPlugPlugin\Gateway\AmericanExpressGatewayFactory; use PayPlug\SyliusPayPlugPlugin\Gateway\ApplePayGatewayFactory; @@ -31,25 +32,24 @@ class PayPlugPaymentDataCreator { private const DELIVERY_TYPE_BILLING = 'BILLING'; - private const DELIVERY_TYPE_NEW = 'NEW'; - private const PAYPLUG_CARD_ID_OTHER = 'other'; private CanSaveCardCheckerInterface $canSaveCardChecker; - private RepositoryInterface $payplugCardRepository; - private RequestStack $requestStack; + private PayplugFeatureChecker $payplugFeatureChecker; public function __construct( CanSaveCardCheckerInterface $canSaveCard, RepositoryInterface $payplugCardRepository, - RequestStack $requestStack + RequestStack $requestStack, + PayplugFeatureChecker $payplugFeatureChecker, ) { $this->canSaveCardChecker = $canSaveCard; $this->payplugCardRepository = $payplugCardRepository; $this->requestStack = $requestStack; + $this->payplugFeatureChecker = $payplugFeatureChecker; } public function create( @@ -93,7 +93,8 @@ public function create( if (PayPlugGatewayFactory::FACTORY_NAME === $gatewayFactoryName && $paymentMethod instanceof PaymentMethodInterface) { $details['allow_save_card'] = false; - $details = $this->alterPayPlugDetails($paymentMethod, $details); + $details = $this->alterPayPlugDetailsForOneClick($paymentMethod, $details); + $details = $this->alterPayPlugDetailsForDeferredCapture($paymentMethod, $details); } if (OneyGatewayFactory::FACTORY_NAME === $gatewayFactoryName) { @@ -233,7 +234,7 @@ private function addShippingInfo( ]; } - private function alterPayPlugDetails(PaymentMethodInterface $paymentMethod, ArrayObject $details): ArrayObject + private function alterPayPlugDetailsForOneClick(PaymentMethodInterface $paymentMethod, ArrayObject $details): ArrayObject { if (!$this->canSaveCardChecker->isAllowed($paymentMethod)) { return $details; @@ -266,6 +267,18 @@ private function alterPayPlugDetails(PaymentMethodInterface $paymentMethod, Arra return $details; } + private function alterPayPlugDetailsForDeferredCapture(PaymentMethodInterface $paymentMethod, ArrayObject $details): ArrayObject + { + if (!$this->payplugFeatureChecker->isDeferredCaptureEnabled($paymentMethod)) { + return $details; + } + + $details['authorized_amount'] = $details['amount']; + unset($details['amount']); + + return $details; + } + private function alterOneyDetails(ArrayObject $details): ArrayObject { $details['payment_method'] = $this->requestStack->getSession()->get('oney_payment_method', 'oney_x3_with_fees'); From ef6daff62c0d86ad67088dd40235e81bd196ca20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jib=C3=A9=20Barth?= Date: Tue, 10 Dec 2024 13:57:24 +0100 Subject: [PATCH 2/2] PPSYL-106 - Fix missing return --- src/Checker/CanSaveCardChecker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Checker/CanSaveCardChecker.php b/src/Checker/CanSaveCardChecker.php index 8d258fba..1a0bd72f 100644 --- a/src/Checker/CanSaveCardChecker.php +++ b/src/Checker/CanSaveCardChecker.php @@ -29,6 +29,6 @@ public function isAllowed(PaymentMethodInterface $paymentMethod): bool return false; } - $this->payplugFeatureChecker->isOneClickEnabled($paymentMethod); + return $this->payplugFeatureChecker->isOneClickEnabled($paymentMethod); } }