首页 > 解决方案 > 如何在我的应用程序中使用 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")

标签: pythondjangoformsvalidationweb

解决方案


推荐阅读