I have a question while using laravel cashier with stripe, can you customize stripes additional fields to a field that is not supplied by stripe. Like if i wanted to customize stripe table and add my own field is that possible?
Just to test i added a integer field called random_id to the stripe subscription migration, when trying to make a subscription i set the default value to 1 i just to see if it would display in the database and it doesn't so i am wondering how can i achieve this using laravel cashier.
paymentcontroller
public function subscribe(Request $request)
{
$user = auth()->user();
$paymentMethod = $request->payment_method;
$planId = $request->plan;
$user->newSubscription('main', $planId)->create($paymentMethod, [
'random_id' => 1,
]);
return response([
'success_url' => redirect()->intended('/')->getTargetUrl(),
'message' => 'success'
]);
}
Payment.blade.php
@section('content')
<select name="plan" class="form-control" id="subscription-plan">
@foreach($plans as $key=>$plan)
<option value="{{$key}}">{{$plan}}</option>
@endforeach
</select>
<input placeholder="Card Holder" class="form-control" id="card-holder-name" type="text">
<!-- Stripe Elements Placeholder -->
<div id="card-element"></div>
<button class="mt-2 btn btn-sm btn-primary" id="card-button" data-secret="{{ $intent->client_secret }}">
Subscribe
</button>
@endsection
@section('js')
<script>
window.addEventListener('load', function() {
const stripe = Stripe('{{env('STRIPE_KEY')}}');
const elements = stripe.elements();
const cardElement = elements.create('card');
cardElement.mount('#card-element');
const cardHolderName = document.getElementById('card-holder-name');
const cardButton = document.getElementById('card-button');
const clientSecret = cardButton.dataset.secret;
const plan = document.getElementById('subscription-plan').value;
cardButton.addEventListener('click', async (e) => {
const { setupIntent, error } = await stripe.handleCardSetup(
clientSecret, cardElement, {
payment_method_data: {
billing_details: { name: cardHolderName.value }
}
}
);
if (error) {
// Display "error.message" to the user...
} else {
// The card has been verified successfully...
console.log('handling success', setupIntent.payment_method);
axios.post('/subscribe',{
payment_method: setupIntent.payment_method,
plan : plan
}).then((data)=>{
location.replace(data.data.success_url)
});
}
});
})
</script>