python - 在 django/python 中创建 Stripe 电荷的正确方法

标签 python django stripe-payments

我目前正在将 stripe 集成到一个 django 项目中。我遵循了 stripe website 上的教程使用代码示例,但我不确定它们是否已准备好生产。这是应用程序中最微妙的部分,我想确保它正确完成。目前,我有一些看起来与此类似的东西。

if new_order_form.is_valid():
    new_order = new_order_form.save(commit=False)
    new_order.total = 10
    new_order.order_number = generate_order_number(8)
    try:
      charge = stripe.Charge.create(
          amount=1000, # new_order.total * 100
          currency="usd",
          source=token,
          description="Example charge"
      )
      new_order.charge_id = charge.id
    except stripe.error.CardError, e:
      # The card has been declined
      pass

    new_order.save()
    return HttpResponseRedirect('/thanks/')
return redirect(request, 'new_order.html' context)

编辑: 我的顾虑是:

  • 会不会出现卡被扣费两次的情况?
  • 会不会有创建和保存订单的场景
    但充值不成功?
  • 应该在哪里创建和保存订单?

最佳答案

这是我们在项目中使用的,希望对您有所帮助!!

try:
    charge = stripe.Charge.create(
      amount={{amount}}, 
      currency="usd",
      customer={{customer}},
      description={{description}},
      metadata={{this.id}}
  )
except stripe.error.CardError as e:
    # Problem with the card
    pass
except stripe.error.RateLimitError as e:
    # Too many requests made to the API too quickly
    pass
except stripe.error.InvalidRequestError as e:
    # Invalid parameters were supplied to Stripe API
    pass
except stripe.error.AuthenticationError as e:
    # Authentication Error: Authentication with Stripe API failed (maybe you changed API keys recently)
    pass
except stripe.error.APIConnectionError as e:
    # Network communication with Stripe failed
    pass
except stripe.error.StripeError as e:
    # Stripe Error
    pass
else:
    #success

关于python - 在 django/python 中创建 Stripe 电荷的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31746633/

相关文章:

python - 如何在python中反转列表的最后N个元素

Python:TypeError: 'TreeNode' 对象不可迭代

django - 如何在 django 翻译中翻译数字?

python - 如何在 Python/Flask 中使用 stripe webhook 识别客户

javascript - Parse.com 的 Stripe 云代码模块有限制吗?

javascript - Flask AJAX 请求返回空 ImmutableMultiDict,相同的 AJAX 适用于 http.server

python - 所有卷积神经网络都可以用于任意数量 channel 的图像吗?

Django:仅显示用户自己的数据

Django 扩建

php - 如何在 symfony2 中包含 strip 客户端 api。如何在 symfony2 中包含没有类的文件