From 02d144fbf9083f03c8473e67a6f5e12e9bacf16f Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Sat, 7 Oct 2017 12:59:40 +0200 Subject: [PATCH 01/10] Add form name and error fields to payment login form --- .../templates/datacenterlight/landing_payment.html | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/datacenterlight/templates/datacenterlight/landing_payment.html b/datacenterlight/templates/datacenterlight/landing_payment.html index fa638d77..461b057d 100644 --- a/datacenterlight/templates/datacenterlight/landing_payment.html +++ b/datacenterlight/templates/datacenterlight/landing_payment.html @@ -25,14 +25,15 @@

{%trans "Log in" %}


{% blocktrans %}Already signed up?
By logging in you can retrieve saved billing information.{% endblocktrans %}

-
+ {% for field in login_form %} {% csrf_token %} {% bootstrap_field field show_label=False type='fields'%} {% endfor %} +

{{login_form.non_field_errors|striptags}}

- +

From 564f47cf8aa6b756073e8db08e47a083356365f8 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Sun, 8 Oct 2017 13:50:43 +0200 Subject: [PATCH 02/10] Handle login and billing address forms together in PaymentOrderView --- .../datacenterlight/landing_payment.html | 7 +- datacenterlight/views.py | 75 +++++++++++++------ 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/datacenterlight/templates/datacenterlight/landing_payment.html b/datacenterlight/templates/datacenterlight/landing_payment.html index 461b057d..ca1a66e9 100644 --- a/datacenterlight/templates/datacenterlight/landing_payment.html +++ b/datacenterlight/templates/datacenterlight/landing_payment.html @@ -30,7 +30,7 @@ {% csrf_token %} {% bootstrap_field field show_label=False type='fields'%} {% endfor %} -

{{login_form.non_field_errors|striptags}}

+

{{login_form.non_field_errors|striptags}}

@@ -59,7 +59,7 @@ {% endfor %}
{% csrf_token %} - {% for field in form %} + {% for field in billing_address_form %} {% bootstrap_field field show_label=False type='fields'%} {% endfor %}
@@ -164,12 +164,13 @@

{% endif %} {% endfor %} - + {% comment %} {% for error in form.non_field_errors %}

{{ error|escape }}

