Python官方条纹客户端创建两个客户
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python官方条纹客户端创建两个客户相关的知识,希望对你有一定的参考价值。
我在下面使用的这段代码非常有效,除非结帐完成后,我注意到每次有人在我的网站上结帐时都会输入两个客户条目。
一个具有默认来源集,一个没有。我已附上屏幕截图。
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创建客户。似乎(由于某种原因)在创建订阅之后,最后创建了重复的客户(没有付款来源)
答案
@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')
以上是关于Python官方条纹客户端创建两个客户的主要内容,如果未能解决你的问题,请参考以下文章