Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gn90at's avatar

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.

0 likes
0 replies

Please or to participate in this conversation.