Nova Framework, Tutorials, Stripe API

Stripe API pass custom variables

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.

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.