Axios POST submits twice creating new subscription with Laravel Cashier and Stripe
Hey guys, I'm really pulling my hair out at the minute. I have a subscription form using Vue 2.0/Laravel 5.4 and axios to post using stripe checkout.
The issue I'm having is when I create a new subscription using laravel cashier in my controller it is duplicating, creating two subscriptions for the user around 5 seconds apart.
I have other similar forms in my app coded the same way (followed Jeffreys stripe course) and they work fine and when charging the customer it only charges once and creates one record in the database.
Apart from some added functionality to validate a coupon code using a get request there is nothing different with this form.
Anyone had any similar issues?
I will post code when I'm at my machine if needed.
@willvincent yes it looks like it is. I will post my code when I get home this evening.
I'm using @click.prevent on my submit form, then in my stripe conifig I'm doing the usual token related things for stripe, submitting via axis.post to the same route my form is and then I call the submit on my form.
I've done some testing and if I remove the form submit it correctly inserts only one subscription to the database but doesn't redirect to completion page - I could redirect here using window.location.replace and this works.
If I take the axios.post out then the form submits fine and correctly inserts only one record.
It may be me being a noob (only been coding 4 months) but it appears that the form post and the axios.post are both submitting to the same url. The thing that is confusing me is my single charge booking forms using the same code but work as expected.
Should I be pointing the form action and the ajax request to the same route, or should my form action point to a route that isn't for my store() method?
It seems the issue was down to my own lack of knowledge as I've now figured it out.
I've used:
if ($request->wantsJson()){
return URL::to('myurl');
}
I had to use $request->wantsJson() instead of $request->ajax() because it just wasn't detecting that the call was made using an ajax request. I think its something to do with the headers, but anyway this workaround does the job.
Then in the response I can set the window.location = response.data;
This allows me to state a redirect if made via a normal request too.