Commits

NikolayBlohin committed bd26f90

Fixed checkout forms + patch

Comments (0)

Files changed (4)

lfs/catalog/views.py

     result = simplejson.dumps({
         "product" : product_inline(request, variant.id),
         "message" : msg,
+        'price': variant.price,
     }, cls = LazyEncoder)
 
     return HttpResponse(result)

lfs/checkout/forms.py

 from django.utils.translation import ugettext_lazy as _
 
 # lfs imports
-import lfs.payment.settings
-from lfs.payment.models import PaymentMethod
+from lfs.payment.settings import CREDIT_CARD_TYPE_CHOICES
 from lfs.core.utils import get_default_shop
-from lfs.core.models import Country
 
 class OnePageCheckoutForm(forms.Form):
     """
     """
-    invoice_firstname = forms.CharField(label=_(u"First Name"), max_length=50)
-    invoice_lastname = forms.CharField(label=_(u"Last Name"), max_length=50)
-    invoice_phone = forms.CharField(label=_(u"Invoice Phone"), max_length=20, required=False)
-    invoice_email = forms.EmailField(label=_(u"Invoice E-mail"), required=False, max_length=50)
+    invoice_firstname = forms.CharField(label=_(u"Firstname"), max_length=50)
+    invoice_lastname = forms.CharField(label=_(u"Lastname"), max_length=50)
+    invoice_company_name = forms.CharField(label=_(u"Company name"), required=False, max_length=50)
+    invoice_street = forms.CharField(label=_(u"Street"), max_length=100)
+    invoice_zip_code = forms.CharField(label=_(u"Zip Code"), max_length=10)
+    invoice_city = forms.CharField(label=_(u"City"), max_length=50)
+    invoice_country = forms.ChoiceField(label=_(u"Country"))
+    invoice_phone = forms.CharField(label=_(u"Phone"), max_length=20)
+    invoice_email = forms.EmailField(label=_(u"E-mail"), required=False, max_length=50)
     
-    shipping_firstname = forms.CharField(label=_(u"First Name"), required=False, max_length=50)
-    shipping_lastname = forms.CharField(label=_(u"Last Name"), required=False, max_length=50)
-    shipping_phone = forms.CharField(label=_(u"Shipping Phone"), required=False, max_length=20)
-    shipping_email = forms.EmailField(label=_(u"Shipping E-mail"), required=False, max_length=50)
-
-    requested_delivery_date = forms.DateField(label=_(u"Requested Delivery Date"), required=False)
+    shipping_firstname = forms.CharField(label=_(u"Firstname"), required=False, max_length=50)
+    shipping_lastname = forms.CharField(label=_(u"Lastname"), required=False, max_length=50)
+    shipping_company_name = forms.CharField(label=_(u"Company name"), required=False, max_length=50)
+    shipping_street = forms.CharField(label=_(u"Street"), required=False, max_length=100)
+    shipping_zip_code = forms.CharField(label=_(u"Zip Code"), required=False, max_length=10)
+    shipping_city = forms.CharField(label=_(u"City"), required=False, max_length=50)
+    shipping_country = forms.ChoiceField(label=_(u"Country"), required=False)
+    shipping_phone = forms.CharField(label=_(u"Phone"), required=False, max_length=20)
 
     account_number = forms.CharField(label=_(u"Account Number"), required=False, max_length=30)
     bank_identification_code = forms.CharField(label=_(u"Bank Indentification Code"), required=False, max_length=30)
     bank_name = forms.CharField(label=_(u"Bankname"), required=False, max_length=100)
     depositor = forms.CharField(label=_(u"Depositor"), required=False, max_length=100)
-
+    
     payment_method = forms.CharField(required=False, max_length=1)
-
-    credit_card_type = forms.ChoiceField(label=_(u"Credit Card Type"), choices=lfs.payment.settings.CREDIT_CARD_TYPE_CHOICES, required=False)
+    
+    credit_card_type = forms.ChoiceField(label=_(u"Credit Card Type"), choices=CREDIT_CARD_TYPE_CHOICES, required=False)
     credit_card_owner = forms.CharField(label=_(u"Credit Card Owner"), max_length=100, required=False)
     credit_card_number = forms.CharField(label=_(u"Credit Card Number"), max_length=30, required=False)
     credit_card_expiration_date_month = forms.ChoiceField(label=_(u"Expiration Date Month"), required=False)
     credit_card_expiration_date_year = forms.ChoiceField(label=_(u"Expiration Date Year"), required=False)
     credit_card_verification = forms.CharField(label=_(u"Verification Number"), max_length=4, required=False, widget=forms.TextInput(attrs={"size" : 4}))
