在Django中使用Stripe进行分账支付

0 投票
1 回答
38 浏览
提问于 2025-04-14 16:56

我正在做一个项目,买家会进行支付,这笔支付需要分成以下几部分:

  • 申请费用
  • 卖家付款
  • 推荐人付款

这是我的代码:


        if buyer_address.state.lower() == 'alabama' or buyer_address.state.lower() == 'al':
            taxes = int(round(float(price * 0.08)))
        shipping_charges = courier.price
        total_charges = price + taxes
        # if referrals:
        admin_amount = int((total_charges - shipping_charges) * 0.2)
        seller_amount = total_charges - admin_amount
        referral_amount = int(admin_amount * 0.02)
        payment_info = {
            "amount": total_charges,  # Amount in cents
            "currency": "usd",
            "connected_accounts": [
                {"account_id": 'acct_1OoODQIbLDChIvG2', "amount": seller_amount},
                {"account_id": "acct_1OrzPRI5g3KKKWKv", "amount": referral_amount},
            ]
        }

        stripe.api_key = "sk_test_EI5Kddsg2MCELde0vbX2cDGw"
        try:
            for account_info in payment_info["connected_accounts"]:
                # Set up transfer amount and destination separately
                transfer_amount = account_info["amount"]
                destination_account = account_info["account_id"]
                print("=======================================================", destination_account)
                
                session = stripe.checkout.Session.create(
                    success_url=my_domain + '/payment_success/' + str(auction_id) + '/' + courier_id + "?mode=" + mode,
                    cancel_url=my_domain + '/payment_failed/' + str(auction_id) + '/' + courier_id + "?mode=" + mode,
                    payment_method_types=['card'],
                    mode = 'payment',
                   line_items=[{
                        'price_data': {
                            'currency': 'usd',
                            'product_data': {
                                'name': 'NailSent Nails',
                            },
                            'unit_amount': int(total_charges),  # amount should be in cents
                        },
                        'quantity': 1,
                    }],
                    payment_intent_data={
                        'transfer_data': {
                            'destination': destination_account,
                            'amount': transfer_amount,  # amount should be in cents
                        }
                    },
                )
        except stripe.error.StripeError as e:
            return HttpResponse(f"Error: {e}")

我试着把这笔支付分给这三个人。

  • 申请费用
  • 卖家费用
  • 推荐费用

我尝试使用支付转账的方法,但出现了资金不足的错误。

现在,如果我进行支付,推荐人的金额会转到推荐人账户,但卖家却没有收到任何钱。

请提供一个好的解决方案。谢谢!

1 个回答

1

你可以使用分开收费和转账的方式,把付款分给多个关联账户。注意这里提到的transfer_group 属性,它可以让你指定转账的来源收费。这样做可以帮助你避免资金不足的错误。也就是说,只有当原始收费成功后,转账才会执行。

撰写回答