Create JSON from a list (Stripe API)

Hi everyone,

I implemented the Stripe API via the API connector to create a checkout session.

For this endpoint you need to pass in line_items as a querystring that looks something like this:

line_items[0][price_data][currency]=usd&line_items[0][price_data][product_data][name]=“Test Product Name”&line_items[0][price_data][unit_amount]=200&&line_items[0]quantity=1&line_items[0][price_data][product_data][images]=https://www.testproductlink.com/testimage.jpg

I am able to do this for one item, but how would you generate this string for a list of items.

I tried to do it with :format as text - the issue here is that I can’t get the current items index number that needs to be passed into line_items[#] for each item.

Also the list in bubble starts with #1, I’m not sure if you can skip the #0 for line_items in Stripe API?

The cart is basically a list of cart items. and each item holds the name, price, image link etc.

This should be a pretty easy task to do in bubble :smiley:

1 Like

Just an Update, I solved this with some Javascript that would generate the appropriate string from a list of cart items. So no further help needed!

1 Like

Hey @nemanja.rilakovic ,
I’m having the same issue! Could you share a bit more details about your solution?
Thanks!

You could create it as JSON using format as text (read on the forum, there’s a lot of example) and convert it using this free plugin:

@Jici Thanks for the quick suggestion!
I like this approach, how would you set this up?
Thanks!

in api connector, you will set the full part after ? dynamic like ?[urlparams]
Set arbitrary text and start your json: {“line_items”:[From your DB, you will get the list of items:format as json]
In the first box, you will set the payload for line in json (something like:

{
    "price_data": {
        "currency": "This item's currency(dynamic)",
        "product_data": {
            "name": "This product 's name(dynamic)"
        }
    }
}

At the end, you will get:

{
    "line_items": [
        {
            "price_data": {
                "currency": "This item's currency(dynamic)",
                "product_data": {
                    "name": "This product 's name(dynamic)"
                }
            }
        },
        {
            "price_data": {
                "currency": "This item's currency(dynamic)",
                "product_data": {
                    "name": "This product 's name2(dynamic)"
                }
            }
        }
    ]
}

This is what you send to the plugin
This will return:
line_items[0][price_data][currency]=This item's currency(dynamic)&line_items[0][price_data][product_data][name]=This product 's name(dynamic)&line_items[1][price_data][currency]=This item's currency(dynamic)&line_items[1][price_data][product_data][name]=This product 's name2(dynamic)

Advantage of the plugin is this will add the numbering index, something not possible actually with Bubble native function on list.

(I’ve used the example provided by the first message)

My approach is to generate this items list using the toolbox plugin. When the “buy” button is pressed the workflow run the 'Run Javascript" action, and that action generates the list of line items from the cart items.

I can provide you with the code i’m using later if you need it?

One thought for this is, does anyone know if javascript run this way with the toolbox or other plugins would be accessable via the front-end somehow?

Can anyone change that and “hack” the store, providing weird prices and discounts to the stripe checkout session?