Absolutely love Laracasts - only been 6 weeks in and already set up a lot of what I wanted. I'm stuck, so badly stuck, on setting up Subscriptions with Stripe. It looks like some of the tutorial is outdated, or at least, there might be better practices by now.
I'm not getting a 500 error when trying to post with Axios (instead of the $http.post suggested in the video)
I've tried setting up the CSRF token any which way I can, then I find out it's supposed to be all built in - but then I'm not sure where to go. Here is my version of the CheckoutForm.vue:
<template>
<form action="/subscriptions" method="POST">
<input type="hidden" name="stripeToken" v-model="stripeToken">
<input type="hidden" name="stripeEmail" v-model="stripeEmail">
<select name="plan" v-model="plan">
<option v-for="plan in plans" :value="plan.id">
{{ plan.name }} — ${{ plan.price / 100 }}
</option>
</select>
<button id="stripeButton" class="btn btn-lg btn-block btn-primary" @click.prevent="subscribe">Get Started</button>
</form>
</template>
<script>
export default {
props: ['plans'],
data() {
return {
stripeEmail: '',
stripeToken: '',
plan: 1,
};
},
created() {
this.stripe = StripeCheckout.configure({
key: Narrative.stripeKey,
image: "https://s3.amazonaws.com/assets.narrativefirst.com/images/nflogo-brand.png",
locale: "auto",
panelLabel: "Subscribe For",
token:(token) => {
this.stripeToken = token.id;
this.stripeEmail = token.email;
axios.post('/subscriptions', this.data)
.then(
response => alert('Complete! Thanks for your payment.'));
}
});
},
methods: {
subscribe() {
let plan = this.findPlanById(this.plan);
this.stripe.open({
name: "The Book",
description: plan.name,
zipCode: true,
amount: plan.price,
});
},
findPlanById(id) {
return this.plans.find(plan => plan.id == id);
}
}
}
</script>
and then here is the relevant store.blade:
<meta name="csrf-token" content="{{ csrf_token() }}">
<script>
var BookStore = {
csrfToken: "{{ csrf_token() }}",
stripeKey: "{{ config('services.stripe.key') }}"
};
</script>
</head>
<body>
@include('layouts.nav')
<div class="container-fluid">
<div class="row">
<main class="col-sm-12 ml-sm-auto col-md-11 pt-3" role="main">
<div class="col-12">
<div id="app">
<article>
<p>
<checkout-form :plans="{{ $plans }}"></checkout-form>
</p>
</article>
</div>
</div>
</main>
</div>
</div>
also, this is my bootstrap.js which I modified based on other entries here and elsewhere...maybe I messed something up?
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
/**
* Next we will register the CSRF Token as a common header with Axios so that
* all outgoing HTTP requests automatically have it attached. This is just
* a simple convenience so we don't have to attach every token manually.
*/
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
Can anyone please help me figure out what is going on here?