{% endfor %} + {% endcomment %}
diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 40544fef..0f117012 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -341,26 +341,25 @@ class PaymentOrderView(FormView): else: return BillingAddressFormSignup - def get_form_kwargs(self): - form_kwargs = super(PaymentOrderView, self).get_form_kwargs() - # if user is signed in, get billing address - if self.request.user.is_authenticated(): - form_kwargs.update({ - 'instance': self.request.user.billing_addresses.first() - }) - if 'billing_address_data' in self.request.session: - billing_address_data = self.request.session['billing_address_data'] - form_kwargs.update({ - 'initial': billing_address_data - }) - return form_kwargs def get_context_data(self, **kwargs): context = super(PaymentOrderView, self).get_context_data(**kwargs) + if 'billing_address_data' in self.request.session: + billing_address_data = self.request.session['billing_address_data'] + else: + billing_address_data = {} context.update({ 'stripe_key': settings.STRIPE_API_PUBLIC_KEY, 'site_url': reverse('datacenterlight:index'), - 'login_form': HostingUserLoginForm() + 'login_form': HostingUserLoginForm(prefix='login_form'), + 'billing_address_form': BillingAddressForm( + prefix='billing_address_form', + instance=self.request.user.billing_addresses.first() + ) if self.request.user.is_authenticated() else + BillingAddressFormSignup( + prefix='billing_address_form_signup', + initial=billing_address_data + ) }) return context @@ -372,9 +371,32 @@ class PaymentOrderView(FormView): return self.render_to_response(self.get_context_data()) def post(self, request, *args, **kwargs): - form = self.get_form() - if form.is_valid(): - token = form.cleaned_data.get('token') + if 'login_form' in request.POST: + login_form = HostingUserLoginForm(data=request.POST, + prefix='login_form') + if login_form.is_valid(): + email = login_form.cleaned_data.get('email') + password = login_form.cleaned_data.get('password') + auth_user = authenticate(email=email, password=password) + if auth_user: + login(self.request, auth_user) + return HttpResponseRedirect(self.get_success_url()) + else: + context = self.get_context_data() + context['login_form'] = login_form + return self.render_to_response(context) + if request.user.is_authenticated(): + address_form = BillingAddressForm( + data=request.POST, + prefix='billing_address_form' + ) + else: + address_form = BillingAddressFormSignup( + data=request.POST, + prefix='billing_address_form_signup' + ) + if address_form.is_valid(): + token = address_form.cleaned_data.get('token') if request.user.is_authenticated(): this_user = { 'email': request.user.email, @@ -384,8 +406,8 @@ class PaymentOrderView(FormView): email=this_user.get('email'), token=token) else: - user_email = form.cleaned_data.get('email') - user_name = form.cleaned_data.get('name') + user_email = address_form.cleaned_data.get('email') + user_name = address_form.cleaned_data.get('name') this_user = { 'email': user_email, 'name': user_name @@ -418,13 +440,18 @@ class PaymentOrderView(FormView): token=token, customer_name=user_name) - request.session['billing_address_data'] = form.cleaned_data + request.session['billing_address_data'] = address_form.cleaned_data request.session['user'] = this_user # Get or create stripe customer if not customer: - form.add_error("__all__", "Invalid credit card") + address_form.add_error( + "__all__", "Invalid credit card" + ) return self.render_to_response( - self.get_context_data(form=form)) + self.get_context_data( + billing_address_form=address_form + ) + ) request.session['token'] = token if type(customer) is StripeCustomer: request.session['customer'] = customer.stripe_id @@ -433,7 +460,9 @@ class PaymentOrderView(FormView): return HttpResponseRedirect( reverse('datacenterlight:order_confirmation')) else: - return self.form_invalid(form) + context = self.get_context_data() + context['billing_address_form'] = address_form + return self.render_to_response(context) class OrderConfirmationView(DetailView): From 5a2657e3573537a108c1b06cce2da0f8c6b09ccd Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Sun, 8 Oct 2017 17:21:38 +0200 Subject: [PATCH 03/10] Create billing_address_form only for a logged user --- datacenterlight/views.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 0f117012..041a7710 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -341,21 +341,32 @@ class PaymentOrderView(FormView): else: return BillingAddressFormSignup - def get_context_data(self, **kwargs): context = super(PaymentOrderView, self).get_context_data(**kwargs) if 'billing_address_data' in self.request.session: billing_address_data = self.request.session['billing_address_data'] else: billing_address_data = {} + + billing_address_form = None + if self.request.user.is_authenticated(): + if billing_address_data: + billing_address_form = BillingAddressForm( + prefix='billing_address_form', + initial=billing_address_data + ) + else: + billing_address_form = BillingAddressForm( + prefix='billing_address_form', + instance=self.request.user.billing_addresses.first() + ) + context.update({ 'stripe_key': settings.STRIPE_API_PUBLIC_KEY, 'site_url': reverse('datacenterlight:index'), 'login_form': HostingUserLoginForm(prefix='login_form'), - 'billing_address_form': BillingAddressForm( - prefix='billing_address_form', - instance=self.request.user.billing_addresses.first() - ) if self.request.user.is_authenticated() else + 'billing_address_form': billing_address_form + if self.request.user.is_authenticated() else BillingAddressFormSignup( prefix='billing_address_form_signup', initial=billing_address_data @@ -380,7 +391,7 @@ class PaymentOrderView(FormView): auth_user = authenticate(email=email, password=password) if auth_user: login(self.request, auth_user) - return HttpResponseRedirect(self.get_success_url()) + return HttpResponseRedirect('') else: context = self.get_context_data() context['login_form'] = login_form From cdbab541bce65dd314f8febe62713cadf1819f2e Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Sun, 8 Oct 2017 17:29:10 +0200 Subject: [PATCH 04/10] Refactor billing_address_form for not logged in case --- datacenterlight/views.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 041a7710..4b26da6d 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -348,7 +348,6 @@ class PaymentOrderView(FormView): else: billing_address_data = {} - billing_address_form = None if self.request.user.is_authenticated(): if billing_address_data: billing_address_form = BillingAddressForm( @@ -360,17 +359,17 @@ class PaymentOrderView(FormView): prefix='billing_address_form', instance=self.request.user.billing_addresses.first() ) + else: + billing_address_form = BillingAddressFormSignup( + prefix='billing_address_form_signup', + initial=billing_address_data + ) context.update({ 'stripe_key': settings.STRIPE_API_PUBLIC_KEY, 'site_url': reverse('datacenterlight:index'), 'login_form': HostingUserLoginForm(prefix='login_form'), 'billing_address_form': billing_address_form - if self.request.user.is_authenticated() else - BillingAddressFormSignup( - prefix='billing_address_form_signup', - initial=billing_address_data - ) }) return context From 86c931cde15c164dc99435946f26a667e1063024 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Sun, 8 Oct 2017 19:24:58 +0200 Subject: [PATCH 05/10] Remove prefixes for billing_address_forms --- datacenterlight/views.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 4b26da6d..c9732c5c 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -351,17 +351,14 @@ class PaymentOrderView(FormView): if self.request.user.is_authenticated(): if billing_address_data: billing_address_form = BillingAddressForm( - prefix='billing_address_form', initial=billing_address_data ) else: billing_address_form = BillingAddressForm( - prefix='billing_address_form', - instance=self.request.user.billing_addresses.first() - ) + instance=self.request.user.billing_addresses.first() + ) else: billing_address_form = BillingAddressFormSignup( - prefix='billing_address_form_signup', initial=billing_address_data ) @@ -398,12 +395,10 @@ class PaymentOrderView(FormView): if request.user.is_authenticated(): address_form = BillingAddressForm( data=request.POST, - prefix='billing_address_form' ) else: address_form = BillingAddressFormSignup( data=request.POST, - prefix='billing_address_form_signup' ) if address_form.is_valid(): token = address_form.cleaned_data.get('token') From b44948b277fb8d87c28ce82489ddc84e6343d921 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Sun, 8 Oct 2017 20:21:17 +0200 Subject: [PATCH 06/10] Prefill cc info on landing for logged in case Attention: We still support only one card --- datacenterlight/views.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index c9732c5c..6eb35624 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -357,6 +357,19 @@ class PaymentOrderView(FormView): billing_address_form = BillingAddressForm( instance=self.request.user.billing_addresses.first() ) + # Get user last order + last_hosting_order = HostingOrder.objects.filter( + customer__user=self.request.user + ).last() + + # If user has already an hosting order, get the credit card + # data from it + if last_hosting_order: + credit_card_data = last_hosting_order.get_cc_data() + if credit_card_data: + context['credit_card_data'] = credit_card_data + else: + context['credit_card_data'] = None else: billing_address_form = BillingAddressFormSignup( initial=billing_address_data From 8b36d9cba4acf4effc9967bb5f00ba61ea905bc8 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Sun, 8 Oct 2017 21:01:25 +0200 Subject: [PATCH 07/10] Create stripe customer if not exists --- datacenterlight/views.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 6eb35624..16872aa2 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -591,9 +591,13 @@ class OrderConfirmationView(DetailView): try: custom_user = CustomUser.objects.get( email=user.get('email')) - customer = StripeCustomer.objects.filter( + stripe_customer = StripeCustomer.objects.filter( user_id=custom_user.id).first() - stripe_customer_id = customer.id + if stripe_customer is None: + stripe_customer = StripeCustomer.objects.create( + user=custom_user, stripe_id=stripe_api_cus_id + ) + stripe_customer_id = stripe_customer.id except CustomUser.DoesNotExist: logger.debug( "Customer {} does not exist.".format(user.get('email'))) From 152c9301806ac3430545716d108fb160753e6426 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Thu, 12 Oct 2017 22:50:54 +0200 Subject: [PATCH 08/10] Redirect explicitly to the payment page on dcl landing login --- datacenterlight/views.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index eec9fcf6..bd1a7f51 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -404,7 +404,9 @@ class PaymentOrderView(FormView): auth_user = authenticate(email=email, password=password) if auth_user: login(self.request, auth_user) - return HttpResponseRedirect('') + return HttpResponseRedirect( + reverse('datacenterlight:payment') + ) else: context = self.get_context_data() context['login_form'] = login_form From 64695d0a3803b87493e7f5acbfe254ee1a402eb0 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Fri, 13 Oct 2017 00:57:30 +0200 Subject: [PATCH 09/10] Remove commented code --- .../templates/datacenterlight/landing_payment.html | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/datacenterlight/templates/datacenterlight/landing_payment.html b/datacenterlight/templates/datacenterlight/landing_payment.html index ca1a66e9..d195690b 100644 --- a/datacenterlight/templates/datacenterlight/landing_payment.html +++ b/datacenterlight/templates/datacenterlight/landing_payment.html @@ -154,23 +154,12 @@ {% endif %}
{% for message in messages %} - {% if 'failed_payment' in message.tags or 'make_charge_error' in message.tags %} + {% if 'failed_payment' in message.tags or 'make_charge_error' in message.tags or 'error' in message.tags %}
  • {{ message|safe }}

- {% elif not form.non_field_errors %} -

- {% trans "You are not making any payment yet. After placing your order, you will be taken to the Submit Payment Page." %} -

{% endif %} {% endfor %} - {% comment %} - {% for error in form.non_field_errors %} -

- {{ error|escape }} -

- {% endfor %} - {% endcomment %}
From ed05411dacba708abb96d9608812bb816e03ad28 Mon Sep 17 00:00:00 2001 From: "M.Ravi" Date: Sat, 14 Oct 2017 12:52:18 +0200 Subject: [PATCH 10/10] Return if the form already contains error --- hosting/forms.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hosting/forms.py b/hosting/forms.py index 056d0004..7be7a588 100644 --- a/hosting/forms.py +++ b/hosting/forms.py @@ -29,6 +29,8 @@ class HostingUserLoginForm(forms.Form): def clean(self): email = self.cleaned_data.get('email') password = self.cleaned_data.get('password') + if self.errors: + return self.cleaned_data is_auth = authenticate(email=email, password=password) if not is_auth: raise forms.ValidationError(