django - Django 应用程序中的 Stripe 支付,可发布的 key 错误

标签 django payment stripe-payments payment-processing

我正在构建一个 Django 应用程序,它使用 Stripe 让用户相互付款。卖家需要将他们的账户连接到 Stripe,这样我才能将他们的访问和刷新 token 保存到数据库中。买家还可以在付款期间向该服务捐款。

如果我尝试向卖家收费,我会收到一个我不太明白的错误:

"Invalid token id: tok_355k8o2rGvbwWLbLbNKPAcOk. The publishable key used to create this token is from a different account.

卖家的访问 token 有问题,但我不知道是什么。我仔细检查了我的 secret key 和可发布 key ,它们没问题。

这是我在付款时使用的一段代码。

将卖家与 Stripe 联系起来:

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.")

你知道如何纠正这个问题吗?非常感谢。

最佳答案

访问 token 带有自己的可发布 key ,您在为该用户创建卡片 token 时需要在您的表单上使用该 key 。您需要存储该可发布 key :

profile.access_token = r['access_token']
profile.refresh_token = r['refresh_token']
profile.publishable_key = r['stripe_publishable_key']
profile.save()

关于django - Django 应用程序中的 Stripe 支付,可发布的 key 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22691300/

相关文章:

Django Gevent 集成

python - 在 django 上使用 Amazon S3 使用 ImageField 上传图像

android - 无需应用内支付的产品线产品升级概念

php - Stripe ,验证卡并仅在操作后充电

javascript - 如何检查 Stripe 元素中的 Stripe 优惠券有效性?

stripe-payments - Stripe PHP - 如何检索新添加的卡的 ID?

python - Pystache 无需转义(未转义)

python - Google App Engine + 表单验证

ios - ApplePay token 中的密码是什么?

java - 创建帐户时 Stripe 连接错误