Skip to content

Commit

Permalink
[FIX] Product references on Stripe for wallet payments (#614)
Browse files Browse the repository at this point in the history
* 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 <davidbenabarre@platoniq.net>
  • Loading branch information
subiabre and davidbeig committed Jul 10, 2024
1 parent e7e9f1b commit 4f8cb03
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions src/Omnipay/Stripe/Subscription/Message/SubscriptionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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' => [
[
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 4f8cb03

Please sign in to comment.