Stripe Checkout Session for custom products

As far as payment processors are concerned, Stripe is definitely up there as far as ease of implementation is concerned.
They’ve done an excellent job at providing extensive documentation and code samples, which, makes simple transactions almost a cop…


This content originally appeared on DEV Community and was authored by Dave Jodhan

As far as payment processors are concerned, Stripe is definitely up there as far as ease of implementation is concerned.
They've done an excellent job at providing extensive documentation and code samples, which, makes simple transactions almost a copy/paste activity.

Stripe's quick start guide provides an opportunity to select your front end language, your back end language and you can simply copy/paste the samples.

However, the kicker is that their guide relies on setting up inventory on their platform.
Let's take a look at the checkout_session object, specifically, the 'price' key in the line_items list of price dictionaries :

        checkout_session = stripe.checkout.Session.create(
            line_items=[
                {
                    # Provide the exact Price ID (for example, pr_1234) of the product you want to sell
                    'price': '{{PRICE_ID}}',
                    'quantity': 1,
                },
            ],
            mode='payment',
            success_url=YOUR_DOMAIN + '?success=true',
            cancel_url=YOUR_DOMAIN + '?canceled=true',
        )

A valid PRICE_ID is one defined on Stripe's platform.

What if, for example, you wish to maintain your own inventory, while still leveraging.
Well, it's not immediately obvious reading through the documentation. However, buried in the API docs is an alternative method of defining a product, along with price and description using a price_data dictionary (in place of the price key), which looks something like this.

                "price_data" : {
                    "currency" : "usd",
                    "product_data":{
                        "name": "bead necklace"
                    },
                    "unit_amount": 2000,
                }

A complete checkout session object will then look like this:

checkout_session = stripe.checkout.Session.create(
    line_items=[
    {
        "price_data" : {
            "currency" : "usd",
            "product_data":{
                "name": "bead necklace"
            },
            "unit_amount": 20000,
        },
        "quantity": 1
    }
    ],
    mode="payment",
    success_url="?success=true",
    cancel_url="?canceled=true"
)

A more scalable approach would be to populate the line_items list with values from your own inventory database, if maintaining your own inventory is part of your overall system design.

Happy coding!!


This content originally appeared on DEV Community and was authored by Dave Jodhan


Print Share Comment Cite Upload Translate Updates
APA

Dave Jodhan | Sciencx (2024-10-24T02:27:49+00:00) Stripe Checkout Session for custom products. Retrieved from https://www.scien.cx/2024/10/24/stripe-checkout-session-for-custom-products/

MLA
" » Stripe Checkout Session for custom products." Dave Jodhan | Sciencx - Thursday October 24, 2024, https://www.scien.cx/2024/10/24/stripe-checkout-session-for-custom-products/
HARVARD
Dave Jodhan | Sciencx Thursday October 24, 2024 » Stripe Checkout Session for custom products., viewed ,<https://www.scien.cx/2024/10/24/stripe-checkout-session-for-custom-products/>
VANCOUVER
Dave Jodhan | Sciencx - » Stripe Checkout Session for custom products. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/24/stripe-checkout-session-for-custom-products/
CHICAGO
" » Stripe Checkout Session for custom products." Dave Jodhan | Sciencx - Accessed . https://www.scien.cx/2024/10/24/stripe-checkout-session-for-custom-products/
IEEE
" » Stripe Checkout Session for custom products." Dave Jodhan | Sciencx [Online]. Available: https://www.scien.cx/2024/10/24/stripe-checkout-session-for-custom-products/. [Accessed: ]
rf:citation
» Stripe Checkout Session for custom products | Dave Jodhan | Sciencx | https://www.scien.cx/2024/10/24/stripe-checkout-session-for-custom-products/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.