Skip to content

Commit

Permalink
Capture and return data for the Payment configuration endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Prometee committed Jun 24, 2024
1 parent c7dd6d9 commit 40d2636
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Api\PaymentConfiguration;

use FluxSE\PayumStripe\Token\TokenHashKeysInterface;
use FluxSE\SyliusPayumStripePlugin\Api\Payum\CaptureProcessorInterface;
use Payum\Core\Payum;
use Stripe\Checkout\Session;
use Sylius\Bundle\ApiBundle\Payment\PaymentConfigurationProviderInterface;
use Sylius\Component\Core\Model\PaymentInterface;

final class StripeCheckoutSessionPaymentConfigProvider implements PaymentConfigurationProviderInterface
{
use StripePaymentConfigProviderTrait {
StripePaymentConfigProviderTrait::__construct as private __stripePaymentConfigProviderConstruct;
}
private CaptureProcessorInterface $captureProcessor;

public function __construct(
CaptureProcessorInterface $captureProcessor,
string $factoryName
) {
$this->captureProcessor = $captureProcessor;
$this->__stripePaymentConfigProviderConstruct($factoryName);
}

public function provideConfiguration(PaymentInterface $payment): array
{
$config = $this->provideDefaultConfiguration($payment);

$details = $this->captureProcessor->__invoke($payment);
$session = Session::constructFrom($details);
$config['stripe_checkout_session_url'] = $session->url;

return $config;
}
}
37 changes: 37 additions & 0 deletions src/Api/PaymentConfiguration/StripeJsPaymentConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Api\PaymentConfiguration;

use FluxSE\SyliusPayumStripePlugin\Api\Payum\CaptureProcessorInterface;
use Stripe\PaymentIntent;
use Sylius\Bundle\ApiBundle\Payment\PaymentConfigurationProviderInterface;
use Sylius\Component\Core\Model\PaymentInterface;

final class StripeJsPaymentConfigProvider implements PaymentConfigurationProviderInterface
{
use StripePaymentConfigProviderTrait {
StripePaymentConfigProviderTrait::__construct as private __stripePaymentConfigProviderConstruct;
}
private CaptureProcessorInterface $captureProcessor;

public function __construct(
CaptureProcessorInterface $captureProcessor,
string $factoryName
) {
$this->captureProcessor = $captureProcessor;
$this->__stripePaymentConfigProviderConstruct($factoryName);
}

public function provideConfiguration(PaymentInterface $payment): array
{
$config = $this->provideDefaultConfiguration($payment);

$details = $this->captureProcessor->__invoke($payment);
$paymentIntent = PaymentIntent::constructFrom($details);
$config['stripe_payment_intent_client_secret'] = $paymentIntent->client_secret;

return $config;
}
}
58 changes: 58 additions & 0 deletions src/Api/PaymentConfiguration/StripePaymentConfigProviderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Api\PaymentConfiguration;

use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Webmozart\Assert\Assert;

trait StripePaymentConfigProviderTrait
{
private string $factoryName;

public function __construct(string $factoryName)
{
$this->factoryName = $factoryName;
}

public function supports(PaymentMethodInterface $paymentMethod): bool
{
/** @var GatewayConfigInterface|null $gatewayConfig */
$gatewayConfig = $paymentMethod->getGatewayConfig();
Assert::notNull($gatewayConfig);

$config = $gatewayConfig->getConfig();
/** @var string $factory */
$factory = $config['factory'] ?? $gatewayConfig->getFactoryName();

return $factory === $this->factoryName;
}

public function provideDefaultConfiguration(PaymentInterface $payment): array
{
$gatewayConfig = $this->getGatewayConfig($payment);

$config = $gatewayConfig->getConfig();

return [
'publishable_key' => $config['publishable_key'],
'use_authorize' => $config['use_authorize'] ?? false,
];
}

private function getGatewayConfig(PaymentInterface $payment): GatewayConfigInterface
{
/** @var PaymentMethodInterface|null $paymentMethod */
$paymentMethod = $payment->getMethod();
Assert::notNull($paymentMethod, 'Unable to found a PaymentMethod on this Payment.');

/** @var GatewayConfigInterface|null $gatewayConfig */
$gatewayConfig = $paymentMethod->getGatewayConfig();
Assert::notNull($gatewayConfig, 'Unable to found a GatewayConfig on this PaymentMethod.');

return $gatewayConfig;
}
}
31 changes: 31 additions & 0 deletions src/Api/Payum/AfterUrlProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Api\Payum;

