2017-01-29 53 views
0

我在我的網站上使用django-allauth的API條帶,我想爲剛剛訂閱的新用戶創建stripe_id,不久前我的代碼正在工作,今天我得到了一個我從未遇到過的新錯誤:如何在訂閱上創建stripe_id?

stripe.error.AuthenticationError: No API key provided. (HINT: set your API key using "stripe.api_key = "). You can generate API keys from the Stripe web interface.

當用戶訂購或登錄中首次出現在我創建一個新的stripe_id,回調的調用,但錯誤加薪,當我創建一個客戶回調。見models.py

class Profile(models.Model): 
    stripe_id = models.CharField(max_length=200, null=True, blank=True) 
    user = models.OneToOneField(User, on_delete=models.CASCADE) 
    ... 

def stripeCallback(sender, request, user, **kwargs): 
    user_stripe_account, created = Profile.objects.get_or_create(user=user) 
    if user_stripe_account.stripe_id is None or user_stripe_account.stripe_id == '': 
     new_stripe_id = stripe.Customer.create(email=user.email) #error occurs here 
     user_stripe_account.stripe_id = new_stripe_id['id'] 
     user_stripe_account.save() 

user_logged_in.connect(stripeCallback) 
user_signed_up.connect(stripeCallback) 

我缺少的東西?

回答

1

您正在類配置文件中定義stripe_id,但這應該在其外部分配。 嘗試設置

stripe.api_key = settings.STRIPE_SECRET_KEY 

以上所有其他代碼。

這也完全在此YouTube視頻說明: https://www.youtube.com/watch?v=9Wbfk16jEOk&t=79s

例如,這是我的一個類似的項目創造了一個models.py:

from __future__ import unicode_literals 
from django.conf import settings 
from django.db import models 
from allauth.account.signals import user_logged_in, user_signed_up 
import stripe 
# Create your views here. 

stripe.api_key = settings.STRIPE_SECRET_KEY 

# Create your models here. 

class profile(models.Model): 
    name = models.CharField(max_length=120) 
    user = models.OneToOneField(settings.AUTH_USER_MODEL, null=True, 
    blank=True) 
    description = models.TextField(default='description default text') 

    def __unicode__(self): 
     return self.name 

class userStripe(models.Model): 
    user = models.OneToOneField(settings.AUTH_USER_MODEL) 
    stripe_id = models.CharField(max_length=200, null=True, blank=True) 

    def __unicode__(self): 
     if self.stripe_id: 
      return str(self.stripe_id) 
     else: 
      return self.user.username 

    def stripeCallback(sender, request, user, **kwargs): 
     user_stripe_account, created = 
       userStripe.objects.get_or_create(user=user) 
     if created: 
      print 'created for %s'%(user.username) 
     if user_stripe_account.stripe_id is None or 
      user_stripe_account.stripe_id == '': 
      new_stripe_id = stripe.Customer.create(email=user.email) 
      user_stripe_account.stripe_id = new_stripe_id['id'] 
      user_stripe_account.save() 

def profileCallback(sender, request, user, **kwargs): 
    userProfile, is_created = profile.objects.get_or_create(user=user) 
    if is_created: 
     userProfile.name = user.username 
     userProfile.save() 
+0

謝謝!我想知道爲什麼它不起作用,我記得現在我從模型中刪除了stripe_secret_key,因爲我使用的是django-allauth,但我想我也應該將它保留在我的設置中。 – Lindow