在 Stripe 结账时添加额外金额

我使用 python/Django 开发电子商务网站。我集成了条带支付,其中购物车项目作为行项目动态传递。如果购物车价值低于 500 卢比,我会添加运费。当前使用 stripe session 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)})


收到一只叮咚
浏览 93回答 1
1回答

一只斗牛犬

有几种方法可以解决这个问题,但一种方法是通过仪表板或 API 创建交货价格。然后,如果订单项价格总和unit_amounts小于 500,则将运费添加到订单项并将项目数组传递给会话创建调用:# The shipping price IDshipping_price = "price_xxx"# Base priceprice = stripe.Price.retrieve(&nbsp; "price_yyy",)# If there are more than one item you can iterate over items and sum the unit_amountsitems = [&nbsp; {&nbsp; &nbsp; "price": price.id,&nbsp; &nbsp; "quantity": 1,&nbsp; },]# For simplicity I assume there is only one price being charged, for summing, iterate over the itemsif(price.unit_amount < 500):&nbsp; items.append({&nbsp; &nbsp; "price": shipping_price,&nbsp; &nbsp; "quantity": 1,&nbsp; })stripe.checkout.Session.create(&nbsp; success_url="https://example.com/success",&nbsp; cancel_url="https://example.com/cancel",&nbsp; payment_method_types=["card"],&nbsp; line_items=items,&nbsp; mode="payment",)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python