在 Stripe 结帐会话中集成自定义税费和折扣

问题描述 投票:0回答:1

您能帮我将税收和折扣整合到我的 Stripe sission 中吗

这是我当前的代码

class CreateCheckoutSessionOrderView(View):
def get(self, request, *args, **kwargs):
    order_id = self.kwargs["order_id"]
    DOMAIN: str = 'http://127.0.0.1:8000'
    order = Order.objects.get(id=order_id)
    # tax_rates = []
    # for tax in order.tax.all():
    #     tax_rate = stripe.TaxRate.create(
    #         display_name=tax.name,
    #         description=tax.name,
    #         percentage=tax.rate,
    #         jurisdiction="RU",
    #         inclusive=False,
    #     )
    #     tax_rates.append(tax_rate.id)
    #
    # discounts = []
    # for discount in order.discount.all():
    #     discount_amount = stripe.Coupon.create(
    #         amount_off=discount.amount,
    #         duration='once',
    #         currency='usd',
    #         name=discount.name,
    #     )
    #     discounts.append(discount_amount.id)

    session = stripe.checkout.Session.create(
        payment_method_types=['card'],
        line_items=[
            {
                'price_data': {
                    'currency': 'usd',
                    'unit_amount': order.get_total_cost() * 100,
                    'product_data': {
                        'name': order.__str__(),
                    },
                },
                'quantity': 1,
            },

        ],
        payment_intent_data={
            'metadata': {
                'order_id': order.id,
            },
        },
        mode='payment',
        success_url=DOMAIN + '/success/',
        cancel_url=DOMAIN + '/cancel/',
        # tax_rates=tax_rates,
        discounts=[{"discounts": '{{COUPON_ID}}'}],
    )
    return JsonResponse({'id': session.id})

现在获取此回溯

stripe._error.InvalidRequestError: Request req_h7p3k0OElRSyeg: Received unknown parameter: discounts[0][discounts]
[07/Jan/2024 10:40:53] "GET /order_checkout/1 HTTP/1.1" 500 115336

我真的很感谢任何建议和代码审查。我现在正在学习如何处理条纹付款

python django stripe-payments
1个回答
0
投票

我没有使用过Stripe API,但是看看错误,我可以说回调中收到的

discounts[0][discounts]
和你写的
discounts=[{"discounts": '{{COUPON_ID}}'}]
之间存在差异。

我认为您应该将

discounts=[{"discounts": '{{COUPON_ID}}'}]
值更改为
discounts=[array_len][{"discounts": '{{COUPON_ID}}'}]
。我猜我这里加的
array_len
可能是折扣数量

正如我所说,我可能没有提供完整的解决方案,因为我不使用 Stripe API。

© www.soinside.com 2019 - 2024. All rights reserved.