【问题标题】:Python Official Stripe Client Creating Two CustomersPython 官方 Stripe 客户端创建两个客户
【发布时间】:2020-08-02 19:41:40
【问题描述】:

我在下面使用的这段代码运行良好,除了结帐完成时,我注意到每次有人在我的网站上结帐时都会输入两个客户条目。

一个有默认源集,一个没有。我附上了截图。

https://i.ibb.co/8dd0Cxz/Screenshot-from-2020-04-20-11-15-25.png

@login_required
def checkout(request):
    if request.method == 'POST':
        plan = Plan.objects.get(nickname=request.POST['plan'])
        stripe_customer = stripe.Customer.create(
            email=request.user.email, source=request.POST['stripeToken'])

        stripe_subscription = stripe.Subscription.create(customer=stripe_customer.id,
                                                         items=[
                                                             {'plan': plan.id}],
                                                         trial_from_plan=True)
        # Tried removing but no the reason for the issue.
        Subscription.sync_from_stripe_data(
            stripe_subscription
        )
        return redirect('settings')
    else:
        if request.method == 'GET':
            plan = Plan.objects.get(nickname=request.GET['plan'])
        return render(request, 'plans/checkout.html',
                      {'plan': plan, 'price': '0'})

我尝试将 {'plan':plan.id} 更改为 {'plan':plan} 并出现错误:

请求 req_0WL0lW2orGwLMV:没有这样的计划:家庭;存在一个名称为 Family,但其 ID 为 plan_H5fvA8jJ0qX9qF。

edit: dj-stripe+webhooks 正在使用,但我需要使用官方 API 创建客户。似乎在(出于某种原因)创建订阅后最后创建了重复的客户(没有付款来源)

【问题讨论】:

  • 如果您查看 Stripe 仪表板中的 Customer 对象,则会有一个 Logs 部分说明您是哪个 API 请求创建它的。这是一个很好的起点,可以查看创建它的请求并通过您的代码进行跟踪。
  • 我在挖。似乎是在(出于某种原因)创建订阅之后最后创建了重复的客户(没有付款来源)。
  • 可能是用户双击了前端,或者前端发送了两次请求,你最终调用了stripe.Customer.create,但是POST 正文为零或没有令牌。
  • 不,是我在测试。我不排除 HTML/JS 做错了什么。

标签: python django python-3.x stripe-payments


【解决方案1】:
@login_required
def checkout(request):
    if request.method == 'POST':
        plan = Plan.objects.get(nickname=request.POST['plan'])

        # Create the stripe Customer, by default subscriber Model is User,
        # this can be overridden with settings.DJSTRIPE_SUBSCRIBER_MODEL
        stripe_customer, _ = Customer.get_or_create(
            subscriber=request.user)

        # Using the Stripe API, create a subscription for this customer,
        # using the customer's default payment source
        stripe_subscription = stripe.Subscription.create(customer=stripe_customer.id,
                                                         items=[
                                                             {'plan': plan.id}],
                                                         trial_from_plan=True)

        # Add the source as the customer's default card
        stripe_customer.add_card(request.POST['stripeToken'])

        # Sync the Stripe API return data to the database,
        # this way we don't need to wait for a webhook-triggered sync
        Subscription.sync_from_stripe_data(
            stripe_subscription
        )
        return redirect('settings')
    else:
        if request.method == 'GET':
            plan = Plan.objects.get(nickname=request.GET['plan'])
        return render(request, 'plans/checkout.html',
                      {'plan': plan, 'price': '0'})

【讨论】:

    猜你喜欢
    • 2015-12-26
    • 2014-11-12
    • 2023-01-18
    • 2021-11-09
    • 2019-03-28
    • 2016-11-20
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    相关资源
    最近更新 更多