I have 2 routes, one for canceling subscription, and one for resuming, and unless I am missing something, both routes are identical except for the names/functions/urls. When I cancel resume the subscription, I click the /resume url, it performs the function, and then returns back, and it’s all so fast it seems like it never leaves the page, then it flashes the success message.
My /cancel url just goes to a blank page (I am pretty sure this is correct, because you would never see this if it worked properly) and it performs the cancel function but never returns back. When I manually go back with my back button, it then flashes the success message. Just can’t figure out why it will not go back on it’s own, as intended. Please tell me if you need any other info besides this.
Working:
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 feel like these aren’t 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;
}
2
Answers
Solved with a comment on this thread https://laracasts.com/discuss/channels/laravel/redirect-with-not-working?page=1&replyId=337923
I did a manual redirect which was also not working, so I added ->send(); to the end, as that thread suggested, and this fixed it. I did not test this with return back(); but it probably will work for that too.
successful code looks like this
The
back()
function it is often used lightly, for many reason is better to avoid it, what if the user go directly to that andpoint and try to referesh the page? he will go back to the same page.What if user arrive to that endpoint without passing from the normal application flow? he don’t have a back parameter.
It’s always better avoid the
return back()
cause you never know from where he come from.Another small tip, instead
$user = Auth::user(); $response = $user->subscription()->cancel();
you should can do it, in one line$response= Auth::user()->subscription()->cancel();