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

cib88's avatar
Level 2

Cashier one off charge & create user in stripe

Hello all,

I currently have a project which accepts donations these are charged as a one-off payment using cashier. Everything works fine however in Stripe the payments are marked as a guest and I need to create a user. Is it possible to create a user in stripe while making the payment? I've checked the docs and found createOrGetStripeCustomer but not sure this is what I need or how I would use this with the charge method.

I don't need to create a user in my app just in stripe, not sure if that effects anything?

Here's what I currently have in my payment controller

public function processPayment(Request $request)
    {
        $paymentMethod = $request->get('paymentMethod');
        $email = $request->get('cardholder-email');
        $paymentAmount = $request->get('amount') * 100;

        try {
            $stripeCharge = (new User)->charge($paymentAmount, $paymentMethod, [
                'currency' => 'GBP',
                'receipt_email' => $email
            ]);
            return redirect()->route('thankyou');
        } catch (IncompletePayment $exception) {
            return redirect()->route(
                'cashier.payment',
                [$exception->payment->id, 'redirect' => route('payment')],
            );
        } catch (Exception $e) {
            return Redirect::back()->withErrors(['error' => $e->getMessage()]);
        }
    }
0 likes
3 replies
tisuchi's avatar

@cib88 You can do it this way. Give it a try:

public function processPayment(Request $request)
{
    $paymentMethod = $request->get('paymentMethod');
    $email = $request->get('cardholder-email');
    $paymentAmount = $request->get('amount') * 100;

    try {
        // Create a new Stripe customer object
        $customer = (new User)->createOrGetStripeCustomer($email, $paymentMethod);

        // Charge the customer
        $stripeCharge = $customer->charge($paymentAmount, $paymentMethod, [
            'currency' => 'GBP',
            'receipt_email' => $email
        ]);
        return redirect()->route('thankyou');
    } catch (IncompletePayment $exception) {
        return redirect()->route(
            'cashier.payment',
            [$exception->payment->id, 'redirect' => route('payment')],
        );
    } catch (Exception $e) {
        return Redirect::back()->withErrors(['error' => $e->getMessage()]);
    }
}

Keep in mind that the createOrGetStripeCustomer method will check if a customer with the provided email address already exists in Stripe, and if so, it will return the existing customer object. If no customer with the provided email address exists, it will create a new customer object. This can be useful if you want to make sure you don't create multiple customer objects for the same user.

Check more: https://laravel.com/docs/9.x/billing#creating-customers

cib88's avatar
Level 2

@tisuchi does this mean I will need to create the user in my Laravel app too?

Please or to participate in this conversation.