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

jimmyhowedotcom's avatar

Order process with Stripe and Cachier

HI guys,

I thought I'd ask yous guys for the lovely community yous are.

I'm building a portal for my client where the remit is, she wants a portal to be able to log in, add her products and allow customers that walk in to her premise (She got a massage business) and she can enter the services that the custemer needs, but a price and the custemer can enter her details on an iPad or whatever to pay.

I built the portal in Laravel 10, InertiaJS and using the Jetstream Vue scaffolding with teams.

My questions is around Stripe process. I have a "create new order" page where they can select service price and click and I use the stripe checkout integration to let the customer fill their details via that and the order gets processed then it redirects back to a list of their orders. Should I be Syncing the services, and orders with stripe, or is it useful to have the orders and products locally. I'm unsure of how to store the order and when, when she click the button the stripe/klarna takes over, and I can catch the webhook info back, the issue is i'm having was I though I could make a local order flagged as unfulfilled, (in case they cancel), but what I was doing before was firing an event to create the order after the process has been done, however I cant see how to associate the order with the unfulfilled order in the db, is there a way to get the stripe webhook to return any order_id back so I can update the paid field?

I get there are plenty of subscriptions tutorials handled by cashier, but for one off payments I'm trying to figure out the process myself and any thoughts or examples would be good. I can provide any process, code samples if yous need, just looking for some tips.

Thanks, Jimmy

0 likes
2 replies
LaryAI's avatar
Level 58

It is useful to have the orders and products stored locally in addition to syncing them with Stripe. This allows for easier management and tracking of orders and products.

To associate the order with the unfulfilled order in the database, you can use the Stripe webhook to return the order ID and update the paid field. Here's an example of how to do this:

  1. Create a webhook endpoint in your Laravel application to receive the Stripe webhook. You can use the stripe-php library to handle the webhook.
Route::post('/stripe/webhook', function (Request $request) {
    $payload = $request->getContent();
    $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
    $event = null;

    try {
        $event = \Stripe\Webhook::constructEvent(
            $payload, $sig_header, config('services.stripe.webhook_secret')
        );
    } catch (\UnexpectedValueException $e) {
        // Invalid payload
        return response()->json(['error' => $e->getMessage()], 400);
    } catch (\Stripe\Exception\SignatureVerificationException $e) {
        // Invalid signature
        return response()->json(['error' => $e->getMessage()], 400);
    }

    // Handle the event
    switch ($event->type) {
        case 'checkout.session.completed':
            $session = $event->data->object;
            $order_id = $session->metadata->order_id;
            $order = Order::find($order_id);
            $order->paid = true;
            $order->save();
            break;
        // Handle other event types
    }

    return response()->json(['success' => true]);
});
  1. When creating the Stripe checkout session, include the order ID as metadata so that it can be retrieved in the webhook.
$session = \Stripe\Checkout\Session::create([
    'payment_method_types' => ['card'],
    'line_items' => [
        [
            'price_data' => [
                'currency' => 'usd',
                'unit_amount' => 1000,
                'product_data' => [
                    'name' => 'Product Name',
                ],
            ],
            'quantity' => 1,
        ],
    ],
    'mode' => 'payment',
    'success_url' => 'https://example.com/success',
    'cancel_url' => 'https://example.com/cancel',
    'metadata' => [
        'order_id' => $order->id,
    ],
]);
  1. When the user clicks the button to create the order, create a new order in the database with the paid field set to false.
$order = new Order;
$order->customer_id = $customer->id;
$order->service_id = $service->id;
$order->price = $service->price;
$order->paid = false;
$order->save();
  1. When the webhook is received, update the paid field of the corresponding order in the database.
$order_id = $session->metadata->order_id;
$order = Order::find($order_id);
$order->paid = true;
$order->save();
1 like
jimmyhowedotcom's avatar

@LaryAI Yeah that was pretty much how I was thinking with the meta data, that's amazing thanks ... also, if this was an AI response, you have just blown my mind !

Please or to participate in this conversation.