【问题标题】:Add additional amount to stripe checkout将额外金额添加到条带结帐
【发布时间】:2021-01-29 16:12:41
【问题描述】:

我正在使用 python/Django 开发电子商务网站。我已经集成了条带付款,其中购物车项目作为行项目动态传递。如果购物车价值低于 500 卢比,我添加了运费。当前使用条带会话 Api 进行结帐过程。我想将运费作为额外金额添加到结帐行项目价格中。 提前致谢

@csrf_exempt
def createCheckoutSession(request):


if request.method=='GET':
    domain_url='http://localhost:8000/'
    stripe.api_key= settings.STRIPE_SECRET_KEY

    profile = UserProfile.objects.get(user__id=request.user.id)


    try:
        customer = stripe.Customer.retrieve(profile.stripe_id)
        print (customer)
        profile = UserProfile.objects.get(user__id=request.user.id)
        checkoutSession = stripe.checkout.Session.create(
        success_url =domain_url+'success?session_id={CHECKOUT_SESSION_ID}',
        cancel_url =domain_url+'cancelled/',
        payment_method_types = ['card'],
        mode='payment',
        line_items= get_line_items(request)[0],
        customer= profile.stripe_id,


        )



        return JsonResponse({'sessionId':checkoutSession['id']})


    except Exception as e:
        return JsonResponse({"error":str(e)})

【问题讨论】:

    标签: python django e-commerce stripe-payments


    【解决方案1】:

    有几种方法可以解决此问题,但其中一种方法是通过仪表板或 API 创建交付价格。然后,如果行项目价格的总和 unit_amounts 小于 500,则将运输价格添加到行项目并将项目数组传递给会话创建调用:

    # The shipping price ID
    shipping_price = "price_xxx"
    
    # Base price
    price = stripe.Price.retrieve(
      "price_yyy",
    )
    
    # If there are more than one item you can iterate over items and sum the unit_amounts
    items = [
      {
        "price": price.id,
        "quantity": 1,
      },
    ]
    
    # For simplicity I assume there is only one price being charged, for summing, iterate over the items
    if(price.unit_amount < 500):
      items.append({
        "price": shipping_price,
        "quantity": 1,
      })
    
    stripe.checkout.Session.create(
      success_url="https://example.com/success",
      cancel_url="https://example.com/cancel",
      payment_method_types=["card"],
      line_items=items,
      mode="payment",
    )
    

    【讨论】:

    • 感谢@v3nkman 的想法。如果购物车价值超过 500 。我将运费添加为数量为 1 的订单项
    猜你喜欢
    • 1970-01-01
    • 2021-07-21
    • 2017-01-22
    • 2015-01-18
    • 1970-01-01
    • 2018-04-27
    • 2023-03-30
    • 2011-12-01
    • 1970-01-01
    相关资源
    最近更新 更多