Skip to content

Commit

Permalink
Fix plan switching
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyiliev committed Mar 26, 2024
1 parent 341db52 commit e631e31
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 6 additions & 6 deletions wave/src/Http/Controllers/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ private function cancelSubscription(){
return redirect('/login')->with(['message' => 'Please log in to continue.', 'message_type' => 'danger']);
}

// Auth user get subscription id
$subscription_id = auth()->user()->subscription->subscription_id;
// Auth user get latest subscription id
$subscription_id = auth()->user()->latestSubscription->subscription_id;

// Ensure the provided subscription ID matches the user's subscription ID
$localSubscription = PaddleSubscription::where('subscription_id', $subscription_id)->first();

if (!$localSubscription || auth()->user()->subscription->subscription_id != $subscription_id) {
if (!$localSubscription || auth()->user()->latestSubscription->subscription_id != $subscription_id) {
return back()->with(['message' => 'Invalid subscription ID.', 'message_type' => 'danger']);
}

Expand Down Expand Up @@ -103,7 +103,7 @@ public function checkout(Request $request){
break;
}
}

sleep($initialDelay * (2 ** $i));
}

Expand Down Expand Up @@ -200,7 +200,7 @@ public function switchPlans(Request $request)
if (isset($plan->id)) {
// Update the user plan with Paddle
$response = Http::withToken($this->vendor_auth_code)->patch(
$this->paddle_url . '/subscriptions/' . (string)$request->user()->subscription->subscription_id,
$this->paddle_url . '/subscriptions/' . (string)$request->user()->latestSubscription->subscription_id,
[
'items' => [
[
Expand All @@ -210,7 +210,7 @@ public function switchPlans(Request $request)
],
'proration_billing_mode' => 'prorated_immediately'
]
);
);

if ($response->successful()) {
$body = $response->json();
Expand Down
6 changes: 6 additions & 0 deletions wave/src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ public function subscription()
}


public function latestSubscription()
{
return $this->hasOne(PaddleSubscription::class)->latest();
}


/**
* @return bool
*/
Expand Down

0 comments on commit e631e31

Please sign in to comment.