def checkout(request): context = {} print('url = ',request.build_absolute_uri) cart_obj, cart_created = Cart.objects.new_or_get(request) billing_address_id = request.session.get('billing_address_id', None) shipping_address_id = request.session.get('shipping_address_id', None) billing_profile, billing_profile_created = BillingProfile.objects.new_or_get(request) order_obj = None if cart_created or cart_obj.cart_items.count() == 0: return redirect('/') login_form = LoginForm() guest_form = GuestForm() if billing_profile : if not shipping_address_id : return redirect('addresses:shipping_address') if not billing_address_id: return redirect('addresses:billing_address') context['shipping_address'] = Address.objects.get(id=shipping_address_id, billing_profile=billing_profile) context['billing_address'] = Address.objects.get(id=billing_address_id, billing_profile=billing_profile) if request.method == "POST": if billing_profile is not None: print('billing exist') order_obj, new_obj = Order.objects.new_or_get(billing_profile, cart_obj) if shipping_address_id: print(shipping_address_id) order_obj.shipping_address = Address.objects.get(id=shipping_address_id) if billing_address_id: print(billing_address_id) order_obj.billing_address = Address.objects.get(id=billing_address_id) if billing_address_id or shipping_address_id: order_obj.save() order_item = OrderItem() for item in cart_obj.cart_items.all(): order_item = OrderItem() order_item.order = order_obj order_item.item = item.item order_item.quantity = item.quantity order_item.total = item.total order_item.save() cart_obj.cart_items.all().delete() del request.session['item_count'] return redirect("success") context['order'] = order_obj context['form'] = login_form context['guest_form'] = guest_form context['cart'] = cart_obj context['billing_profile'] = billing_profile return render(request, 'carts/checkout.html', context)
def checkout_home(request): cart_obj, cart_created = Cart.objects.new_or_get(request) order_obj = None if cart_created or cart_obj.products.count() == 0: return redirect("cart") login_form = LoginForm() guest_form = GuestForm() address_form = AddressForm() billing_address_id = request.session.get("billing_address_id", None) shipping_address_id = request.session.get("shipping_address_id", None) billing_profile, billing_profile_created = BillingProfile.objects.new_or_get( request) address_qs = None has_card = None if billing_profile is not None: if request.user.is_authenticated: address_qs = Address.objects.filter( billing_profile=billing_profile) order_obj, order_obj_created = Order.objects.new_or_get( billing_profile, cart_obj) if shipping_address_id: order_obj.shipping_address = Address.objects.get( id=shipping_address_id) del request.session["shipping_address_id"] if billing_address_id: order_obj.billing_address = Address.objects.get( id=billing_address_id) del request.session["billing_address_id"] if billing_address_id or shipping_address_id: order_obj.save() has_card = billing_profile.has_card if request.method == "POST": is_prepared = order_obj.check_done() if is_prepared: did_charge, crg_msg = billing_profile.charge(order_obj) if did_charge: order_obj.mark_done() del request.session['cart_id'] if not billing_profile.user: billing_profile.set_cards_inactive() return redirect("done/") else: return redirect("done/") context = { "object": order_obj, "billing_profile": billing_profile, "login_form": login_form, "guest_form": guest_form, "address_form": address_form, "address_qs": address_qs, "has_card": has_card, "publish_key": STRIPE_PUB_KEY, } return render(request, "carts/checkout.html", context)
def checkout_login(request): cart_obj, cart_created = Cart.objects.new_or_get(request) if cart_created or cart_obj.accounts.count() == 0: return redirect("cart:home") billing_profile, billing_profile_created = BillingProfile.objects.new_or_get( request) if not billing_profile: login_form = LoginForm() guest_form = GuestForm() context = {'login_form': login_form, "guest_form": guest_form} return render(request, 'cart/checkout_login.html', context) return redirect('cart:checkout_address')
def checkout(request): cart_obj, new_obj = Cart.objects.new_or_get(request) order_obj = None if new_obj or cart_obj.products.count() == 0: return redirect('home') user = request.user billing = None login_form = LoginForm() guest_form = GuestForm() address_form = AddressForm() guest_email_id = request.session.get('guest_email_id') if user.is_authenticated: billing, billing_created = Billing.objects.get_or_create( user=user, email=user.email) elif guest_email_id is not None: guest_email_obj = GuestEmail.objects.get(id=guest_email_id) billing, billing_created = Billing.objects.get_or_create( email=guest_email_obj.email) else: pass address_query = None if billing is not None: address_query = Address.objects.filter(billing=billing) order_query = Order.objects.filter(billing=billing, cart=cart_obj, active=True) if order_query.count() == 1: order_obj = order_query.first() else: old_order_query = Order.objects.exclude(billing=billing).filter( cart=cart_obj) if old_order_query.exists(): old_order_query.update(active=False) order_obj = Order.objects.create(billing=billing, cart=cart_obj) context = { 'order': order_obj, 'billing': billing, 'guest_form': guest_form, 'login_form': login_form, 'address_form': address_form, 'address_query': address_query, } return render(request, 'carts/checkout.html', context)
def CheckoutView(request): cart_obj, cart_created = Cart.objects.new_or_get(request) order_obj = None if cart_created or cart_obj.products.count()==0: return redirect('cart-home') login_form = AuthenticationForm() guest_form = GuestForm() address_form = AddressForm() billing_address_id = request.session.get('billing_address_id', None) shipping_address_id = request.session.get('shipping_address_id', None) billing_profile, billing_guest_profile_create = BillingProfile.objects.new_or_get(request) address_qs = None if billing_profile is not None: if request.user.is_authenticated: address_qs = Address.objects.filter(billing_profile=billing_profile) order_obj, order_obj_created = Order.objects.new_or_get(billing_profile, cart_obj) if shipping_address_id: order_obj.shipping_address = Address.objects.get(id=shipping_address_id) del request.session['shipping_address_id'] if billing_address_id: order_obj.billing_address = Address.objects.get(id=billing_address_id) del request.session['billing_address_id'] if billing_address_id or shipping_address_id: order_obj.save() if request.method == 'POST': is_done = order_obj.check_done() if is_done: order_obj.mark_paid() request.session['cart_items'] = 0 del request.session['cart_id'] return redirect('checkout-success') context = { 'object': order_obj, 'billing_profile': billing_profile, 'login_form': login_form, 'guest_form': guest_form, 'address_form': address_form, 'address_qs': address_qs, } return render(request, 'carts/checkout.html', context)
def checkout_home(request): cart_obj, cart_created = Cart.objects.new_or_get(request) order_obj = None if cart_created or cart_obj.products.count() == 0: return redirect("cart:home") login_form = LoginForm() guest_form = GuestForm() billing_profile, billing_profile_created = BillingProfile.objects.new_or_get( request) # guest_email_id = request.session.get('guest_email_id') # if user.is_authenticated: # # logged in user checkout; remember payment detail # billing_profile, billing_profile_created =BillingProfile.objects.get_or_create( # user=user, email= user.email) # elif guest_email_id is not None: # # guest user checkout; auto reloads payment detail # guest_email_obj = GuestEmail.objects.get(id=guest_email_id) # billing_profile, billing_guest_profile_created = billing_guest_profile_created =BillingProfile.objects.get_or_create( # email= guest_email_obj.email) # else: # print("something has gone horiblly wrong") # pass if billing_profile is not None: order_obj, order_obj_created = Order.objects.new_or_get( billing_profile, cart_obj) context = { "object": order_obj, "billing_profile": billing_profile, "login_form": login_form, "guest_form": guest_form } return render(request, "carts/checkout.html", context)
def checkout_home(request): cart_obj, cart_created = ShopCart.objects.get_or_new(request) # get cart cart_items = cart_obj.shopCartItems.all() login_form = LoginForm() guest_form = GuestForm() signup_form = SignUpForm() address_form = AddressForm() edit_form = EditSaleForm() #initializers sale_obj = None address_qs = None discount_qs = None card_qs = None coupon_form = None ship_method_form = None billing_address_id = request.session.get('billing_address_id', None) shipping_address_id = request.session.get('shipping_address_id', None) if cart_created or cart_items.count( ) == 0: # if cart was just created redirect to cart home return redirect('shopcart:home') # first check if cart_items are available for item in cart_items: if item.piIsAvailable == False: messages.warning( request, 'An item in your cart has become unavailable and was removed. This does not necessarily mean we are out of stock. Please attempt to add it back to cart to verify.' ) cart_obj.shopCartItems.remove(item) # if sale was already created remove the item from the sale as well billing_profile, billing_profile_created = BillingProfile.objects.new_or_get( request) if billing_profile is not None: # remove item from sale sale_item = SaleItems.objects.filter( prodStockID=item.prodStockID, saleID__saleStatus='created', saleID__saleBillingProfile=billing_profile) sale_item.delete() return redirect('shopcart:home') # get billing profile has_card = False billing_profile, billing_profile_created = BillingProfile.objects.new_or_get( request) if billing_profile is not None: address_qs = Address.objects.filter( addressBillingProfile=billing_profile, addressActive=True, ) # create sale sale_obj, sale_obj_created = Sale.objects.new_or_get( billing_profile, cart_obj) # initialize coupon form with sale object coupon_form = SDiscountForm(initial={'sobj': sale_obj.saleStringID}) ship_method_form = CustomerShipMethodForm( initial={'sale': sale_obj.saleStringID}) # get discount qs discount_qs = SDiscount.objects.filter(sale=sale_obj) if shipping_address_id: sale_obj.saleShippingAddress = Address.objects.get( id=shipping_address_id) del request.session['shipping_address_id'] if billing_address_id: sale_obj.saleBillingAddress = Address.objects.get( id=billing_address_id) del request.session['billing_address_id'] if billing_address_id or shipping_address_id: sale_obj.save() has_card = billing_profile.has_card card_qs = billing_profile.get_cards() if request.method == 'POST': '''check if sale is done''' is_done = sale_obj.check_done() if not is_done: pass elif is_done: did_charge, charge_msg = billing_profile.charge(sale_obj) if did_charge: # set cart status to submitted cart_obj.shopCartStatus = 'Submitted' cart_obj.save() sale_obj.mark_paid() from_email = getattr(settings, 'EMAIL_HOST_ALIAS_SUPPORT', settings.EMAIL_HOST_USER) # # send order confirmation email to_email = str(sale_obj.saleBillingProfile) html_message = render_to_string( 'emails/order-confirmation.html', { 'sale_obj': sale_obj, }) plain_message = strip_tags(html_message) subject = 'HoneyBox Boutique - Order Confirmation %s' % ( sale_obj.saleStringID) send_mail(subject, plain_message, from_email, [to_email], auth_user=settings.EMAIL_HOST_USER, auth_password=settings.EMAIL_HOST_PASSWORD, html_message=html_message) # clear guest_email_id from session if 'guest_email_id' in request.session: del request.session['guest_email_id'] if not billing_profile.user: billing_profile.set_inactive() # clear carts session stuff request.session['cart_count'] = 0 # clear items from user cart if authenticated is_empty = cart_obj.empty_shopcart() return redirect('shopcart:success') else: # add error message here messages.warning(request, charge_msg) return redirect('shopcart:checkout') context = { "cart_obj": cart_obj, "sale_obj": sale_obj, "shopcart": cart_obj, "billing_profile": billing_profile, "login_form": login_form, "guest_form": guest_form, "signup_form": signup_form, "address_form": address_form, "coupon_form": coupon_form, "edit_form": edit_form, "ship_method_form": ship_method_form, "address_qs": address_qs, "discount_qs": discount_qs, "card_qs": card_qs, "publish_key": STRIPE_PUB_KEY, "has_card": has_card, } return render(request, 'shopcart/checkout.html', context)