【发布时间】: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