Skip to content

Commit

Permalink
PPSYL-106 - Switch amount to authorized_amount when deferred capture …
Browse files Browse the repository at this point in the history
…is on
  • Loading branch information
Jibbarth committed Dec 4, 2024
1 parent 921270b commit d913fa7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 22 deletions.
23 changes: 8 additions & 15 deletions src/Checker/CanSaveCardChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Check failure on line 32 in src/Checker/CanSaveCardChecker.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 Symfony 6.2.*

Method PayPlug\SyliusPayPlugPlugin\Checker\CanSaveCardChecker::isAllowed() should return bool but return statement is missing.
}
}
42 changes: 42 additions & 0 deletions src/Checker/PayplugFeatureChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace PayPlug\SyliusPayPlugPlugin\Checker;

use PayPlug\SyliusPayPlugPlugin\Gateway\PayPlugGatewayFactory;
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;

class PayplugFeatureChecker
{
public function isDeferredCaptureEnabled(PaymentMethodInterface $paymentMethod): bool
{
return $this->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);
}
}
27 changes: 20 additions & 7 deletions src/Creator/PayPlugPaymentDataCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit d913fa7

Please sign in to comment.