【问题标题】:How to loop inside Stripe如何在 Stripe 内循环
【发布时间】:2024-01-06 02:33:01
【问题描述】:

我有一个问题,我想在 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,
                        }],

【问题讨论】:

  • 您能否提供有关该问题的更多详细信息?

标签: python django stripe-payments


【解决方案1】:

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

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

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

【讨论】: