charlie2
charlie2

Reputation: 180

Pass product id in stripe checkout session

I have created the checkout session as below:

checkout_session = stripe.checkout.Session.create(
            success_url=domain_url + 'success?session_id={CHECKOUT_SESSION_ID}',
            cancel_url=request.session['latestURL'],
            payment_method_types=['card'],
            mode='payment',
            line_items=[
                {
                    'name': 'Awesome Product',
                    'quantity': 1,
                    'currency': 'usd',
                    'amount': price,
                }
            ]
        )
        return JsonResponse({'sessionId': checkout_session['id']})
    except Exception as e:
        return JsonResponse({'error': str(e)})

I have also created a product using stripe dashboard and have the product id. Now how can I pass that product id in the above checkout session so that my product image and price info will be displayed in checkout page. In the above I have pass product name price manually. I want to directly link it with the product I have created from stripe dashboard. How can I do this?

Upvotes: 4

Views: 5716

Answers (3)

Dorian
Dorian

Reputation: 9085

You can also fetch the prices of your products:

Stripe::Price.list

And pass those prices directly

Upvotes: 0

The Bisto Kid
The Bisto Kid

Reputation: 47

They have a field called 'product' => prod_ifxgshHhhhhlrt which is part of the line_items fields when you call Session::create.

so grab the product id from your dashboard and plug into the field value.

Upvotes: 1

Keith Turkowski
Keith Turkowski

Reputation: 811

When you set the price for a product it will create a 'price id', you can pass the price id which will be associated with a specific product, when the checkout.session.completed event fires, it will have an id that you can use to retrieve the lineItems which will then have a list of the associated products purchased (linked via their price ids).

https://stripe.com/docs/payments/accept-a-payment#create-product-prices-upfront

Example:

line_items=[{'price': 'price_abcdefghi123456789', 'quantity': 1}]

You can kind of use a product id like this:

line_items=[{'price_data': {'currency': 'USD', 'product': 'prod_abcdef123456', 'unit_amount': 500}, 'quantity': 1}]

But it seems pointless (and potentially insecure) since you must supply a price in the unit_amount field.

You could potentially add the product to a plan, and use the plan_id in some way, but that seems more complicated than just using the price id (which is probably the best solution).

The API reference for line items used in checkout sessions is here: https://stripe.com/docs/api/checkout/sessions/create

Upvotes: 2

Related Questions