[英]Python Official Stripe Client Creating Two Customers
我在下面使用的這段代碼運行良好,除了結帳完成時,我注意到每次有人在我的網站上結帳時都會輸入兩個客戶條目。
一個有默認源集,一個沒有。 我附上了截圖。
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。
編輯: 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'})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.