【问题标题】:Two orders being submitted after checkout结帐后提交了两个订单
【发布时间】:2020-08-02 05:15:57
【问题描述】:

我遇到的问题是,在我提交付款后,订单被提交了两次。在某些情况下,如果我第一次在我的设备中加载应用程序,它将被收取一次费用。但是,由于它不止一次完成,同一订单将产生不止一次的费用。

# Create your views here.
stripe.api_key = settings.STRIPE_SECRET


@login_required()
def checkout(request):
    if request.method == "POST":
        # call the two forms that will be used
        order_form = OrderForm(request.POST)
        payment_form = MakePaymentForm(request.POST)

        # Then will check if both forms are valid if yes, save
        if order_form.is_valid() and payment_form.is_valid():
            order = order_form.save(commit=False)
            order.date = timezone.now()
            order.save()

            cart = request.session.get('cart', {})
            total = 0
            for id, quantity in cart.items():
                destination = get_object_or_404(Destinations, pk=id)
                total += quantity * destination.price
                order_line_item = OrderLineItem(
                    order=order,
                    destination=destination,
                    quantity=quantity
                )
                order_line_item.save()

            try:
                customer = stripe.Charge.create(
                    amount=int(total * 100),
                    currency="EUR",
                    description=request.user.email,
                    card=payment_form.cleaned_data['stripe_id'],
                )
            except stripe.error.CardError:
                messages.error(request, "Your card was declined!")

            if customer.paid:
                messages.error(request, "You have successfully paid")
                request.session['cart'] = {} #clear the cart in session
                return redirect(reverse('destination'))
            else:
                messages.error(request, "Unable to take payment")
        else:
            messages.error(request, "We were unable to take a payment with that card!")
    else:
        payment_form = MakePaymentForm()
        order_form = OrderForm()
    return render(request, "checkout.html", {"order_form": order_form, "payment_form": payment_form, "publishable": settings.STRIPE_PUBLISHABLE})

由于它是我正在做的训练营的回收代码,我尝试修复第二个 if on order.save() 并将其添加到 if customer.paid: 之后,但我收到错误。

【问题讨论】:

  • 您是否检查过您的 AJAX/HTTP 请求没有被发送两次?
  • 我不能在终端上看到错误,我该怎么做。我能看到的唯一与条纹有关的是 chrome 控制台中的一条消息(index):2 It looks like Stripe.js was loaded more than one time. Please only load it once per page.

标签: python django stripe-payments checkout


【解决方案1】:

很可能(如 cmets 中所述)您的客户端代码发送了多个请求来创建费用。理想情况下,您应该弄清楚为什么会发生这种情况,但您也可以使用Idempotency Keys 来解决它。

【讨论】:

  • 我看了你给我的关于幂等性的信息。然而,我花了一天的时间来分析和修复这个错误,我发现我加载了Stripe.publishableKey 两次。一个在base.html 中,另一个在checkout.html 中。一旦我检查了 chrome 控制台并收到一条消息,即条带已加载两次,我就遇到了这个问题。因此,我将其从结帐页面中删除,并将条带键保留在 base.html 页面中,问题就解决了。
猜你喜欢
  • 2015-03-08
  • 2019-06-23
  • 1970-01-01
  • 2015-12-07
  • 2019-08-20
  • 1970-01-01
  • 2018-05-02
  • 2013-03-18
  • 2017-12-31
相关资源
最近更新 更多