如何在 Stripe 中循环

How to loop inside Stripe

我有一个问题,我想在 Stripe 中循环以在结帐时创建动态多个对象。我无法在 stripe.checkout.Session.create() 之后执行此操作,因为出现错误。此外,我无法在 stripe.checkout.Session.create() 的 for 循环中创建 JSON 对象。有任何想法吗?如何使用 for 循环并创建多个 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,
                        }],

您应该可以根据需要迭代准备line_items然后传递准备好的数组:

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

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