From 4f8cb037e91d09dc1f534eb03c42cc804104bc37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Subiabre=20Garc=C3=ADa?= Date: Wed, 10 Jul 2024 13:31:07 +0200 Subject: [PATCH] [FIX] Product references on Stripe for wallet payments (#614) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix use of project variable in Paypal payment method * WIP: refactor use of project in stripe metadata * change product description in stripe payment when there's no project * Left pad transaction ids with 0 * Fix references to project * Fix rediretions --------- Co-authored-by: David Igón --- .../Message/SubscriptionRequest.php | 62 ++++++++++++++----- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/src/Omnipay/Stripe/Subscription/Message/SubscriptionRequest.php b/src/Omnipay/Stripe/Subscription/Message/SubscriptionRequest.php index 622ff50e25..a4c1cefaba 100644 --- a/src/Omnipay/Stripe/Subscription/Message/SubscriptionRequest.php +++ b/src/Omnipay/Stripe/Subscription/Message/SubscriptionRequest.php @@ -41,23 +41,31 @@ public function sendData($data) $customer = $this->getStripeCustomer($user)->id; $metadata = $this->getMetadata($invest); - $successUrl = sprintf('%s?session_id={CHECKOUT_SESSION_ID}', $this->getRedirectUrl( - 'invest', - $metadata['project'], - $invest->id, - 'complete' - )); + $successUrl = $this->getRedirectUrl('pool', $invest->id, 'complete'); + if ($invest->getProject()) { + $successUrl = $this->getRedirectUrl( + 'invest', + $metadata['project'], + $invest->id, + 'complete' + ); + } + + $redirectUrl = $this->getRedirectUrl('dashboard', 'wallet'); + if ($invest->getProject()) { + $redirectUrl = $this->getRedirectUrl('project', $metadata['project']); + } $checkout = $this->stripe->checkout->sessions->create([ 'customer' => $customer, - 'success_url' => $successUrl, - 'cancel_url' => $this->getRedirectUrl('project', $project->id), + 'success_url' => sprintf('%s?session_id={CHECKOUT_SESSION_ID}', $successUrl), + 'cancel_url' => $redirectUrl, 'mode' => CheckoutSession::MODE_SUBSCRIPTION, 'line_items' => [ [ 'price' => $this->stripe->prices->create([ 'unit_amount' => $invest->amount * 100, - 'currency' => $project->currency, + 'currency' => $this->getStripeCurrency($invest, $user), 'recurring' => ['interval' => 'month'], 'product' => $this->getStripeProduct($invest)->id, 'metadata' => $metadata @@ -94,15 +102,25 @@ public function completePurchase(array $options = []) return new SubscriptionResponse($this, $checkout, $subscription); } - $donationCheckout = $this->stripe->checkout->sessions->create([ - 'customer' => $this->getStripeCustomer(User::get($metadata['user']))->id, - 'success_url' => sprintf('%s?session_id={CHECKOUT_SESSION_ID}', $this->getRedirectUrl( + $successUrl = $this->getRedirectUrl('pool', $metadata['invest']); + if ($metadata['project'] !== '') { + $successUrl = $this->getRedirectUrl( 'invest', $metadata['project'], $metadata['invest'], 'complete' - )), - 'cancel_url' => $this->getRedirectUrl('project', $metadata['project']->id), + ); + } + + $cancelUrl = $this->getRedirectUrl('dashboard', 'wallet'); + if ($metadata['project'] !== '') { + $cancelUrl = $this->getRedirectUrl('project', $metadata['project']); + } + + $donationCheckout = $this->stripe->checkout->sessions->create([ + 'customer' => $this->getStripeCustomer(User::get($metadata['user']))->id, + 'success_url' => sprintf('%s?session_id={CHECKOUT_SESSION_ID}', $successUrl), + 'cancel_url' => $cancelUrl, 'mode' => CheckoutSession::MODE_PAYMENT, 'line_items' => [ [ @@ -237,4 +255,20 @@ private function getProductWithoutProjectId(Invest $invest): string $user->id ); } + + private function getStripeCurrency(Invest $invest, User $user): string + { + if ($project = $invest->getProject()) { + return $project->currency; + } + + /** @var stdClass */ + $preferences = User::getPreferences($user); + + if (\property_exists($preferences, 'currency')) { + return $preferences->currency; + } + + return Config::get('currency'); + } }