-
+    
     no_shipping = forms.BooleanField(label=_(u"Same as invoice"), initial=True, required=False)
     message = forms.CharField(label=_(u"Your message to us"), widget=forms.Textarea(attrs={'cols':'80;'}), required=False)
 
     def __init__(self, *args, **kwargs):
         super(OnePageCheckoutForm, self).__init__(*args, **kwargs)
         
+        shop = get_default_shop()
+        self.fields["invoice_country"].choices = [(c.id, c.name) for c in shop.invoice_countries.all()]
+        self.fields["shipping_country"].choices = [(c.id, c.name) for c in shop.shipping_countries.all()]
+        
         year = datetime.now().year
         self.fields["credit_card_expiration_date_month"].choices = [(i, i) for i in range(1, 13)]
         self.fields["credit_card_expiration_date_year"].choices = [(i, i) for i in range(year, year+10)]
-
+        
     def clean(self):
         """
         """
         msg = _(u"This field is required.")
-
+        
         if self.data.get("is_anonymous") == "1" and \
            not self.cleaned_data.get("invoice_email"):
             self._errors["invoice_email"] = ErrorList([msg])
             if self.cleaned_data.get("shipping_lastname", "") == "":
                 self._errors["shipping_lastname"] = ErrorList([msg])
 
-        # check that shipping country is in the shops shipping countries list
-        shop = get_default_shop()
-        shipping_countries = shop.shipping_countries.all()
-        shipping_country = None
-        if not self.cleaned_data.get("no_shipping"):
-            shipping_country_code = self.data.get("shipping-country", None)
-            if shipping_country_code:
-                shipping_country = Country.objects.get(code=shipping_country_code.lower())
-        else:
-            shipping_country_code = self.data.get("invoice-country", None)
-            if shipping_country_code:
-                shipping_country = Country.objects.get(code=shipping_country_code.lower())
-        if shipping_country:
-            if shipping_country not in shipping_countries:
-                msg = _(u"Invalid shipping country.")
-                #self._errors["all"] = ErrorList([msg])
-                raise forms.ValidationError("Invalid Shipping Country")
+            if self.cleaned_data.get("shipping_street", "") == "":
+                self._errors["shipping_street"] = ErrorList([msg])
 
-		# Check data of selected payment method        
-        payment_method_id = self.data.get("payment_method")        
-        payment_method = PaymentMethod.objects.get(pk=payment_method_id)
+            if self.cleaned_data.get("shipping_zip_code", "") == "":
+                self._errors["shipping_zip_code"] = ErrorList([msg])
 
-        if payment_method.type == lfs.payment.settings.PM_BANK:
+            if self.cleaned_data.get("shipping_city", "") == "":
+                self._errors["shipping_city"] = ErrorList([msg])
+
+            if self.cleaned_data.get("shipping_country", "") == "":
+                self._errors["shipping_country"] = ErrorList([msg])
+                
+        # 1 == Direct Debit
+        if self.data.get("payment_method") == "1":
             if self.cleaned_data.get("account_number", "") == "":
                 self._errors["account_number"] = ErrorList([msg])
-
+            
             if self.cleaned_data.get("bank_identification_code", "") == "":
                 self._errors["bank_identification_code"] = ErrorList([msg])
 
 
             if self.cleaned_data.get("depositor", "") == "":
                 self._errors["depositor"] = ErrorList([msg])
-
-        elif payment_method.type == lfs.payment.settings.PM_CREDIT_CARD:
+        # 6 == Credit Card
+        elif self.data.get("payment_method") == "6":
             if self.cleaned_data.get("credit_card_owner", "") == "":
                 self._errors["credit_card_owner"] = ErrorList([msg])
-
+            
             if self.cleaned_data.get("credit_card_number", "") == "":
                 self._errors["credit_card_number"] = ErrorList([msg])
-
+            
             if self.cleaned_data.get("credit_card_verification", "") == "":
                 self._errors["credit_card_verification"] = ErrorList([msg])
-
-        return self.cleaned_data
+        
+        return self.cleaned_data

