Example #1
0
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(request=request)
    guest_form = GuestForm(request=request)
    address_form = AddressCheckoutForm()
    billing_address_id = request.session.get("billing_address_id", None)
    shipping_address_required = not cart_obj.is_digital
    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 = False
    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_paid()
                request.session['cart_items'] = 0
                del request.session['cart_id']
                if not billing_profile.user:
                    billing_profile.set_cards_inactive()
                return redirect("cart:success")
            else:
                return redirect("cart:checkout")
    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,
        "shipping_address_required": shipping_address_required
    }
    return render(request, "carts/checkout.html", context)
Example #2
0
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(request=request)
    guest_form = GuestForm(request=request)
    address_form = AddressCheckoutForm()
    billing_address_id = request.session.get('billing_address_id', None)
    shipping_address_required = cart_obj.delivery
    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 = False

    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, orderID = billing_profile.charge('S', order_obj)
            if did_charge:
                order_obj.mark_paid() # sort signal
                request.session['cart_items'] = 0
                del request.session['cart_id']
                if not billing_profile.user:
                    billing_profile.set_cards_inactive()
                return redirect(reverse('cart:success', kwargs={ 'orderID': orderID }))
            else:
                return redirect('cart:checkout')

    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,
        'shipping_address_required': shipping_address_required,
    }
    return render(request, 'carts/checkout.html', context)
