Esempio n. 1
0
def changed_invoice_country(request):
    """
    """
    form = OnePageCheckoutForm(initial=request.POST)
    result = simplejson.dumps({
        "invoice_address": address_inline(request, INVOICE_PREFIX, form),
    })
    return HttpResponse(result)
Esempio n. 2
0
def changed_invoice_country(request):
    """
    """
    form = OnePageCheckoutForm(initial=request.POST)
    result = simplejson.dumps({
        "invoice_address":
        address_inline(request, INVOICE_PREFIX, form),
    })
    return HttpResponse(result)
Esempio n. 3
0
def changed_shipping_country(request):
    """
    """
    form = OnePageCheckoutForm(initial=request.POST)
    result = simplejson.dumps({
        "shipping_address": address_inline(request, SHIPPING_PREFIX, form),
    })

    return HttpResponse(result)
Esempio n. 4
0
def changed_shipping_country(request):
    """
    """
    form = OnePageCheckoutForm(initial=request.POST)
    result = simplejson.dumps({
        "shipping_address":
        address_inline(request, SHIPPING_PREFIX, form),
    })

    return HttpResponse(result)
Esempio n. 5
0
def one_page_checkout(request,
                      checkout_form=OnePageCheckoutForm,
                      template_name="muecke/checkout/one_page_checkout.html"):
    """One page checkout form.
    """
    # If the user is not authenticated and the if only authenticate checkout
    # allowed we rediret to authentication page.
    shop = muecke.core.utils.get_default_shop(request)
    if request.user.is_anonymous() and \
       shop.checkout_type == CHECKOUT_TYPE_AUTH:
        return HttpResponseRedirect(reverse("muecke_checkout_login"))

    customer = customer_utils.get_or_create_customer(request)
    if request.method == "POST":
        form = checkout_form(request.POST)
        toc = True

        if shop.confirm_toc:
            if "confirm_toc" not in request.POST:
                toc = False
                if form.errors is None:
                    form.errors = {}
                form.errors["confirm_toc"] = _(
                    u"Please confirm our terms and conditions")

        # Validate invoice address
        prefix = "invoice"
        country_iso = request.POST.get(prefix + "-country",
                                       shop.default_country.code)
        form_class = form_factory(country_iso)
        invoice_form = form_class(request.POST, prefix=prefix)

        # Validate shipping address
        valid_shipping_form = True
        if not request.POST.get("no_shipping"):
            prefix = "shipping"
            country_iso = request.POST.get(prefix + "-country",
                                           shop.default_country.code)
            form_class = form_factory(country_iso)
            shipping_form = form_class(request.POST, prefix=prefix)
            valid_shipping_form = shipping_form.is_valid()

        if form.is_valid() and invoice_form.is_valid(
        ) and valid_shipping_form and toc:
            # save invoice details
            customer.selected_invoice_address.firstname = request.POST.get(
                "invoice_firstname")
            customer.selected_invoice_address.lastname = request.POST.get(
                "invoice_lastname")
            customer.selected_invoice_address.phone = request.POST.get(
                "invoice_phone")
            customer.selected_invoice_address.email = request.POST.get(
                "invoice_email")
            customer.selected_invoice_address.company_name = request.POST.get(
                "invoice_company_name")

            # Create or update invoice address
            valid_invoice_address = save_address(request, customer,
                                                 INVOICE_PREFIX)
            if valid_invoice_address == False:
                form._errors.setdefault(
                    NON_FIELD_ERRORS,
                    ErrorList([_(u"Invalid invoice address")]))
            else:
                # If the shipping address differs from invoice firstname we create
                # or update the shipping address.
                valid_shipping_address = True
                if not form.cleaned_data.get("no_shipping"):
                    # save shipping details
                    customer.selected_shipping_address.firstname = request.POST.get(
                        "shipping_firstname")
                    customer.selected_shipping_address.lastname = request.POST.get(
                        "shipping_lastname")
                    customer.selected_shipping_address.phone = request.POST.get(
                        "shipping_phone")
                    customer.selected_shipping_address.email = request.POST.get(
                        "shipping_email")
                    customer.selected_shipping_address.company_name = request.POST.get(
                        "shipping_company_name")

                    valid_shipping_address = save_address(
                        request, customer, SHIPPING_PREFIX)

                if valid_shipping_address == False:
                    form._errors.setdefault(
                        NON_FIELD_ERRORS,
                        ErrorList([_(u"Invalid shipping address")]))
                else:
                    # Payment method
                    customer.selected_payment_method_id = request.POST.get(
                        "payment_method")

                    if int(form.data.get("payment_method")) == DIRECT_DEBIT:
                        bank_account = BankAccount.objects.create(
                            account_number=form.cleaned_data.get(
                                "account_number"),
                            bank_identification_code=form.cleaned_data.get(
                                "bank_identification_code"),
                            bank_name=form.cleaned_data.get("bank_name"),
                            depositor=form.cleaned_data.get("depositor"),
                        )

                        customer.selected_bank_account = bank_account

                    # Save the selected information to the customer
                    customer.save()

                    # process the payment method ...
                    result = muecke.payment.utils.process_payment(request)

                    if result["accepted"]:
                        return HttpResponseRedirect(
                            result.get("next_url",
                                       reverse("muecke_thank_you")))
                    else:
                        if "message" in result:
                            form._errors[result.get(
                                "message_location")] = result.get("message")
        else:  # form is not valid
            # save invoice details
            customer.selected_invoice_address.firstname = request.POST.get(
                "invoice_firstname")
            customer.selected_invoice_address.lastname = request.POST.get(
                "invoice_lastname")
            customer.selected_invoice_address.phone = request.POST.get(
                "invoice_phone")
            customer.selected_invoice_address.email = request.POST.get(
                "invoice_email")
            customer.selected_invoice_address.company_name = request.POST.get(
                "invoice_company_name")

            # If the shipping address differs from invoice firstname we create
            # or update the shipping address.
            if not form.data.get("no_shipping"):
                # save shipping details
                customer.selected_shipping_address.firstname = request.POST.get(
                    "shipping_firstname")
                customer.selected_shipping_address.lastname = request.POST.get(
                    "shipping_lastname")
                customer.selected_shipping_address.phone = request.POST.get(
                    "shipping_phone")
                customer.selected_shipping_address.email = request.POST.get(
                    "shipping_email")
                customer.selected_shipping_address.company_name = request.POST.get(
                    "shipping_company_name")
                customer.save()

            # Payment method
            customer.selected_payment_method_id = request.POST.get(
                "payment_method")

            # 1 = Direct Debit
            if customer.selected_payment_method_id:
                if int(customer.selected_payment_method_id) == DIRECT_DEBIT:
                    bank_account = BankAccount.objects.create(
                        account_number=form.data.get("account_number"),
                        bank_identification_code=form.data.get(
                            "bank_identification_code"),
                        bank_name=form.data.get("bank_name"),
                        depositor=form.data.get("depositor"),
                    )

                    customer.selected_bank_account = bank_account

            # Save the selected information to the customer
            customer.save()

    else:
        # If there are addresses intialize the form.
        initial = {}
        invoice_address = customer.selected_invoice_address
        initial.update({
            "invoice_firstname": invoice_address.firstname,
            "invoice_lastname": invoice_address.lastname,
            "invoice_phone": invoice_address.phone,
            "invoice_email": invoice_address.email,
            "invoice_country": invoice_address.country,
            "invoice_company_name": invoice_address.company_name,
        })
        shipping_address = customer.selected_shipping_address
        initial.update({
            "shipping_firstname": shipping_address.firstname,
            "shipping_lastname": shipping_address.lastname,
            "shipping_phone": shipping_address.phone,
            "shipping_email": shipping_address.email,
            "shipping_company_name": shipping_address.company_name,
            "no_shipping": True,
        })
        form = checkout_form(initial=initial)
    cart = cart_utils.get_cart(request)
    if cart is None:
        return HttpResponseRedirect(reverse('muecke_cart'))

    # Payment
    try:
        selected_payment_method_id = request.POST.get("payment_method")
        selected_payment_method = PaymentMethod.objects.get(
            pk=selected_payment_method_id)
    except PaymentMethod.DoesNotExist:
        selected_payment_method = muecke.payment.utils.get_selected_payment_method(
            request)

    valid_payment_methods = muecke.payment.utils.get_valid_payment_methods(
        request)
    valid_payment_method_ids = [m.id for m in valid_payment_methods]

    display_bank_account = any([
        pm.type == muecke.payment.settings.PM_BANK
        for pm in valid_payment_methods
    ])
    display_credit_card = any([
        pm.type == muecke.payment.settings.PM_CREDIT_CARD
        for pm in valid_payment_methods
    ])

    response = render_to_response(
        template_name,
        RequestContext(
            request, {
                "form":
                form,
                "cart_inline":
                cart_inline(request),
                "shipping_inline":
                shipping_inline(request),
                "invoice_address_inline":
                address_inline(request, INVOICE_PREFIX, form),
                "shipping_address_inline":
                address_inline(request, SHIPPING_PREFIX, form),
                "payment_inline":
                payment_inline(request, form),
                "selected_payment_method":
                selected_payment_method,
                "display_bank_account":
                display_bank_account,
                "display_credit_card":
                display_credit_card,
                "voucher_number":
                muecke.voucher.utils.get_current_voucher_number(request),
                "settings":
                settings,
            }))

    return response
