python - 如何在我的应用程序中使用 Stripe 支付网关?
问题描述
我在我的应用程序中集成了 Stripe 支付网关,但既没有生成令牌也没有ref_code
生成令牌。我该如何正确地做到这一点?
class PaymentView(View):
def get(self, *args, **kwargs):
order = Order.objects.get(user=self.request.user, ordered=False)
if order.billing_address:
context = {
'order': order,
'DISPLAY_COUPON_FORM': False,
'STRIPE_PUBLIC_KEY': settings.STRIPE_PUBLIC_KEY
}
return render(self.request, 'payment.html', context)
else:
messages.warning(self.request, "You've not added billing address.")
return redirect('e_commerce:checkout')
def post(self, *args, **kwargs):
order = Order.objects.get(user=self.request.user, ordered=False)
token = self.request.POST.get('stripeToken')
amount = int(order.get_total()*100) # cents
try:
charge = stripe.Charge.create(
amount=amount,
currency="usd",
source=token
)
# order.ordered = True
# create the payment
payment = Payment()
payment.stripe_charge_id = charge['id']
payment.user = self.request.user
payment.amount = order.get_total()
payment.save()
# assign the payment to the order
order_items = order.items.all()
order_items.update(ordered=True)
for item in order_items:
item.save()
order.ordered = True
order.payment = payment
# TODO assign ref code
order.ref_code = create_ref_code()
order.save()
# print("your order placed!")
messages.success(self.request, "your order was successful.")
return redirect("/e_commerce")
except stripe.error.CardError as e:
# Since it's a decline, stripe.error.CardError will be caught
body = e.json_body
err = body.get('error', {})
messages.warning(self.request, f"{err.get('messages')}")
return redirect("/e_commerce")
except stripe.error.RateLimitError as e:
# Too many requests made to the API too quickly
messages.warning(self.request, "Rate limit error")
return redirect("/e_commerce")
except stripe.error.InvalidRequestError as e:
# Invalid parameters were supplied to Stripe's API
messages.warning(self.request, "Invalid parameters ,Please try again!")
return redirect("/e_commerce")
except stripe.error.AuthenticationError as e:
# Authentication with Stripe's API failed
# (maybe you changed API keys recently)
messages.warning(self.request, "Authentication error ")
return redirect("/e_commerce")
except stripe.error.APIConnectionError as e:
# Network communication with Stripe failed
messages.warning(self.request, "Network error")
return redirect("/e_commerce")
except stripe.error.StripeError as e:
# Display a very generic error to the user, and maybe send
# yourself an email
messages.warning(
self.request, "Something went wrong.You were not charged.Please try again.")
return redirect("/e_commerce")
except Exception as e:
# Something else happened, completely unrelated to Stripe
messages.warning(self.request, "A serious error occured.We have been notified.")
return redirect("/e_commerce")
messages.warning(self.request, "Invalid data received.")
return redirect("payment/stripe")
解决方案
推荐阅读
- java - 由于泛型问题,我的论点在该方法中不起作用
- javascript - 使用 AJAX 和 PHP 时的 Google 图表问题
- python - 通过 Selenium 向 WebElement 发送文本问题
- docker - NEO4j:不同主机中的因果集群
- javascript - 一个没有参数的函数怎么会突然在javascript中取参数?
- firebase - Firestore 中的数字分页
- angular - 类型'typeof observable angular 8 上不存在属性'throwerror'
- java - 使用 OpenCV 在 Android Studio 应用程序中获取 jpg 文件的路径以制作 Mat
- node.js - 用 Alexa 数数
- angular - 最初加载远程数据时 Angular 组件出现问题