Example #3
0
def checkout_home(request):
    cart, new_cart = Cart.objects.new_or_get(request)
    order = None
    if new_cart or cart.products.count() == 0:
        return redirect('carts:home')
    login_form = LoginForm(request=request)
    guest_form = GuestForm(request=request)
    address_form = AddressCheckoutForm()
    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 = False
    if billing_profile is not None:
        if request.user.is_authenticated:
            address_qs = Address.objects.filter(
                billing_profile=billing_profile)
        order, order_created = Order.objects.new_or_get(
            billing_profile=billing_profile, cart=cart)
        if shipping_address_id:
            order.shipping_address = Address.objects.get(
                id=shipping_address_id)
            del request.session['shipping_address_id']
        if shipping_address_id:
            order.save()
        has_card = billing_profile.has_card

    if request.method == 'POST':
        is_prepared = order.check_done()
        if is_prepared:
            charge, charge_msg = billing_profile.charge(order)
            if charge:
                order.mark_paid()
                request.session['cart_items'] = 0
                del request.session['cart_id']
                if not billing_profile.user:
                    billing_profile.set_cards_inactive()
                return redirect('carts:success')
            else:
                print(charge_msg)
                return redirect('carts:checkout')
    context = {
        'order': order,
        '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)
Example #4
0
    def get(self, 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(request=request)
        guest_form = GuestForm(request=request)
        address_form = AddressCheckoutForm()
        shipping_address_required = not cart_obj.is_digital
        shipping_address_id = request.session.get('shipping_address_id', None)
        billing_address_id = request.session.get('billing_address_id', None)
        billing_profile, billing_profile_created = BillingProfile.objects.new_or_get(
            request)

        address_qs = None
        has_card = False
        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 shipping_address_id or billing_address_id:
                order_obj.save()
            has_card = billing_profile.has_card
        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,
            'shipping_address_required': shipping_address_required,
        }
        return render(request, self.template_name, context)
Example #5
0
def checkout_home(request):
    cart = Cart(request)
    order_obj = None

    if cart.get_length() == 0:
        messages.info(request, "No item in cart.")
        return redirect("menu:trouser_by_category", "collections")
    login_form = LoginForm(request=request)
    guest_form = GuestForm(request=request)

    shipping_address_id = request.session.get("shipping_address_id", None)
    order_id = request.session.get("order_id", None)
    old_post = request.session.get("old_form_post", None)

    address_form = AddressCheckoutForm(initial=old_post)
    billing_profile = BillingProfile.objects.new_or_get(request)
    address_qs = None
    address = None

    if billing_profile is not None:
        address_qs = Address.objects.filter(billing_profile=billing_profile)
        if request.user.is_authenticated:
            if address_qs:
                if shipping_address_id:
                    address = address_qs.filter(id=shipping_address_id).first()
                else:
                    address = address_qs.filter(default=True).first()
                    if not address:
                        address = address_qs.first()
                shipping_address_id = request.session.get(
                    "shipping_address_id", address.id)
        else:
            if address_qs:
                try:
                    del request.session["old_form_post"]
                except KeyError:
                    pass
                address = address_qs.first()
                shipping_address_id = request.session.get(
                    "shipping_address_id", address.id)
                address_form = AddressCheckoutForm(instance=address)
                guest_form = GuestForm(request=request,
                                       initial={
                                           'email': billing_profile.email,
                                       })

        trousersString = ''
        order_obj = Order.objects.new_or_get(billing_profile,
                                             order_id=order_id)
        request.session["order_id"] = order_obj.id
        if shipping_address_id:
            address_temp = Address.objects.get(id=shipping_address_id)
            order_obj.shipping_address = address_temp
            # print(order_obj.shipping_address)
            order_obj.save()
            try:
                del request.session["shipping_address_id"]
            except KeyError:
                pass

    if request.method == "POST":
        "check that order is done"
        try:
            order_obj.orders.all().delete()
        except ObjectDoesNotExist:
            print("error")
        finally:
            trouser_temp = {}
            carttmp = request.session.get('cart')
            trouser_ids = carttmp.keys()
            trousers = TrouserVariant.objects.filter(
                id__in=trouser_ids).prefetch_related('trouser_variant_meta',
                                                     'trouser_variant_images')
            for trouser in trousers:
                trouser_temp[str(trouser.id)] = trouser
            for key, item in carttmp.items():
                if len(item['size']) == 1:
                    OrderItem.objects.create(order=order_obj,
                                             trouser=trouser_temp[key],
                                             price=item['price'],
                                             quantity=item['quantity'][0],
                                             size=item['size'][0],
                                             billing_profile=billing_profile)
                else:
                    for i, size in enumerate(item['size']):
                        OrderItem.objects.create(
                            order=order_obj,
                            trouser=trouser_temp[key],
                            price=item['price'],
                            quantity=item['quantity'][i],
                            size=size,
                            billing_profile=billing_profile)
            # clear the cart
            msg = "Order created successfully."
            messages.success(request, msg)
            return redirect('cart:payment')
    context = {
        "object": order_obj,
        "billing_profile": billing_profile,
        "login_form": login_form,
        "guest_form": guest_form,
        "address_form": address_form,
        "address_qs": address_qs,
        "address": address,
        "shipping_address_id": shipping_address_id,
    }
    return render(request, "cart/checkout.html", context)
Example #6
0
def checkout_home(request):
    cart_obj, cart_created = Cart.objects.new_or_get(request)
    if cart_created or cart_obj.books.count() == 0:
        return redirect("carts:home")
    order_obj = None
    login_form = LoginForm(request=request)
    address_form = AddressCheckoutForm()
    shipping_address_id = request.session.get("shipping_address_id", None)
    
    billing_profile, billing_profile_created = 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 shipping_address_id:
            order_obj.save()
    
    if request.method == "POST":
        "check that order is done"
        cart_obj.save()
        if cart_obj.books_removed.count() != 0:
            for book in cart_obj.books_removed.all():
                messages.add_message(request, messages.ERROR, "Book " + book.name + " has been removed from your cart. Because it went out of stock.")
                cart_obj.books_removed.remove(book)
                request.session['cart_item_count'] = cart_obj.books.count()
                if cart_obj.books.count() == 0:
                    return redirect("carts:home")
                else:
                    return redirect("carts:checkout")
        is_done = order_obj.check_done()
        if is_done:
            order_obj.mark_paid()
            for book in order_obj.cart.books.all():
                book_quantity = BookQuantity.objects.get(cart=cart_obj, book=book)
                book.numberOfCopiesSold  += book_quantity.quantity
                book.inventory -= book_quantity.quantity
                book.save()
            del request.session['cart_item_count']
            del request.session['cart_id']
            cart_obj.active = False
            cart_obj.save()
            return redirect("carts:success")
    
    cart_obj.save()
    if cart_obj.books_removed.count() != 0:
        for book in cart_obj.books_removed.all():
            messages.add_message(request, messages.ERROR, "Book " + book.name + " has been removed from your cart. Because it went out of stock.")
            cart_obj.books_removed.remove(book)
            request.session['cart_item_count'] = cart_obj.books.count()
            if cart_obj.books.count() == 0:
                return redirect("carts:home")
    context = {
        "object": order_obj,
        "billing_profile": billing_profile,
        "login_form": login_form, 
        "address_form": address_form,
        "address_qs": address_qs,
    }
    return render(request, "carts/checkout.html", context)
Example #7
0
def checkout_home(request):
    """
    final checkout view, incudes checkout and sending invoice through mail
    """
    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(request=request)
    guest_form = GuestForm(request=request)
    address_form = AddressCheckoutForm()
    billing_address_id = request.session.get("billing_address_id", None)

    shipping_address_required = not cart_obj.is_digital


    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 = False
    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":
        "check that order is done"
        is_prepared = order_obj.check_done()
        if is_prepared:
            did_charge, crg_msg = billing_profile.charge(order_obj)
            if did_charge:
                order_obj.mark_paid() # sort a signal for us
                request.session['cart_items'] = 0
                del request.session['cart_id']
                if not billing_profile.user:
                    '''
                    is this the best spot?
                    '''
                    billing_profile.set_cards_inactive()

                # send mail just berfore success of cart
                user = request.user
                user_full_name = user.get_full_name()
                user_email = user.email

                invoice_context = {
                    "object": order_obj,
                    "billing_profile": billing_profile,
                    "address_qs": address_qs,
                    "user_full_name":user_full_name,
                    "user_email":user_email
                }

                post_pdf = render_to_pdf(
                    'carts/checkout_reciept.html', invoice_context
                )

                email = EmailMessage(
                'Purchase innvoice', 'Invoice of purchase that you do on Electro.com', 'Electro', ['*****@*****.**'])
                email.attach('invoice.pdf', post_pdf, 'application/pdf')
                email.send()

                return redirect("cart:success")
            else:
                print(crg_msg)
                return redirect("cart:checkout")
    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,
        "shipping_address_required": shipping_address_required,
    }

    return render(request, "carts/checkout.html", context)