Пример #1
0
def add_to_cart(request):
    """
    Add product to cart
    """
    product_id = int(request.POST['product_id'])

    # Checking if user already has cart in session
    # otherwise create a new cart for the user
    if 'cart_id' in request.session:
        cart_id = int(request.session['cart_id'])
        cart = Cart.get_cart(cart_id)
    else:
        cart = Cart.get_cart()
        request.session['cart_id'] = cart.id

    try:
        quantity = int(request.POST['quantity'])
        if quantity > 0:
            cart.add_item(product_id, quantity, request.user)
        else:
            raise ValueError()
    except ValueError:
        return HttpResponseBadRequest(
            'Product quantity is not correct, please enter one or more products in numbers.'
        )

    if request.is_ajax():
        default_currency = get_default_currency(request)
        return render(request, 'sales/cart_basket.html', {
            'cart': cart,
            'default_currency': default_currency
        })

    return HttpResponseRedirect(reverse('sales_checkout_cart'))
Пример #2
0
def add_to_cart(request):
    """
    Add product to cart
    """
    product_id = int(request.POST['product_id'])

    # Checking if user already has cart in session
    # otherwise create a new cart for the user
    if 'cart_id' in request.session:
        cart_id = int(request.session['cart_id'])
        cart = Cart.get_cart(cart_id)
    else:
        cart = Cart.get_cart()
        request.session['cart_id'] = cart.id

    try:
        quantity = int(request.POST['quantity'])
        if quantity > 0:
            cart.add_item(product_id, quantity, request.user)
        else:
            raise ValueError()
    except ValueError:
        return HttpResponseBadRequest('Product quantity is not correct, please enter one or more products in numbers.')

    if request.is_ajax():
        default_currency = get_default_currency(request)
        return render(request, 'sales/cart_basket.html', {'cart': cart, 'default_currency': default_currency})

    return HttpResponseRedirect(reverse('sales_checkout_cart'))
Пример #3
0
def process_online(request, order_id, receipt_code):
    """
    Shows online or process online payment
    """
    if request.method == 'GET':
        form = None
        gateways = Gateway.get_gateways()
        order = get_object_or_404(Order,
                                  id=order_id,
                                  receipt_code=receipt_code)
        default_currency = get_default_currency(request)

        for gateway in gateways:
            if gateway.accept_credit_card:
                form = CreditCardForm(initial={'gateway': gateway})

        return render(
            request, 'payments/process_online.html', {
                'form': form,
                'order': order,
                'gateways': gateways,
                'default_currency': default_currency
            })

    raise Http404
Пример #4
0
def process_credit_card(request, order_id, receipt_code):
    """
    Process credit card payment
    """
    if request.method == 'POST':
        error = None
        form = CreditCardForm(request.POST)
        order = get_object_or_404(Order, id=order_id, receipt_code=receipt_code)

        if form.is_valid():
            data = form.cleaned_data
            gateway = data['gateway']

            if gateway.name == Gateway.PAYPAL:
                processor = PayPal(gateway)
            elif gateway.name == Gateway.STRIPE:
                processor = Stripe(gateway)
            else:
                raise ImproperlyConfigured('%s is not supported gateway for processing credit cards.' % gateway)

            try:
                processor.credit_card_payment(data['card'], order, request.user)
                return render(request, 'payments/credit_card_processed.html', {'order': order})
            except DoorsaleError as e:
                error = e.message

        gateways = Gateway.get_gateways()
        default_currency = get_default_currency(request)
        return render(request, 'payments/process_online.html',
                      {'form': form, 'order': order, 'gateways': gateways,
                       'default_currency': default_currency, 'error': error})

    raise Http404
Пример #5
0
def online_payment(request, order_id, receipt_code):   
    """
    Shows online or process online payment
    """
    order = get_object_or_404(Order, id=order_id, receipt_code=receipt_code)
    default_currency = get_default_currency(request)

    form = CreditCardForm()
    gateways = Gateway.get_gateways()
    return render(request, 'payments/online_payment.html', { 'form': form, 'order': order, 'gateways': gateways, 'default_currency': default_currency })
Пример #6
0
def remove_all_from_cart(request):
    """
    Remove all products from cart
    """
    if request.method == 'POST':
        if 'cart_id' in request.session:
            cart_id = int(request.session['cart_id'])
            cart = Cart.get_cart(cart_id)
            cart.remove_all_items()
        else:
            cart = Cart()

        if request.is_ajax():
            default_currency = get_default_currency(request)
            return render(request, 'sales/cart_basket.html', {'cart': cart, 'default_currency': default_currency})

    return HttpResponseRedirect(reverse('sales_checkout_cart'))
