#snacksnob

1 messages · Page 1 of 1 (latest)

hybrid matrixBOT
grizzled bison
#

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

frozen hazel
#

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.

grizzled bison
frozen hazel
#

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)```

In this article I'm going to show you how to implement an order page for your Flask application that you can use to sell products or services online. The solution I'm going to present to you uses…

#

My shopping cart contains everything in line_items. Although if the product has been selected twice, it is in the list twice.

grizzled bison
#

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.

frozen hazel
#

Could the checkout session be called on that list?

grizzled bison
#

What list do you mnea?

frozen hazel
#
@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

grizzled bison
#

OK, and have you tried passing that in to a checkout session create request?

frozen hazel
#

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.

grizzled bison
#

It uses flask too

#

You'll need to do something like line_items=shopping_cart in a call to stripe.checkout.Session.create

frozen hazel
#

Line items is a list of dictionaries?

grizzled bison
#

Yes, a list of objects with a price (or price data), quantity etc

frozen hazel
#

Ok. thanks for your help.