lfs/checkout/settings.py

     (CHECKOUT_TYPE_AUTH,   _(u"Authenticated only")),
 )
 SHIPPING_PREFIX ="shipping"
-INVOICE_PREFIX = "invoice"
+INVOICE_PREFIX = "invoice"

lfs/checkout/views.py

+
 # django imports
 from django.conf import settings
 from django.contrib.auth.forms import AuthenticationForm
 from lfs.payment.settings import CREDIT_CARD
 from lfs.voucher.models import Voucher
 from lfs.voucher.settings import MESSAGES
+# django imports
+from django.conf import settings
+from django.contrib.auth.forms import AuthenticationForm
+from django.contrib.auth.models import User
+from django.core.urlresolvers import reverse
+from django.http import HttpResponse
+from django.http import HttpResponseRedirect
+from django.shortcuts import render_to_response
+from django.template.loader import render_to_string
+from django.template import RequestContext
+from django.utils import simplejson
+from django.utils.translation import ugettext_lazy as _
 
-# other imports
-from postal.library import form_factory
+# lfs imports
+import lfs.core.utils
+import lfs.order.utils
+import lfs.payment.utils
+import lfs.shipping.utils
+from lfs.cart import utils as cart_utils
+from lfs.checkout.forms import OnePageCheckoutForm
+from lfs.checkout.settings import CHECKOUT_TYPE_ANON
+from lfs.checkout.settings import CHECKOUT_TYPE_AUTH
+from lfs.customer import utils as customer_utils
+from lfs.customer.models import Address
+from lfs.customer.models import BankAccount
+from lfs.customer.forms import RegisterForm
+from lfs.payment.models import PaymentMethod
+from lfs.payment.settings import PAYPAL
+from lfs.payment.settings import DIRECT_DEBIT
+from lfs.payment.settings import CREDIT_CARD
 
 def login(request, template_name="lfs/checkout/login.html"):
     """Displays a form to login or register/login the user within the check out
     selected_payment_method = lfs.payment.utils.get_selected_payment_method(request)
     payment_costs = lfs.payment.utils.get_payment_costs(request, selected_payment_method)
 
-    # Cart costs
+    # cart costs
     cart_costs = cart_utils.get_cart_costs(request, cart)
     cart_price = cart_costs["price"] + shipping_costs["price"] + payment_costs["price"]
     cart_tax = cart_costs["tax"] + shipping_costs["tax"] + payment_costs["tax"]
-    
-    discounts = lfs.discounts.utils.get_valid_discounts(request)
-    for discount in discounts:
-        cart_price = cart_price - discount["price"]
-        cart_tax = cart_tax - discount["tax"]
-
-    # Voucher
-    try:
-        voucher_number = lfs.voucher.utils.get_current_voucher_number(request)
-        voucher = Voucher.objects.get(number=voucher_number)
-    except Voucher.DoesNotExist:
-        display_voucher = False
-        voucher_value = 0
-        voucher_tax = 0
-        voucher_message = MESSAGES[6]
-    else:
-        lfs.voucher.utils.set_current_voucher_number(request, voucher_number)
-        is_voucher_effective, voucher_message = voucher.is_effective(cart)
-        if is_voucher_effective:
-            display_voucher = True
-            voucher_value = voucher.get_price_gross(cart)
-            cart_price = cart_price - voucher_value
-            voucher_tax = voucher.get_tax(cart)
-        else:
-            display_voucher = False
-            voucher_value = 0
-            voucher_tax = 0
 
     return render_to_string(template_name, RequestContext(request, {
         "cart" : cart,
         "cart_price" : cart_price,
         "cart_tax" : cart_tax,
-        "display_voucher" : display_voucher,
-        "discounts" : discounts,        
-        "voucher_value" : voucher_value,
-        "voucher_tax" : voucher_tax,
         "shipping_price" : shipping_costs["price"],
         "payment_price" : payment_costs["price"],
         "selected_shipping_method" : selected_shipping_method,
         "selected_payment_method" : selected_payment_method,
-        "voucher_number" : voucher_number,
-        "voucher_message" : voucher_message,
     }))
 
 def one_page_checkout(request, checkout_form = OnePageCheckoutForm,
     customer = customer_utils.get_or_create_customer(request)
     if request.method == "POST":
         form = checkout_form(request.POST)
+        if form.is_valid():
+            # Create or update invoice address
+            if customer.selected_invoice_address is None:
+                invoice_address = Address.objects.create(
+                    firstname = form.cleaned_data.get("invoice_firstname"),
+                    lastname = form.cleaned_data.get("invoice_lastname"),
+                    company_name = form.cleaned_data.get("invoice_company_name"),
+                    street = form.cleaned_data.get("invoice_street"),
+                    zip_code = form.cleaned_data.get("invoice_zip_code"),
+                    city = form.cleaned_data.get("invoice_city"),
+                    country_id = form.cleaned_data.get("invoice_country"),
+                    phone = form.cleaned_data.get("invoice_phone"),
+                    email = form.cleaned_data.get("invoice_email"),
+                )
+                customer.selected_invoice_address = invoice_address
+            else:
+                selected_invoice_address = customer.selected_invoice_address
+                selected_invoice_address.firstname = form.cleaned_data.get("invoice_firstname")
+                selected_invoice_address.lastname = form.cleaned_data.get("invoice_lastname")
+                selected_invoice_address.company_name = form.cleaned_data.get("invoice_company_name")
+                selected_invoice_address.street = form.cleaned_data.get("invoice_street")
+                selected_invoice_address.zip_code = form.cleaned_data.get("invoice_zip_code")
+                selected_invoice_address.city = form.cleaned_data.get("invoice_city")
+                selected_invoice_address.country_id = form.cleaned_data.get("invoice_country")
+                selected_invoice_address.phone = form.cleaned_data.get("invoice_phone")
+                selected_invoice_address.email = form.cleaned_data.get("invoice_email")
+                selected_invoice_address.save()
 
-        toc = True
-                
-        if shop.confirm_toc:
-            if not request.POST.has_key("confirm_toc"):
-                toc = False
-                if form._errors is None:
-                    form._errors = {}
-                form._errors["confirm_toc"] = _(u"Please confirm our terms and conditions")
+            # If the shipping address differs from invoice firstname we create
+            # or update the shipping address.
+            if not form.cleaned_data.get("no_shipping"):
+                if customer.selected_shipping_address is None:
+                    shipping_address = Address.objects.create(
+                        firstname = form.cleaned_data.get("shipping_firstname"),
+                        lastname = form.cleaned_data.get("shipping_lastname"),
+                        company_name = form.cleaned_data.get("shipping_company_name"),
+                        street = form.cleaned_data.get("shipping_street"),
+                        zip_code = form.cleaned_data.get("shipping_zip_code"),
+                        city = form.cleaned_data.get("shipping_city"),
+                        country_id = form.cleaned_data.get("shipping_country"),
+                        phone = form.cleaned_data.get("shipping_phone"),
+                        email = form.cleaned_data.get("shipping_email"),
+                    )
+                    customer.selected_shipping_address = shipping_address
+                else:
+                    selected_shipping_address = customer.selected_shipping_address
+                    selected_shipping_address.firstname = form.cleaned_data.get("shipping_firstname")
+                    selected_shipping_address.lastname = form.cleaned_data.get("shipping_lastname")
+                    selected_shipping_address.company_name = form.cleaned_data.get("shipping_company_name")
+                    selected_shipping_address.street = form.cleaned_data.get("shipping_street")
+                    selected_shipping_address.zip_code = form.cleaned_data.get("shipping_zip_code")
+                    selected_shipping_address.city = form.cleaned_data.get("shipping_city")
+                    selected_shipping_address.country_id = form.cleaned_data.get("shipping_country")
+                    selected_shipping_address.phone = form.cleaned_data.get("shipping_phone")
+                    selected_shipping_address.save()
+                    
+            # Payment method
+            customer.selected_payment_method_id = request.POST.get("payment_method")
 
-        if toc and form.is_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")
+            # 1 = Direct Debit
+            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 = lfs.payment.utils.process_payment(request)
+
+            payment_method = lfs.payment.utils.get_selected_payment_method(request)
+
+            # Only if the payment is succesful we create the order out of the
+            # cart.
+            if result.get("success") == True:
+                order = lfs.order.utils.add_order(request)
+
+                # TODO: Get rid of these payment specific payment stuff. This
+                # should be within payment utils.
+                if payment_method.id == PAYPAL and settings.LFS_PAYPAL_REDIRECT:
+                    return HttpResponseRedirect(order.get_pay_link())
+                else:
+                    return HttpResponseRedirect(result.get("next-url"))
+            else:
+                if result.has_key("message"):
+                    form._errors[result.get("message-key")] = result.get("message")
+        
+        else: # form is not valid
             # Create or update invoice address
-            valid_invoice_address = save_address(request, customer, INVOICE_PREFIX)
-            if valid_invoice_address == False:
-                form._errors["invoice-address"] = ErrorList([_(u"Invalid invoice address")])
+            if customer.selected_invoice_address is None:
+                invoice_address = Address.objects.create(
+                    firstname = form.data.get("invoice_firstname"),
+                    lastname = form.data.get("invoice_lastname"),
+                    company_name = form.data.get("invoice_company_name"),
+                    street = form.data.get("invoice_street"),
+                    zip_code = form.data.get("invoice_zip_code"),
+                    city = form.data.get("invoice_city"),
+                    country_id = form.data.get("invoice_country"),
+                    phone = form.data.get("invoice_phone"),
+                    email = form.data.get("invoice_email"),
+                )
+                customer.selected_invoice_address = 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")
-
-                    valid_shipping_address = save_address(request, customer, SHIPPING_PREFIX)
-
-                if valid_shipping_address == False:
-                    form._errors["shipping-address"] = 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 = lfs.payment.utils.process_payment(request)
-
-					next_url = None
-					if result["accepted"] == True:
-					    return HttpResponseRedirect(
-					        result.get("next-url", reverse("lfs_thank_you")))
-					else:
-					    if result.has_key("message"):
-					        form._errors[result.get("message-position")] = 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")
-
-            # Create or update invoice address
-            save_address(request, customer, INVOICE_PREFIX)
+                selected_invoice_address = customer.selected_invoice_address
+                selected_invoice_address.firstname = form.data.get("invoice_firstname")
+                selected_invoice_address.lastname = form.data.get("invoice_lastname")
+                selected_invoice_address.company_name = form.data.get("invoice_company_name")
+                selected_invoice_address.street = form.data.get("invoice_street")
+                selected_invoice_address.zip_code = form.data.get("invoice_zip_code")
+                selected_invoice_address.city = form.data.get("invoice_city")
+                selected_invoice_address.country_id = form.data.get("invoice_country")
+                selected_invoice_address.phone = form.data.get("invoice_phone")
+                selected_invoice_address.email = form.data.get("invoice_email")
+                selected_invoice_address.save()
 
             # 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.save()
-
-                save_address(request, customer, SHIPPING_PREFIX)
-
+                if customer.selected_shipping_address is None:
+                    shipping_address = Address.objects.create(
+                        firstname = form.data.get("shipping_firstname"),
+                        lastname = form.data.get("shipping_lastname"),
+                        company_name = form.data.get("shipping_company_name"),
+                        street = form.data.get("shipping_street"),
+                        zip_code = form.data.get("shipping_zip_code"),
+                        city = form.data.get("shipping_city"),
+                        country_id = form.data.get("shipping_country"),
+                        phone = form.data.get("shipping_phone"),
+                        email = form.data.get("shipping_email"),
+                    )
+                    customer.selected_shipping_address = shipping_address
+                else:
+                    selected_shipping_address = customer.selected_shipping_address
+                    selected_shipping_address.firstname = form.data.get("shipping_firstname")
+                    selected_shipping_address.lastname = form.data.get("shipping_lastname")
+                    selected_shipping_address.company_name = form.data.get("shipping_company_name")
+                    selected_shipping_address.street = form.data.get("shipping_street")
+                    selected_shipping_address.zip_code = form.data.get("shipping_zip_code")
+                    selected_shipping_address.city = form.data.get("shipping_city")
+                    selected_shipping_address.country_id = form.data.get("shipping_country")
+                    selected_shipping_address.phone = form.data.get("shipping_phone")
+                    selected_shipping_address.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"),
-                    )
+            if int(form.data.get("payment_method")) == 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
+                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,
-        })
-        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,
-            "no_shipping" : False,
-        })
+        if customer.selected_invoice_address is not None:
+            invoice_address = customer.selected_invoice_address
+            initial.update({
+                "invoice_firstname" : invoice_address.firstname,
+                "invoice_lastname" : invoice_address.lastname,
+                "invoice_street" : invoice_address.street,
+                "invoice_zip_code" : invoice_address.zip_code,
+                "invoice_city" : invoice_address.city,
+                "invoice_country" : invoice_address.country_id,
+                "invoice_phone" : invoice_address.phone,
+                "invoice_email" : invoice_address.email,
+            })
+        if customer.selected_shipping_address is not None:
+            shipping_address = customer.selected_shipping_address
+            initial.update({
+                "shipping_firstname" : shipping_address.firstname,
+                "shipping_lastname" : shipping_address.lastname,
+                "shipping_street" : shipping_address.street,
+                "shipping_zip_code" : shipping_address.zip_code,
+                "shipping_city" : shipping_address.city,
+                "shipping_phone" : shipping_address.phone,
+                "shipping_email" : shipping_address.email,
+                "no_shipping" : False,
+            })
+
+        # Set the addresses country to the current selected in any case.
+        country = lfs.shipping.utils.get_selected_shipping_country(request)
+        initial["shipping_country"] = country.id
+        initial["invoice_country"] = country.id
         form = checkout_form(initial=initial)
+
     cart = cart_utils.get_cart(request)
     if cart is None:
         return HttpResponseRedirect(reverse('lfs_cart'))
         "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" : lfs.voucher.utils.get_current_voucher_number(request),
     }))
 
     if form._errors:
-        return lfs.core.utils.set_message_to(response, _(u"An error has occured."))
+        return lfs.core.utils.set_message_to(response, _(u"An error has been occured."))
     else:
         return response
 
     Factored out to be reusable for the starting request (which renders the
     whole checkout page and subsequent ajax requests which refresh the
     selectable payment methods.
-
+    
     Passing the form to be able to display payment forms within the several
     payment methods, e.g. credit card form.
     """
