#snacksnob
1 messages · Page 1 of 1 (latest)
I can leave this open in case other users want to chime in, but this doesn't seem like its Stripe-specific, correct?
ie, you're looking for a general implementation example in your chosen framework, separate from any parts that interact with the Stripe SDK
What do you mean? line items takes adjustable quantities. So, how would I increment that in the cart and then pass it to line_items? On Stripes checkout page you can adjust the quantities.
Ah if that's what you're asking, you can set the adjustable_quantity options per line item when you create the session:
https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-line_items-adjustable_quantity
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
I am following this https://blog.miguelgrinberg.com/post/accept-credit-card-payments-in-flask-with-stripe-checkout
Can you give me any help in how to divide this code into a shopping cart and checkout
def order(product_id):
if product_id not in products:
abort(404)
checkout_session = stripe.checkout.Session.create(
line_items=[
{
'price_data': {
'product_data': {
'name': products[product_id]['name'],
},
'unit_amount': products[product_id]['price'],
'currency': 'usd',
},
'quantity': 1,
},
],
payment_method_types=['card'],
mode='payment',
success_url=request.host_url + 'order/success',
cancel_url=request.host_url + 'order/cancel',
)
return redirect(checkout_session.url)```
My shopping cart contains everything in line_items. Although if the product has been selected twice, it is in the list twice.
I can't really advise on that pat of your integration. You may want to deduplicate those entries and adjust your quantity to 2, for example. For help with that example in particular I'd reach out to the author.
Could the checkout session be called on that list?
What list do you mnea?
@app.route('/cart/<product_id>', methods=['POST'])
def add_to_cart(product_id):
if product_id not in products:
abort(404)
else:
global shopping_cart
shopping_cart+=[
{
'price_data': {
'product_data': {
'name': products[product_id]['name'],
},
'unit_amount': products[product_id]['price'],
'currency': 'usd',
},
'quantity': 1,
},
],
#caclulate total cost
# for items in shopping_cart
return render_template('cart.html', title='Shopping Cart', shopping_cart=shopping_cart, products=products,)```
shopping_cart
OK, and have you tried passing that in to a checkout session create request?
This is where I am stuck because I am not sure how to pass the information into the next function / route and get into the line_items variable.
If you don't know I can try emailing the author.
Take a look at the example here: https://stripe.com/docs/checkout/quickstart?lang=python
It uses flask too
You'll need to do something like line_items=shopping_cart in a call to stripe.checkout.Session.create
Line items is a list of dictionaries?
Yes, a list of objects with a price (or price data), quantity etc
Ok. thanks for your help.