From 4e3cc4f3fbc18dea70fbd648566c34827019ddac Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Fri, 24 Nov 2023 11:48:31 +0100 Subject: [PATCH] Redirect customer to next step in invest controller --- .../Method/StripeSubscriptionPaymentMethod.php | 17 ++++++++++++++--- src/Omnipay/Stripe/Subscription/Gateway.php | 8 ++++++++ .../Message/SubscriptionRequest.php | 13 +++++++++---- .../Message/SubscriptionResponse.php | 2 +- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/Goteo/Payment/Method/StripeSubscriptionPaymentMethod.php b/src/Goteo/Payment/Method/StripeSubscriptionPaymentMethod.php index 541c6b08dc..181a9010e8 100644 --- a/src/Goteo/Payment/Method/StripeSubscriptionPaymentMethod.php +++ b/src/Goteo/Payment/Method/StripeSubscriptionPaymentMethod.php @@ -61,11 +61,14 @@ public function isPublic($amount = 0): bool return true; } - public function purchase(): ResponseInterface + public function getGateway(): SubscriptionGateway { - $gateway = Omnipay::create(SubscriptionGateway::class); + return Omnipay::create(SubscriptionGateway::class); + } - $response = $gateway->purchase([ + public function purchase(): ResponseInterface + { + $response = $this->getGateway()->purchase([ 'invest' => $this->invest, 'user' => $this->user ])->send(); @@ -78,6 +81,14 @@ public function purchase(): ResponseInterface return $response; } + public function completePurchase(): ResponseInterface + { + /** @var SubscriptionGateway */ + $gateway = $this->getGateway(); + + return $gateway->completePurchase(['success' => true]); + } + public function refundable(): bool { return false; diff --git a/src/Omnipay/Stripe/Subscription/Gateway.php b/src/Omnipay/Stripe/Subscription/Gateway.php index 8131c87ba3..fb7a491e47 100644 --- a/src/Omnipay/Stripe/Subscription/Gateway.php +++ b/src/Omnipay/Stripe/Subscription/Gateway.php @@ -5,6 +5,7 @@ use Omnipay\Common\AbstractGateway; use Omnipay\Common\Http\ClientInterface; use Omnipay\Stripe\Subscription\Message\SubscriptionRequest; +use Omnipay\Stripe\Subscription\Message\SubscriptionResponse; use Symfony\Component\HttpFoundation\Request as HttpRequest; class Gateway extends AbstractGateway @@ -29,4 +30,11 @@ public function purchase($options = array()) { return new SubscriptionRequest($options); } + + public function completePurchase($options = array()): SubscriptionResponse + { + $request = new SubscriptionRequest($options); + + return $request->completePurchase($options); + } } diff --git a/src/Omnipay/Stripe/Subscription/Message/SubscriptionRequest.php b/src/Omnipay/Stripe/Subscription/Message/SubscriptionRequest.php index 8872031137..3a9812656b 100644 --- a/src/Omnipay/Stripe/Subscription/Message/SubscriptionRequest.php +++ b/src/Omnipay/Stripe/Subscription/Message/SubscriptionRequest.php @@ -41,8 +41,8 @@ public function sendData($data) $session = $this->stripe->checkout->sessions->create([ 'customer' => $this->getStripeCustomer($data['user'])->id, - 'success_url' => $this->getRedirectUrl('/dashboard/subscriptions'), - 'cancel_url' => $this->getRedirectUrl('/project/', $data['invest']->getProject()->id), + 'success_url' => $this->getRedirectUrl('invest', $data['invest']->getProject()->id, $data['invest']->id, 'complete'), + 'cancel_url' => $this->getRedirectUrl('project', $data['invest']->getProject()->id), 'mode' => 'subscription', 'line_items' => [ [ @@ -55,13 +55,18 @@ public function sendData($data) return new SubscriptionResponse($this, $session); } + public function completePurchase(array $options = []) + { + return new SubscriptionResponse($this, $options); + } + private function getRedirectUrl(...$args): string { return sprintf( - '%s://%s%s', + '%s://%s/%s', isset($_SERVER['HTTPS']) ? 'https' : 'http', $_SERVER['HTTP_HOST'], - implode('', $args) + implode('/', $args) ); } diff --git a/src/Omnipay/Stripe/Subscription/Message/SubscriptionResponse.php b/src/Omnipay/Stripe/Subscription/Message/SubscriptionResponse.php index 309e336399..f479e5c277 100644 --- a/src/Omnipay/Stripe/Subscription/Message/SubscriptionResponse.php +++ b/src/Omnipay/Stripe/Subscription/Message/SubscriptionResponse.php @@ -9,7 +9,7 @@ class SubscriptionResponse extends AbstractResponse implements RedirectResponseI { public function isSuccessful() { - return false; + return $this->data['success']; } public function isRedirect()