2
我正在構建一個使用Stripe讓用戶互相支付的Django應用程序。賣家需要將他們的賬戶連接到Stripe,以便將他們的訪問權限和刷新令牌保存到數據庫中。買家還可以在付款期間爲服務捐款。在Django應用程序中的條帶支付,可發佈的關鍵錯誤
如果我嘗試賣家收費,我得到一個錯誤,我真的不明白:
「無效的令牌ID:tok_355k8o2rGvbwWLbLbNKPAcOk用於創建此令牌的發佈的關鍵是從不同的帳戶。
也有一些是錯誤的賣方的訪問令牌,但我無法弄清楚它是什麼。我仔細檢查過我的祕密和發佈的鑰匙,他們都OK。
這裏是我使用du的一段代碼環支付。
連接賣家與條紋:
def callback(request):
code = request.GET.get('code')
profile = UserProfile.objects.get(user=request.user)
r = requests.post('https://connect.stripe.com/oauth/token', params={
'client_secret': settings.STRIPE_SECRET_KEY,
'code': code,
'grant_type': 'authorization_code'
}).json()
try:
profile.access_token = r['access_token']
profile.refresh_token = r['refresh_token']
profile.save()
messages.success(request, "Your account was successfully connected to Stripe.")
except KeyError:
messages.error(request, "Unable to connect your account to Stripe.")
return redirect('home')
製作費:
def charge(request, item_id):
stripe.api_key = settings.STRIPE_SECRET_KEY
try:
item = Item.objects.get(pk=item_id)
profile = UserProfile.objects.get(user=item.owner)
access_token = profile.access_token
except Item.DoesNotExist:
raise Http404
if request.method == 'POST':
form = PaymentForm(request.POST)
if form.is_valid():
try:
charge = stripe.Charge.create(
# Multiply by 100 to get value in cents
amount=form.cleaned_data['amount'] * 100,
application_fee=form.cleaned_data['donation'] * 100,
currency='gbp',
card=form.cleaned_data['stripeToken'],
description="{} by {}".format(item.title, item.author),
api_key=access_token, # <-- This is the line where the error occurs
)
messages.success(request, "The payment was successful.")
except stripe.CardError, e:
messages.error(request, "The payment could not be completed.")
你有一個想法如何糾正呢?非常感謝。
它的工作原理類似於魅力。謝謝! – lukas
「你需要在表單上使用哪一個」是什麼意思? –
您收集卡片詳細信息的表單使用可發佈密鑰將卡片詳細信息轉換爲卡片令牌:https://stripe.com/docs/tutorials/forms – brian