May 30, 2022
0
Level 3
Laravel Cashier - New Payment Element - Single Charge
Hi, I am really stuck in this. Andre showed in his video how to use the new Stripe Element with Cashier. I was able to create this with no problem. Now i tried to make this work for single charges (no subscriptions) but i really don't know how to make it work. here is my submit function:
async function handleSubmit(e) {
e.preventDefault();
//setLoading(true);
const { setupIntent, error } = await stripe.confirmSetup({
elements,
confirmParams: {
return_url: "http://localhost:4242/public/checkout.html",
},
redirect: 'if_required'
});
if (error) {
if (error.type === "card_error" || error.type === "validation_error") {
showMessage(error.message);
} else {
showMessage("An unexpected error occured.");
}
} else {
console.log(setupIntent);
let form = document.getElementById('payment-form');
let hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'paymentMethod');
hiddenInput.setAttribute('value', setupIntent.payment_method);
form.appendChild(hiddenInput);
form.submit();
}
//setLoading(false);
}
And my post request:
$stripeCharge = auth()->user()->charge(
100, $request->paymentMethod, [
'description' => 'Test Guthaben',
'customer' => auth()->user()->stripe_id
]
);
Note: a single charge will only be possible when a user has a subscription, so he already got a stripe_id
As soon the form get submited Laravel/Stripe sends me this error: The provided PaymentMethod cannot be attached. To reuse a PaymentMethod, you must attach it to a Customer first.
Please or to participate in this conversation.