-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Capture and return data for the Payment configuration endpoint
- Loading branch information
Showing
8 changed files
with
254 additions
and
91 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/Api/PaymentConfiguration/StripeCheckoutSessionPaymentConfigProvider.php
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,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
37
src/Api/PaymentConfiguration/StripeJsPaymentConfigProvider.php
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,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
58
src/Api/PaymentConfiguration/StripePaymentConfigProviderTrait.php
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,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; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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 FluxSE\SyliusPayumStripePlugin\Api\Payum; | ||
|
||
use Sylius\Component\Core\Model\PaymentInterface; | ||
|
||
interface AfterUrlProviderInterface | ||
{ | ||
public function getAfterPath(PaymentInterface $payment): string; | ||
public function getAfterParameters(PaymentInterface $payment): array; | ||
} |
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,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; | ||
} | ||
} |
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,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; | ||
} |
This file was deleted.
Oops, something went wrong.