Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 结帐后提交两份订单_Python_Django_Stripe Payments_Checkout - Fatal编程技术网

Python 结帐后提交两份订单

Python 结帐后提交两份订单,python,django,stripe-payments,checkout,Python,Django,Stripe Payments,Checkout,我的问题是,在我提交付款后,订单将提交两次。有些情况下,如果我第一次在设备中加载应用程序,它将被充电一次。然而,由于这是做了不止一次,将有一个以上的费用为同一订单 # Create your views here. stripe.api_key = settings.STRIPE_SECRET @login_required() def checkout(request): if request.method == "POST": # call the two for

我的问题是,在我提交付款后,订单将提交两次。有些情况下,如果我第一次在设备中加载应用程序,它将被充电一次。然而,由于这是做了不止一次,将有一个以上的费用为同一订单

# 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})
由于这是我正在做的训练营中回收的代码,我已尝试在
order.save()
上修复第二个if,并将其添加到
if customer.paid:
之后,但我遇到了错误


很可能(如评论中所述)您的客户端代码正在发送多个创建费用的请求。理想情况下,您应该找出发生这种情况的原因,但您也可以使用来解决它。

您是否检查过您的AJAX/HTTP请求没有发送两次?既然我看不到终端上的错误,我该怎么做。我能看到的唯一与stripe相关的是chrome控制台
(index)中的一条消息:2看起来stripe.js被加载了不止一次。请每页只加载一次。
。我看过你给我的关于幂等性的信息。然而,我花了一天的时间来分析和修复这个bug,我发现我加载了两次
Stripe.publishableKey
。一个在
base.html
中,另一个在
checkout.html
中。我在检查chrome控制台时发现了这个问题,并收到一条消息说stripe加载了两次。因此,我将其从签出页面中删除,并将条带键保留在
base.html
页面中,问题就解决了。