如何在 Stripe 内循环

Posted

技术标签:

【中文标题】如何在 Stripe 内循环【英文标题】:How to loop inside Stripe 【发布时间】:2021-12-06 17:58:51 【问题描述】:

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

【问题讨论】:

您能否提供有关该问题的更多详细信息? 【参考方案1】:

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

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

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

【讨论】:

以上是关于如何在 Stripe 内循环的主要内容,如果未能解决你的问题,请参考以下文章