我目前正在将 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/