def pay(request): """ Do the "create order" thing. It get the CARD_ID_SESSION_KEY from session to get the items information that been added into cart by user. Then calling the cart_subtotal() function in cart_module to get the order total price. The shipping information will also be collected through html form. If every thing is fine, then create an order, else return error message. :param request: :return: """ if request.method == 'POST': if request.session[CART_ID_SESSION_KEY] != '': cart_items = cart_module.get_cart_items(request) cart_subtotal = cart_module.cart_subtotal(request) shipping_zip = request.POST['shipping_zip'] shipping_add = request.POST['shipping_add'] shipping_to = request.POST['shipping_to'] username = request.user.username cart_id = cart_items[0].cart_id try: order = Order.objects.get(cart_id = request.session[CART_ID_SESSION_KEY]) except Order.DoesNotExist: order = None if order != None: order = Order.objects.get(cart_id=request.session[CART_ID_SESSION_KEY]) else: order = Order() order.cart_id = cart_id order.shipping_add = shipping_add order.shipping_to = shipping_to order.shipping_zip = shipping_zip order.username = username order.total_price = cart_subtotal order.save() request.session[CART_ID_SESSION_KEY] = '' # delete session key return HttpResponseRedirect('/account/order/'+order.order_id+'/') else: return render(request, 'account/error.html',{'error':'This order has been created.' }) else: return HttpResponseRedirect('/account/')