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

patressz's avatar

Laravel - cashier/stripe

Hello guys, i have a website where i use the stripe payment gate. I install laravel/cashier, set up the checkout and also set up the webhooks. My question is, am I doing this correctly.

public function checkout(Request $request)
{
    return $request->user()->checkoutCharge(
        amount: $request->amount,
        name: 'Credit',
        quantity: 1,
        sessionOptions: [
            'success_url' => route('payments.success'),
            'cancel_url' => route('payments.cancel'),
            'expires_at' => now()->addMinutes(30)->timestamp,
        ]);
}

My success() method do nothing, only give success message.

public function success()
{
    return redirect()->route('home')->withStatus('success message...');
}

Failure() method do same.

And i also create and register the StripeEventListener which is fired with webhook. Where i do increment the user's credit in the database.

class StripeEventListener
{
    /**
     * Handle the event.
     */
    public function handle(WebhookReceived $event): void
    {
        if ($event->payload['type'] === 'payment_intent.succeeded') {
            $customer = $event->payload['data']['object']['customer'];
            $amount = $event->payload['data']['object']['amount'];

            Cashier::findBillable($customer)->increment('credit', $amount * 0.01);
        }
    }
}

is there something i'm doing wrong? Thank you for your answer! :)

0 likes
1 reply
LaryAI's avatar
Level 58

The code provided looks correct for setting up the checkout and webhooks with Laravel Cashier and Stripe. However, it's always a good idea to test the checkout process thoroughly to ensure that everything is working as expected.

One thing to note is that the success() and failure() methods could be improved to provide more information to the user about the status of their payment. For example, you could include the amount that was charged and a reference number for the transaction.

Here's an updated version of the success() method that includes this information:

public function success()
{
    $charge = Auth::user()->charges()->latest()->first();

    return redirect()->route('home')->with([
        'status' => 'success',
        'message' => 'Your payment of $' . number_format($charge->amount / 100, 2) . ' was successful. Your reference number is ' . $charge->id . '.',
    ]);
}

This assumes that you have a charges table in your database to store information about each payment.

Overall, your implementation looks good and should work as expected.

Please or to participate in this conversation.