Stripe API pass custom variables

David Carr

Stripe API Tutorials Nova Framework

When sending charges to Stripe you’ll often want to send additional information such as an order id, the meta option makes this simple.

I’m using Nova Framework not setup with Nova? please read Getting Stripe API setup with Nova Framework

The Meta option accepts an array which is perfect for passing in your own information. Making a charge simply add a metadata option.

$charge = Charge::create([
    'description' => 'item name',
    "amount" => 2000,
    "currency" => "gbp",
    "metadata" => [
        'order_id' => 2,
        'user_id' => 11
    ]
]);

In the above example an order_id and a user_id will be passed to Stripe and then returned from Stripe in their webhooks.

Getting the metadata out of a webhook

The payload that is posted back to your server via webhooks will contain the metadata to get at it simply reference the path to the event, say your payload is called $event

To get the order_id

$event->data->object->metadata->order_id

To get the user_id

$event->data->object->metadata->user_id;

That’s it, very simple but really useful.

Copyright © 2006 - 2024 DC Blog - All rights reserved.