use Sylius\Component\Core\Model\PaymentInterface;

final class AfterUrlProvider implements AfterUrlProviderInterface
{
private string $afterPath;
private array $afterParameters;

public function __construct(
string $afterPath,
array $afterParameters = []
) {
$this->afterPath = $afterPath;
$this->afterParameters = $afterParameters;
}

public function getAfterPath(PaymentInterface $payment): string
{
return $this->afterPath;
}

public function getAfterParameters(PaymentInterface $payment): array
{
return $this->afterParameters;
}
}
13 changes: 13 additions & 0 deletions src/Api/Payum/AfterUrlProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Api\Payum;

use Sylius\Component\Core\Model\PaymentInterface;

interface AfterUrlProviderInterface
{
public function getAfterPath(PaymentInterface $payment): string;
public function getAfterParameters(PaymentInterface $payment): array;
}
64 changes: 64 additions & 0 deletions src/Api/Payum/CaptureProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Api\Payum;

use FluxSE\SyliusPayumStripePlugin\Factory\CaptureRequestFactoryInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Payum;
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Webmozart\Assert\Assert;

final class CaptureProcessor implements CaptureProcessorInterface
{
private Payum $payum;
private CaptureRequestFactoryInterface $captureRequestFactory;
private AfterUrlProviderInterface $afterUrlProvider;

public function __construct(
Payum $payum,
CaptureRequestFactoryInterface $captureRequestFactory,
AfterUrlProviderInterface $afterUrlProvider
) {
$this->payum = $payum;
$this->captureRequestFactory = $captureRequestFactory;
$this->afterUrlProvider = $afterUrlProvider;
}
public function __invoke(PaymentInterface $payment): array
{
$tokenFactory = $this->payum->getTokenFactory();
$gatewayName = $this->getGatewayConfig($payment)->getGatewayName();

$token = $tokenFactory->createCaptureToken(
$gatewayName,
$payment,
$this->afterUrlProvider->getAfterPath($payment),
$this->afterUrlProvider->getAfterParameters($payment)
);
$gateway = $this->payum->getGateway($gatewayName);

$captureRequest = $this->captureRequestFactory->createNewWithToken($token);
$gateway->execute($captureRequest, true);

/** @var ArrayObject $details */
$details = $captureRequest->getModel();

return $details->getArrayCopy();
}

private function getGatewayConfig(PaymentInterface $payment): GatewayConfigInterface
{
/** @var PaymentMethodInterface|null $paymentMethod */
$paymentMethod = $payment->getMethod();
Assert::notNull($paymentMethod, 'Unable to found a PaymentMethod on this Payment.');

/** @var GatewayConfigInterface|null $gatewayConfig */
$gatewayConfig = $paymentMethod->getGatewayConfig();
Assert::notNull($gatewayConfig, 'Unable to found a GatewayConfig on this PaymentMethod.');

return $gatewayConfig;
}
}
12 changes: 12 additions & 0 deletions src/Api/Payum/CaptureProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace FluxSE\SyliusPayumStripePlugin\Api\Payum;

use Sylius\Component\Core\Model\PaymentInterface;

interface CaptureProcessorInterface
{
public function __invoke(PaymentInterface $payment): array;
}
91 changes: 0 additions & 91 deletions src/ApiPlatform/Sylius/StripeJsPayment.php

This file was deleted.

0 comments on commit 40d2636

Please sign in to comment.