Esempio n. 6
0
def one_page_checkout(request, checkout_form=OnePageCheckoutForm,
    template_name="muecke/checkout/one_page_checkout.html"):
    """One page checkout form.
    """
    # If the user is not authenticated and the if only authenticate checkout
    # allowed we rediret to authentication page.
    shop = muecke.core.utils.get_default_shop(request)
    if request.user.is_anonymous() and \
       shop.checkout_type == CHECKOUT_TYPE_AUTH:
        return HttpResponseRedirect(reverse("muecke_checkout_login"))

    customer = customer_utils.get_or_create_customer(request)
    if request.method == "POST":
        form = checkout_form(request.POST)
        toc = True

        if shop.confirm_toc:
            if "confirm_toc" not in request.POST:
                toc = False
                if form.errors is None:
                    form.errors = {}
                form.errors["confirm_toc"] = _(u"Please confirm our terms and conditions")

        # Validate invoice address
        prefix="invoice"
        country_iso = request.POST.get(prefix + "-country", shop.default_country.code)
        form_class = form_factory(country_iso)
        invoice_form = form_class(request.POST, prefix=prefix)

        # Validate shipping address
        valid_shipping_form = True
        if not request.POST.get("no_shipping"):
            prefix="shipping"
            country_iso = request.POST.get(prefix + "-country", shop.default_country.code)
            form_class = form_factory(country_iso)
            shipping_form = form_class(request.POST, prefix=prefix)
            valid_shipping_form = shipping_form.is_valid()

        if form.is_valid() and invoice_form.is_valid() and valid_shipping_form and toc:
            # save invoice details
            customer.selected_invoice_address.firstname = request.POST.get("invoice_firstname")
            customer.selected_invoice_address.lastname = request.POST.get("invoice_lastname")
            customer.selected_invoice_address.phone = request.POST.get("invoice_phone")
            customer.selected_invoice_address.email = request.POST.get("invoice_email")
            customer.selected_invoice_address.company_name = request.POST.get("invoice_company_name")

            # Create or update invoice address
            valid_invoice_address = save_address(request, customer, INVOICE_PREFIX)
            if valid_invoice_address == False:
                form._errors.setdefault(NON_FIELD_ERRORS, ErrorList([_(u"Invalid invoice address")]))
            else:
                # If the shipping address differs from invoice firstname we create
                # or update the shipping address.
                valid_shipping_address = True
                if not form.cleaned_data.get("no_shipping"):
                    # save shipping details
                    customer.selected_shipping_address.firstname = request.POST.get("shipping_firstname")
                    customer.selected_shipping_address.lastname = request.POST.get("shipping_lastname")
                    customer.selected_shipping_address.phone = request.POST.get("shipping_phone")
                    customer.selected_shipping_address.email = request.POST.get("shipping_email")
                    customer.selected_shipping_address.company_name = request.POST.get("shipping_company_name")

                    valid_shipping_address = save_address(request, customer, SHIPPING_PREFIX)

                if valid_shipping_address == False:
                    form._errors.setdefault(NON_FIELD_ERRORS, ErrorList([_(u"Invalid shipping address")]))
                else:
                    # Payment method
                    customer.selected_payment_method_id = request.POST.get("payment_method")

                    if int(form.data.get("payment_method")) == DIRECT_DEBIT:
                        bank_account = BankAccount.objects.create(
                            account_number=form.cleaned_data.get("account_number"),
                            bank_identification_code=form.cleaned_data.get("bank_identification_code"),
                            bank_name=form.cleaned_data.get("bank_name"),
                            depositor=form.cleaned_data.get("depositor"),
                        )

                        customer.selected_bank_account = bank_account

                    # Save the selected information to the customer
                    customer.save()

                    # process the payment method ...
                    result = muecke.payment.utils.process_payment(request)

                    if result["accepted"]:
                        return HttpResponseRedirect(result.get("next_url", reverse("muecke_thank_you")))
                    else:
                        if "message" in result:
                            form._errors[result.get("message_location")] = result.get("message")
        else:  # form is not valid
            # save invoice details
            customer.selected_invoice_address.firstname = request.POST.get("invoice_firstname")
            customer.selected_invoice_address.lastname = request.POST.get("invoice_lastname")
            customer.selected_invoice_address.phone = request.POST.get("invoice_phone")
            customer.selected_invoice_address.email = request.POST.get("invoice_email")
            customer.selected_invoice_address.company_name = request.POST.get("invoice_company_name")

            # If the shipping address differs from invoice firstname we create
            # or update the shipping address.
            if not form.data.get("no_shipping"):
                # save shipping details
                customer.selected_shipping_address.firstname = request.POST.get("shipping_firstname")
                customer.selected_shipping_address.lastname = request.POST.get("shipping_lastname")
                customer.selected_shipping_address.phone = request.POST.get("shipping_phone")
                customer.selected_shipping_address.email = request.POST.get("shipping_email")
                customer.selected_shipping_address.company_name = request.POST.get("shipping_company_name")
                customer.save()

            # Payment method
            customer.selected_payment_method_id = request.POST.get("payment_method")

            # 1 = Direct Debit
            if customer.selected_payment_method_id:
                if int(customer.selected_payment_method_id) == DIRECT_DEBIT:
                    bank_account = BankAccount.objects.create(
                        account_number=form.data.get("account_number"),
                        bank_identification_code=form.data.get("bank_identification_code"),
                        bank_name=form.data.get("bank_name"),
                        depositor=form.data.get("depositor"),
                    )

                    customer.selected_bank_account = bank_account

            # Save the selected information to the customer
            customer.save()

    else:
        # If there are addresses intialize the form.
        initial = {}
        invoice_address = customer.selected_invoice_address
        initial.update({
            "invoice_firstname": invoice_address.firstname,
            "invoice_lastname": invoice_address.lastname,
            "invoice_phone": invoice_address.phone,
            "invoice_email": invoice_address.email,
            "invoice_country": invoice_address.country,
            "invoice_company_name": invoice_address.company_name,
        })
        shipping_address = customer.selected_shipping_address
        initial.update({
            "shipping_firstname": shipping_address.firstname,
            "shipping_lastname": shipping_address.lastname,
            "shipping_phone": shipping_address.phone,
            "shipping_email": shipping_address.email,
            "shipping_company_name": shipping_address.company_name,
            "no_shipping": True,
        })
        form = checkout_form(initial=initial)
    cart = cart_utils.get_cart(request)
    if cart is None:
        return HttpResponseRedirect(reverse('muecke_cart'))

    # Payment
    try:
        selected_payment_method_id = request.POST.get("payment_method")
        selected_payment_method = PaymentMethod.objects.get(pk=selected_payment_method_id)
    except PaymentMethod.DoesNotExist:
        selected_payment_method = muecke.payment.utils.get_selected_payment_method(request)

    valid_payment_methods = muecke.payment.utils.get_valid_payment_methods(request)
    valid_payment_method_ids = [m.id for m in valid_payment_methods]

    display_bank_account = any([pm.type == muecke.payment.settings.PM_BANK for pm in valid_payment_methods])
    display_credit_card = any([pm.type == muecke.payment.settings.PM_CREDIT_CARD for pm in valid_payment_methods])

    response = render_to_response(template_name, RequestContext(request, {
        "form": form,
        "cart_inline": cart_inline(request),
        "shipping_inline": shipping_inline(request),
        "invoice_address_inline": address_inline(request, INVOICE_PREFIX, form),
        "shipping_address_inline": address_inline(request, SHIPPING_PREFIX, form),
        "payment_inline": payment_inline(request, form),
        "selected_payment_method": selected_payment_method,
        "display_bank_account": display_bank_account,
        "display_credit_card": display_credit_card,
        "voucher_number": muecke.voucher.utils.get_current_voucher_number(request),
        "settings": settings,
    }))

    return response