Ingap.dev - A Personal Dev Blog

Dress for the job you want, not the job you have.

Published on Thursday 9 March 2023

Tags: stripe1 nodejs1 typescript2 api1 reactjs4

How to pass custom parameters to Stripe payment links?

An easy way to pass user defined parameters to Stripe payment links without using hooks and fetch them later.


A standard way to let our application know that payments succeeded throgh Stripe is using Hooks, i.e. backend endpoints called after the checkout-session. The reason why it could be preferred is to reduce the latency between the payment and our information update in our backend.

Still, if for some reason you don't want Hooks, I'm going to describe you another much easier way to handle this use case.

Passing custom parameters

Passing parameters through Stripe payment links is as easy as appending them in the url.

Original payment link:

https://buy.stripe.com/1k646jhgOmfdcxQ2218

Adding paramenter "client_reference_id":

https://buy.stripe.com/1k646jhgOmfdcxQ2218?client_reference_id=123

Fetching custom parameters

As soon as the user complete the checkout session, an Event will be logged on Stripe servers, also containing our previously passed parameter.

Before executing the code, install stripe for nodejs:

npm install stripe --save
# or
yarn add stripe

Now you'll be able to fetch, e.g. the last 3 stripe events of type checkout.session.completed not older than 2 days which succeeded, and filter such results by matchin any client_reference_id equal to "123".

export async function checkUserSubscription(){
    const stripe = require('stripe')(<YOUR_STRIPE_SECRET_API_KEY>);
    const daysAgo = 2;

    const events = await stripe.events.list({
        limit: 3,
        type: "checkout.session.completed",
        delivery_success: true,
        created: {gt: Math.floor(Date.now()/1000-daysAgo*24*3600)}
    });
    
    const results = events.data.filter((obj:any) => obj.data.object.client_reference_id == "123");

    if(results.length == 0){ return false; }
    return true;
}

The function will return false, if the user subscription was not found under our search criteria, otherwise true.