Did you find out how to use the setup_intent and the setup_intent_client_secret? I also can't find information about it. Very annoying.
How to handle subscriptions with sepa_debit and trial with laravel cashier
So I need to include a payment method and i'm struggling to find a way to include subscriptions with trial and sepa_debit with laravel cashier. I was kinda able to do it with stripe directly but without a trial, but i won't get it how to do it with cashier and stripe elements.
I currently have 3 endpoints:
/** Show the available prices and create or get the stripe customer */
public function showPrices()
{
$customer = \Auth::user()->createOrGetStripeCustomer();
$prices = $this->stripe->prices->all();
return view('central.payment.start', compact('customer', 'prices', 'intent'));
}
/** After click on the price, the user is redirectet to this endpoint */
public function startSubscription()
{
$intent = \Auth::user()->createSetupIntent([
'payment_method_types' => ['sepa_debit', 'card'],
]);
/* $subscription = \Auth::user()->newSubscription('test', request()->priceId)
->trialDays(5)
;*/
/* $subscription = $this->stripe->subscriptions->create([
'customer' => \request()->customerId,
'items' => [[
'price' => request()->priceId
]],
# 'trial_period_days' => '10',
'payment_behavior' => 'allow_incomplete',
'expand' => ['latest_invoice.payment_intent', 'pending_setup_intent']
]);
*/
$subscription = null;
$clientSecret = $intent->client_secret; //$subscription->pending_setup_intent->client_secret;
return view('central.payment.subscription', compact('subscription', 'intent', 'clientSecret'));
}
/* Here i receive a stripe key starting with 'seti_..... '*/
public function subscribed(Request $request)
{
dd($request->all());
}
I use this code to show the elements form
<form id="payment-form" >
<div id="payment-element">
<!-- Elements will create form elements here -->
</div>
<button id="submit">Subscribe</button>
<div id="error-message">
<!-- Display error message to your customers here -->
</div>
</form>
<script>
const stripe = Stripe('{{ config('cashier.key') }}');
const options = {
clientSecret: '{{ $clientSecret }}',
// Fully customizable with appearance API.
appearance: {/*...*/},
};
// Set up Stripe.js and Elements to use in checkout form, passing the client secret obtained in step 5
const elements = stripe.elements(options);
// Create and mount the Payment Element
const paymentElement = elements.create('payment');
paymentElement.mount('#payment-element');
const form = document.getElementById('payment-form');
form.addEventListener('submit', async (event) => {
event.preventDefault();
console.log(elements);
const { error} = await stripe.confirmSetup({
//`Elements` instance that was used to create the Payment Element
elements,
confirmParams: {
return_url: '{{ route('payment.status.returnUrl') }}',
}
});
console.log(error)
if (error) {
// This point will only be reached if there is an immediate error when
// confirming the payment. Show error to your customer (e.g., payment
// details incomplete)
const messageContainer = document.querySelector('#error-message');
messageContainer.textContent = error.message;
} else {
// Your customer will be redirected to your `return_url`. For some payment
// methods like iDEAL, your customer will be redirected to an intermediate
// site first to authorize the payment, then redirected to the `return_url`.
}
});
</script>
So my flow currently is:
-> Create or retrieve the current customer
-> Get all available prices
-> User clicks on price -> Form submits
-> A setup intent is created with sepa_debit and card as payment subscription options
-> Show the Stripe elements form
-> I type in the stripe test card number
-> Gets redirected to the returnUrl which in my case calls subscribed. But here I receive two tokens:
A setup_intent and a setup_intent_client_secret
What am I supposed to do with those? Where should I create the subscription? After a week of trial and error am confused as hell and I have no clue what I'm doing wrong. Every tutorial I find only includes card payments.
Thanks for any help and hints in advance!
Please or to participate in this conversation.