0

I am having issue looping products (displaying more than one product) in stripe checkout in flask. Normally it should work. But when looping, it displays only one product instead of all the products that was looped. I don't know what am doing wrong. The codes are shown below:

@posts.route('/checkout')
def checkout():    
    res=Ct.query.filter_by(username = current_user.username, status='pending').order_by(Ct.id.desc()).all() #course current user intends to buy
    for r in res:
        te = {
              'price_data': {
                'currency': 'usd',
                'unit_amount_decimal':  total,
                'product_data': {
                'name': r.course,
                'images': ['https://i.imgur.com/EHyR2nP.png'],
                },
              },
                'quantity': 1,
            }
        session = stripe.checkout.Session.create(
            payment_method_types=['card'],
            line_items=[te,],
            payment_intent_data={
               'application_fee_amount': fee,
               'transfer_data': {
                  'destination': account_id,
                 },
               },
               metadata={'order_id': '6735'},
            customer_email='a@b.c',
            mode='payment',
            success_url=url_for('posts.processing', _external=True) + '?session_id={CHECKOUT_SESSION_ID}',
            cancel_url=url_for('posts.viewcart', _external=True),

         )
        return jsonify(checkout_session_id=session['id'], checkout_public_key=config.get('STRIPE_PUBLIC_KEY'))

  • Right now you are creating a checkout session while looping through the `res` list. What you likely want to do instead is produce an array of line items and then pass that array to the `line_items` property when creating the session. Try moving the session creation call outside of the for loop, and follow this link here for an example on how to append to an array in a loop in Python: https://stackoverflow.com/questions/41452819/list-append-in-for-loop – ttmarek Sep 18 '20 at 18:16
  • It worked. Thank you very much for the tip. I really appreciate..... – ebonyhumble Sep 19 '20 at 02:44

0 Answers0