Home > OS >  How to loop inside Stripe
How to loop inside Stripe

Time:10-21

I have a problem I would like to loop inside Stripe to create dynamic multiple objects in checkout. I can not do that after stripe.checkout.Session.create() because I get an error. Also I can not create JSON object in for loop out of stripe.checkout.Session.create(). Any ideas? How can I use for loop and create multiple line_items?

def create_checkout_session(request):
    if request.method == "GET":
        try:
            cart = Cart.objects.get(order_user=request.user)
            checkout_session = stripe.checkout.Session.create(
                payment_method_types=['card', 'p24'], 
                    line_items=[{
                        'price_data': {
                            'currency': 'eur',
                            'product_data': {
                            'name': 'total'
                            },
                            'unit_amount': cart.total,
                        },
                        'quantity': 1,
                        }],

CodePudding user response:

You should be able to iterate to prepare line_items according to your needs then pass the prepared array:

count = 5
lineItems = =[]
for i in range(count):    
    lineItems.append({...})

checkout_session = stripe.checkout.Session.create(
  line_items=**lineItems**,
  ...
)
  • Related