Error handling for Cashier Controller methods while building an API
Essentially I have a CheckoutController that handles the functionality for subscribing, unsubscribing, resuming a subscription, and changing a subscription. For example:
public function processCheckout(Request $request)
{
try {
$plan = Plan::findOrFail($request->billing_plan_id);
$user = $user = auth()->user();
//If user has no subscriptions subscribe them to new plan
if($user->subscriptions->count() === 0){
$user->newSubscription($plan->name, $plan->stripe_plan_id)->create($request->payment_method['id']);
return response([], 201);
}
} catch (\Exception $e) {
return response([], 500);
}
}
What I don't understand is what type of errors I should output for the frontend to pick up if something goes wrong. Right now for all of my Checkout methods I just out put a 500 error, and on the front end I catch that error by outputting a message: "An error occurred. Please try again."
submitPaymentForm(paymentMethod){
this.$http({
method: 'POST',
url: 'api/checkout',
data: {
billing_plan_id: this.planId,
payment_method: paymentMethod
}
}).then(function (response) {
this.$store.dispatch('getUser')
}.bind(this))
.catch(error => {
this.serverErrorMessage = "An error occurred. Please try again.";
console.log('submit payment'+error);
});
},
},
Now, I do understand that Stripe implements its own errors on the frontend, but I'm not sure what to do with my backend. I was hoping someone could help me out with this.
Please or to participate in this conversation.