From 40d2636e630e00cd01d7a5288fe75f8719310232 Mon Sep 17 00:00:00 2001 From: Francis Hilaire Date: Mon, 24 Jun 2024 21:25:40 +0200 Subject: [PATCH] Capture and return data for the Payment configuration endpoint --- ...peCheckoutSessionPaymentConfigProvider.php | 39 ++++++++ .../StripeJsPaymentConfigProvider.php | 37 ++++++++ .../StripePaymentConfigProviderTrait.php | 58 ++++++++++++ src/Api/Payum/AfterUrlProvider.php | 31 +++++++ src/Api/Payum/AfterUrlProviderInterface.php | 13 +++ src/Api/Payum/CaptureProcessor.php | 64 +++++++++++++ src/Api/Payum/CaptureProcessorInterface.php | 12 +++ src/ApiPlatform/Sylius/StripeJsPayment.php | 91 ------------------- 8 files changed, 254 insertions(+), 91 deletions(-) create mode 100644 src/Api/PaymentConfiguration/StripeCheckoutSessionPaymentConfigProvider.php create mode 100644 src/Api/PaymentConfiguration/StripeJsPaymentConfigProvider.php create mode 100644 src/Api/PaymentConfiguration/StripePaymentConfigProviderTrait.php create mode 100644 src/Api/Payum/AfterUrlProvider.php create mode 100644 src/Api/Payum/AfterUrlProviderInterface.php create mode 100644 src/Api/Payum/CaptureProcessor.php create mode 100644 src/Api/Payum/CaptureProcessorInterface.php delete mode 100644 src/ApiPlatform/Sylius/StripeJsPayment.php diff --git a/src/Api/PaymentConfiguration/StripeCheckoutSessionPaymentConfigProvider.php b/src/Api/PaymentConfiguration/StripeCheckoutSessionPaymentConfigProvider.php new file mode 100644 index 0000000..de6d623 --- /dev/null +++ b/src/Api/PaymentConfiguration/StripeCheckoutSessionPaymentConfigProvider.php @@ -0,0 +1,39 @@ +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; + } +} diff --git a/src/Api/PaymentConfiguration/StripeJsPaymentConfigProvider.php b/src/Api/PaymentConfiguration/StripeJsPaymentConfigProvider.php new file mode 100644 index 0000000..d76938e --- /dev/null +++ b/src/Api/PaymentConfiguration/StripeJsPaymentConfigProvider.php @@ -0,0 +1,37 @@ +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; + } +} diff --git a/src/Api/PaymentConfiguration/StripePaymentConfigProviderTrait.php b/src/Api/PaymentConfiguration/StripePaymentConfigProviderTrait.php new file mode 100644 index 0000000..01b176b --- /dev/null +++ b/src/Api/PaymentConfiguration/StripePaymentConfigProviderTrait.php @@ -0,0 +1,58 @@ +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; + } +} diff --git a/src/Api/Payum/AfterUrlProvider.php b/src/Api/Payum/AfterUrlProvider.php new file mode 100644 index 0000000..b3c38a5 --- /dev/null +++ b/src/Api/Payum/AfterUrlProvider.php @@ -0,0 +1,31 @@ +afterPath = $afterPath; + $this->afterParameters = $afterParameters; + } + + public function getAfterPath(PaymentInterface $payment): string + { + return $this->afterPath; + } + + public function getAfterParameters(PaymentInterface $payment): array + { + return $this->afterParameters; + } +} diff --git a/src/Api/Payum/AfterUrlProviderInterface.php b/src/Api/Payum/AfterUrlProviderInterface.php new file mode 100644 index 0000000..af16b4e --- /dev/null +++ b/src/Api/Payum/AfterUrlProviderInterface.php @@ -0,0 +1,13 @@ +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; + } +} diff --git a/src/Api/Payum/CaptureProcessorInterface.php b/src/Api/Payum/CaptureProcessorInterface.php new file mode 100644 index 0000000..96019cb --- /dev/null +++ b/src/Api/Payum/CaptureProcessorInterface.php @@ -0,0 +1,12 @@ +payum = $payum; - } - - 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 === 'stripe_js'; - } - - public function provideConfiguration(PaymentInterface $payment): array - { - $gatewayConfig = $this->getGatewayConfig($payment); - - $config = $gatewayConfig->getConfig(); - - $paymentIntent = $this->capturePayment($payment); - - return [ - 'publishable_key' => $config['publishable_key'], - 'client_secret' => $paymentIntent->client_secret, - ]; - } - - private function capturePayment(PaymentInterface $payment): PaymentIntent - { - $tokenFactory = $this->payum->getTokenFactory(); - $gatewayConfig = $this->getGatewayConfig($payment); - - $gatewayName = $gatewayConfig->getGatewayName(); - $token = $tokenFactory->createCaptureToken( - $gatewayName, - $payment, - 'sylius_shop_homepage' - ); - - $request = new Capture($token); - $gateway = $this->payum->getGateway($gatewayName); - - // Execute with catchReply to avoid displaying something here - /*$reply = */$gateway->execute($request, true); - // Reply contain a Payum\Core\Reply\HttpResponse with the rendered template - - /** @var ArrayObject $details */ - $details = $request->getModel(); - - return PaymentIntent::constructFrom($details->getArrayCopy()); - } - - private function getGatewayConfig(PaymentInterface $payment): GatewayConfigInterface - { - /** @var PaymentMethodInterface|null $paymentMethod */ - $paymentMethod = $payment->getMethod(); - Assert::notNull($paymentMethod); - - /** @var GatewayConfigInterface|null $gatewayConfig */ - $gatewayConfig = $paymentMethod->getGatewayConfig(); - Assert::notNull($gatewayConfig); - - return $gatewayConfig; - } -}