-    selected_payment_method = lfs.payment.utils.get_selected_payment_method(request)
+    # 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 = lfs.payment.utils.get_selected_payment_method(request)
+
     valid_payment_methods = lfs.payment.utils.get_valid_payment_methods(request)
+    display_bank_account = DIRECT_DEBIT in [m.id for m in valid_payment_methods]
 
     return render_to_string(template_name, RequestContext(request, {
         "payment_methods" : valid_payment_methods,
         "selected_shipping_method" : selected_shipping_method,
     }))
 
+def changed_checkout(request):
+    """
+    """
+    form = OnePageCheckoutForm()
+    customer = customer_utils.get_or_create_customer(request)
+    _save_customer(request, customer)
+    _save_country(request, customer)
+    
+    result = simplejson.dumps({
+        "shipping" : shipping_inline(request),
+        "payment" : payment_inline(request, form),
+        "cart" : cart_inline(request),
+    })
+
+    return HttpResponse(result)
+
+def _save_country(request, customer):
+    """
+    """
+    # Update shipping country
+    country = request.POST.get("shipping_country")
+    if request.POST.get("no_shipping") == "on":
+        country = request.POST.get("invoice_country")
+
+    if customer.selected_shipping_address:
+        customer.selected_shipping_address.country_id = country
+        customer.selected_shipping_address.save()
+    customer.selected_country_id = country
+    customer.save()
+
+    lfs.shipping.utils.update_to_valid_shipping_method(request, customer)
+    lfs.payment.utils.update_to_valid_payment_method(request, customer)
+    customer.save()
+
+def _save_customer(request, customer):
+    """
+    """
+    shipping_method = request.POST.get("shipping-method")
+    customer.selected_shipping_method_id = shipping_method
+
+    payment_method = request.POST.get("payment_method")
+    customer.selected_payment_method_id = payment_method
+
+    customer.save()
+
+    lfs.shipping.utils.update_to_valid_shipping_method(request, customer)
+    lfs.payment.utils.update_to_valid_payment_method(request, customer)
+    customer.save()
+
 def check_voucher(request):
     """
     """
         country_iso = request.POST.get("invoice-country", None)
 
     if country_iso is not None:
-        country = Country.objects.filter(code=country_iso.lower())[0]
+        country = Country.objects.get(code=country_iso.lower())
         if customer.selected_shipping_address:
             customer.selected_shipping_address.country = country
             customer.selected_shipping_address.save()
     lfs.shipping.utils.update_to_valid_shipping_method(request, customer)
     lfs.payment.utils.update_to_valid_payment_method(request, customer)
     customer.save()
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.