Пример #7
0
def credit_card_payment(request, order_id, receipt_code):
    """
    Process credit card payment
    """
    if request.method == 'POST':
        form = CreditCardForm(request.POST)
        order = get_object_or_404(Order, id=order_id, receipt_code=receipt_code)
        
        if form.is_valid():
            # Doorsale by default accept credit card payment via PayPal
            paypal = PayPal()
        else:
            gateways = Gateway.get_gateways()
            default_currency = get_default_currency(request)
            return render(request, 'payments/online_payment.html', { 'form': form, 'order': order, 'gateways': gateways, 'default_currency': default_currency })

    raise Http404
Пример #8
0
def remove_all_from_cart(request):
    """
    Remove all products from cart
    """
    if request.method == 'POST':
        if 'cart_id' in request.session:
            cart_id = int(request.session['cart_id'])
            cart = Cart.get_cart(cart_id)
            cart.remove_all_items()
        else:
            cart = Cart()

        if request.is_ajax():
            default_currency = get_default_currency(request)
            return render(request, 'sales/cart_basket.html', {'cart': cart, 'default_currency': default_currency})

    return HttpResponseRedirect(reverse('sales_checkout_cart'))
Пример #9
0
def remove_all_from_cart(request):
    """
    Remove all products from cart
    """
    if request.method == "POST":
        if "cart_id" in request.session:
            cart_id = int(request.session["cart_id"])
            cart = Cart.get_cart(cart_id)
            cart.remove_all_items()
        else:
            cart = Cart()

        if request.is_ajax():
            default_currency = get_default_currency(request)
            return render(request, "sales/cart_basket.html", {"cart": cart, "default_currency": default_currency})

    return HttpResponseRedirect(reverse("sales_checkout_cart"))
Пример #10
0
def process_online(request, order_id, receipt_code):
    """
    Shows online or process online payment
    """
    if request.method == 'GET':
        form = None
        gateways = Gateway.get_gateways()
        order = get_object_or_404(Order, id=order_id, receipt_code=receipt_code)
        default_currency = get_default_currency(request)

        for gateway in gateways:
            if gateway.accept_credit_card:
                form = CreditCardForm(initial={'gateway': gateway})

        return render(request, 'payments/process_online.html',
                      {'form': form, 'order': order, 'gateways': gateways, 'default_currency': default_currency})

    raise Http404
Пример #11
0
def process_credit_card(request, order_id, receipt_code):
    """
    Process credit card payment
    """
    if request.method == 'POST':
        error = None
        form = CreditCardForm(request.POST)
        order = get_object_or_404(Order,
                                  id=order_id,
                                  receipt_code=receipt_code)

        if form.is_valid():
            data = form.cleaned_data
            gateway = data['gateway']

            if gateway.name == Gateway.PAYPAL:
                processor = PayPal(gateway)
            elif gateway.name == Gateway.STRIPE:
                processor = Stripe(gateway)
            else:
                raise ImproperlyConfigured(
                    '%s is not supported gateway for processing credit cards.'
                    % gateway)

            try:
                processor.credit_card_payment(data['card'], order,
                                              request.user)
                return render(request, 'payments/credit_card_processed.html',
                              {'order': order})
            except DoorsaleError as e:
                error = e.message

        gateways = Gateway.get_gateways()
        default_currency = get_default_currency(request)
        return render(
            request, 'payments/process_online.html', {
                'form': form,
                'order': order,
                'gateways': gateways,
                'default_currency': default_currency,
                'error': error
            })

    raise Http404
Пример #12
0
def remove_from_cart(request):
    """
    Remove product from cart
    """
    product_id = int(request.POST['product_id'])

    # Checking if user session has cart or session may already flushed
    # Cart an empty cart for user
    if 'cart_id' in request.session:
        cart_id = int(request.session['cart_id'])
        cart = Cart.get_cart(cart_id)
        cart.remove_item(product_id)
    else:
        cart = Cart()

    if request.is_ajax():
        default_currency = get_default_currency(request)
        return render(request, 'sales/cart_basket.html', {'cart': cart, 'default_currency': default_currency})

    return HttpResponseRedirect(reverse('sales_checkout_cart'))
Пример #13
0
def remove_from_cart(request):
    """
    Remove product from cart
    """
    product_id = int(request.POST['product_id'])

    # Checking if user session has cart or session may already flushed
    # Cart an empty cart for user
    if 'cart_id' in request.session:
        cart_id = int(request.session['cart_id'])
        cart = Cart.get_cart(cart_id)
        cart.remove_item(product_id)
    else:
        cart = Cart()

    if request.is_ajax():
        default_currency = get_default_currency(request)
        return render(request, 'sales/cart_basket.html', {'cart': cart, 'default_currency': default_currency})

    return HttpResponseRedirect(reverse('sales_checkout_cart'))