I have 2 routes, one for unsubscribing and one for restoring, both routes are the same except for the name/function/url unless I'm missing something. When I cancel the resume subscription, I click the /resume url, it performs the function and then comes back, everything is so fast and never seems to leave the page, then it flashes the success message.
My /cancel url just goes to a blank page (I'm pretty sure this is correct because if it was working properly you would never see it) and it performs the cancel function but never returns. When I go back manually using the back button it flashes a success message. Just don't understand why it doesn't return on its own as expected. Apart from that, if you need any other information, please let me know.
Work:
Route::get('/resume', function (Request $request) {
// $user = User::find(1);
$user = Auth::user();
$response = $user->subscription()->resume();
toastr()->success('Your subscription has been successfully resumed.', 'Success' , ['closeButton' => 'true','positionClass' => 'toast-top-center']);
return back();
});
Not working:
Route::get('/cancel', function (Request $request) {
// $user = User::find(1);
$user = Auth::user();
$response = $user->subscription()->cancel();
toastr()->success('Your subscription has been successfully cancelled.', 'Success' , ['closeButton' => 'true','positionClass' => 'toast-top-center']);
return back();
});
I don't think these are necessary, but just in case, here are the 2 functions in my subscription controller.
public function cancel(): self
{
$response = LemonSqueezy::api('DELETE', "subscriptions/{$this->lemon_squeezy_id}");
$this->sync($response['data']['attributes']);
return $this;
}
/**
* Resume the subscription.
*/
public function resume(): self
{
if ($this->expired()) {
throw new LogicException('Cannot resume an expired subscription.');
}
$response = LemonSqueezy::api('PATCH', "subscriptions/{$this->lemon_squeezy_id}", [
'data' => [
'type' => 'subscriptions',
'id' => $this->lemon_squeezy_id,
'attributes' => [
'cancelled' => false,
],
],
]);
$this->sync($response['data']['attributes']);
return $this;
}
Resolved by commenting on this thread https://laracasts.com/discuss/channels/laravel/redirect-with-not-working?page=1&replyId=337923
I did a manual redirect but that didn't work either so I added ->send(); to the end as this thread suggested and that solved it. I didn't test this with return back(); but it might work too.
The successful code is as follows
return redirect()->to('/settings/subscription')->send();