From 34acf2310b484920b6dd93fca030d88030128ad8 Mon Sep 17 00:00:00 2001 From: Siarhei Puhach Date: Thu, 3 Aug 2017 12:47:34 +0300 Subject: [PATCH 1/8] Added stripe failed payment error handler --- datacenterlight/views.py | 4 ++++ hosting/templates/hosting/payment.html | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 10e2d791..7dd2a76f 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -426,6 +426,10 @@ class OrderConfirmationView(DetailView): customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() stripe_utils = StripeUtils() card_details = stripe_utils.get_card_details(customer.stripe_id, request.session.get('token')) + if not card_details.get('response_object') and not card_details.get('paid'): + msg = _('Currently its not possible to make payments. Please try later.') + messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') + return HttpResponseRedirect(reverse('datacenterlight:payment')) context = { 'site_url': reverse('datacenterlight:index'), 'cc_last4': card_details.get('response_object').get('last4'), diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index d03713c1..5ef7f1ce 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -49,6 +49,15 @@ +
+ {% for message in messages %} + {% if 'failed_payment' in message.tags %} + + {% endif %} + {% endfor %} +
From 29fb771afb3aa787d0fec7203f4cde0a1c8f50ff Mon Sep 17 00:00:00 2001 From: Siarhei Puhach Date: Fri, 4 Aug 2017 11:01:32 +0300 Subject: [PATCH 2/8] Changed payment error place, changed message source to Stripe errors handler --- datacenterlight/views.py | 2 +- hosting/templates/hosting/payment.html | 32 ++++++++++++++------------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 7dd2a76f..993cb508 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -427,7 +427,7 @@ class OrderConfirmationView(DetailView): stripe_utils = StripeUtils() card_details = stripe_utils.get_card_details(customer.stripe_id, request.session.get('token')) if not card_details.get('response_object') and not card_details.get('paid'): - msg = _('Currently its not possible to make payments. Please try later.') + msg = card_details.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') return HttpResponseRedirect(reverse('datacenterlight:payment')) context = { diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index 5ef7f1ce..acb5d389 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -49,15 +49,6 @@
-
- {% for message in messages %} - {% if 'failed_payment' in message.tags %} - - {% endif %} - {% endfor %} -
@@ -139,12 +130,23 @@
-

- {% blocktrans %} - You are not making any payment yet. After submitting your card - information, you will be taken to the Confirm Order Page. - {% endblocktrans %} -

+ {% if not messages %} +

+ {% blocktrans %} + You are not making any payment yet. After submitting your card + information, you will be taken to the Confirm Order Page. + {% endblocktrans %} +

+ {% endif %} +
+ {% for message in messages %} + {% if 'failed_payment' in message.tags %} +
  • +

    {{ message|safe }}

    +
+ {% endif %} + {% endfor %} +
From 8769c4cd1f6106ab7526be17e3c256bd28529374 Mon Sep 17 00:00:00 2001 From: Siarhei Puhach Date: Fri, 4 Aug 2017 12:38:24 +0300 Subject: [PATCH 3/8] Changed payment error message style --- hosting/static/hosting/css/landing-page.css | 6 +++++- hosting/templates/hosting/payment.html | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/hosting/static/hosting/css/landing-page.css b/hosting/static/hosting/css/landing-page.css index a7d69094..49471bc8 100644 --- a/hosting/static/hosting/css/landing-page.css +++ b/hosting/static/hosting/css/landing-page.css @@ -537,6 +537,10 @@ a.unlink:hover { border-radius: 3px; padding: 5px; } +.card-warning-error { + border: 1px solid #EB4D5C; + color: #EB4D5C; +} .stripe-payment-btn { outline: none; @@ -718,4 +722,4 @@ a.unlink:hover { .footer-light a:hover, .footer-light a:focus, .footer-light a:active { color: #ddd; -} \ No newline at end of file +} diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index acb5d389..ac9ab41b 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -138,11 +138,11 @@ {% endblocktrans %}

{% endif %} -
+
{% for message in messages %} {% if 'failed_payment' in message.tags %}
  • -

    {{ message|safe }}

    +

    {{ message|safe }}

{% endif %} {% endfor %} From 4de04b2663c47d5e367e84570faf39288e49ae5e Mon Sep 17 00:00:00 2001 From: Siarhei Puhach Date: Mon, 7 Aug 2017 09:56:57 +0300 Subject: [PATCH 4/8] Changed visa card error placement --- hosting/templates/hosting/payment.html | 6 ++++-- utils/stripe_utils.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index 41c6d3eb..449962a5 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -59,7 +59,6 @@ {% csrf_token %} {% bootstrap_field field show_label=False type='fields'%} {% endfor %} - {% bootstrap_form_errors form type='non_fields'%}
@@ -130,7 +129,7 @@
- {% if not messages %} + {% if not messages and not form.errors %}

{% blocktrans %} You are not making any payment yet. After submitting your card @@ -146,6 +145,9 @@ {% endif %} {% endfor %} + {% if form.errors %} + {% bootstrap_form_errors form type='non_fields'%} + {% endif %}

diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index d46cf54d..f7d52157 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -11,7 +11,7 @@ def handleStripeError(f): 'error': None } - common_message = "Currently its not possible to make payments." + common_message = "Currently it's not possible to make payments." try: response_object = f(*args, **kwargs) response = { From 4fea099b5d8cba24e6a39a35a3a38961e4db9a5b Mon Sep 17 00:00:00 2001 From: Siarhei Puhach Date: Mon, 7 Aug 2017 10:05:12 +0300 Subject: [PATCH 5/8] Changed invalid credit card error style --- hosting/templates/hosting/payment.html | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index 449962a5..cb7a4d8c 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -145,8 +145,13 @@ {% endif %} {% endfor %} + {% if form.errors %} - {% bootstrap_form_errors form type='non_fields'%} + {% for error in form.non_field_errors %} +

+ {{ error|escape }} +

+ {% endfor %} {% endif %}
From 4cd3d6a4aada182b3d88ec7a6f6e2743278a7754 Mon Sep 17 00:00:00 2001 From: Siarhei Puhach Date: Mon, 7 Aug 2017 13:36:21 +0300 Subject: [PATCH 6/8] Added stripe make_charge error handler --- datacenterlight/views.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index dcf64814..d82522e9 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -467,15 +467,12 @@ class OrderConfirmationView(DetailView): stripe_utils = StripeUtils() charge_response = stripe_utils.make_charge(amount=final_price, customer=customer.stripe_id) - charge = charge_response.get('response_object') # Check if the payment was approved - if not charge: - context = {} - context.update({ - 'paymentError': charge_response.get('error') - }) - return render(request, self.payment_template_name, context) + if not charge_response.get('response_object') and not charge_response.get('paid'): + msg = charge_response.get('error') + messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error') + return HttpResponseRedirect(reverse('datacenterlight:payment')) charge = charge_response.get('response_object') From a71ccbc56615d2f8370d912ae8e4702b261144ed Mon Sep 17 00:00:00 2001 From: Siarhei Puhach Date: Mon, 7 Aug 2017 17:02:47 +0300 Subject: [PATCH 7/8] Added error hash in url, added payment error handler to hosting/views --- datacenterlight/views.py | 4 +- hosting/templates/hosting/payment.html | 57 ++++++++++++++------------ hosting/views.py | 11 ++--- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index d82522e9..b20b212f 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -441,7 +441,7 @@ class OrderConfirmationView(DetailView): if not card_details.get('response_object') and not card_details.get('paid'): msg = card_details.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') - return HttpResponseRedirect(reverse('datacenterlight:payment')) + return HttpResponseRedirect(reverse('datacenterlight:payment') + '#dcl_payment_error') context = { 'site_url': reverse('datacenterlight:index'), @@ -472,7 +472,7 @@ class OrderConfirmationView(DetailView): if not charge_response.get('response_object') and not charge_response.get('paid'): msg = charge_response.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error') - return HttpResponseRedirect(reverse('datacenterlight:payment')) + return HttpResponseRedirect(reverse('datacenterlight:payment') + '#dcl_payment_error') charge = charge_response.get('response_object') diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index cb7a4d8c..4c4ffa62 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -85,13 +85,29 @@
-

- {% blocktrans %} - You are not making any payment yet. After submitting your card - information, you will be taken to the Confirm Order Page. - {% endblocktrans %} -

-
+ {% if not messages and not form.non_field_errors %} +

+ {% blocktrans %} + You are not making any payment yet. After submitting your card + information, you will be taken to the Confirm Order Page. + {% endblocktrans %} +

+ {% endif %} +
+ {% for message in messages %} + {% if 'failed_payment' or 'make_charge_error' in message.tags %} +
  • +

    {{ message|safe }}

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

+ {{ error|escape }} +

+ {% endfor %} +
+
- {% if not messages and not form.errors %} + {% if not messages and not form.non_field_errors %}

{% blocktrans %} You are not making any payment yet. After submitting your card @@ -137,22 +153,20 @@ {% endblocktrans %}

{% endif %} -
+
{% for message in messages %} - {% if 'failed_payment' in message.tags %} + {% if 'failed_payment' or 'make_charge_error' in message.tags %}
  • {{ message|safe }}

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

- {{ error|escape }} -

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

+ {{ error|escape }} +

+ {% endfor %}
@@ -168,15 +182,6 @@

- {% if paymentError %} -
-
-

- {% bootstrap_alert paymentError alert_type='danger' %} -

-
-
- {% endif %} {% endif %} diff --git a/hosting/views.py b/hosting/views.py index 19ec5b2a..d1666b04 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -557,15 +557,12 @@ class PaymentVMView(LoginRequiredMixin, FormView): stripe_utils = StripeUtils() charge_response = stripe_utils.make_charge(amount=final_price, customer=customer.stripe_id) - charge = charge_response.get('response_object') # Check if the payment was approved - if not charge: - context.update({ - 'paymentError': charge_response.get('error'), - 'form': form - }) - return render(request, self.template_name, context) + if not charge_response.get('response_object') and not charge_response.get('paid'): + msg = charge_response.get('error') + messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error') + return HttpResponseRedirect(reverse('hosting:payment') + '#hosting_payment_error') charge = charge_response.get('response_object') From 72ddfd96ab0fad277a1471ba3c723c6c5ff4fc45 Mon Sep 17 00:00:00 2001 From: Siarhei Puhach Date: Mon, 7 Aug 2017 17:23:58 +0300 Subject: [PATCH 8/8] Payment error was unified --- datacenterlight/views.py | 4 ++-- hosting/templates/hosting/payment.html | 4 ++-- hosting/views.py | 7 ++++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index b20b212f..929b0833 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -441,7 +441,7 @@ class OrderConfirmationView(DetailView): if not card_details.get('response_object') and not card_details.get('paid'): msg = card_details.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') - return HttpResponseRedirect(reverse('datacenterlight:payment') + '#dcl_payment_error') + return HttpResponseRedirect(reverse('datacenterlight:payment') + '#payment_error') context = { 'site_url': reverse('datacenterlight:index'), @@ -472,7 +472,7 @@ class OrderConfirmationView(DetailView): if not charge_response.get('response_object') and not charge_response.get('paid'): msg = charge_response.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error') - return HttpResponseRedirect(reverse('datacenterlight:payment') + '#dcl_payment_error') + return HttpResponseRedirect(reverse('datacenterlight:payment') + '#payment_error') charge = charge_response.get('response_object') diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index 4c4ffa62..7bf84645 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -93,7 +93,7 @@ {% endblocktrans %}

{% endif %} -
+
{% for message in messages %} {% if 'failed_payment' or 'make_charge_error' in message.tags %}
  • @@ -153,7 +153,7 @@ {% endblocktrans %}

    {% endif %} -
    +
    {% for message in messages %} {% if 'failed_payment' or 'make_charge_error' in message.tags %}
    • diff --git a/hosting/views.py b/hosting/views.py index d1666b04..c5877ca2 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -547,8 +547,9 @@ class PaymentVMView(LoginRequiredMixin, FormView): customer = StripeCustomer.get_or_create(email=owner.email, token=token) if not customer: - form.add_error("__all__", "Invalid credit card") - return self.render_to_response(self.get_context_data(form=form)) + msg = _("Invalid credit card") + messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error') + return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error') # Create Billing Address billing_address = form.save() @@ -562,7 +563,7 @@ class PaymentVMView(LoginRequiredMixin, FormView): if not charge_response.get('response_object') and not charge_response.get('paid'): msg = charge_response.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error') - return HttpResponseRedirect(reverse('hosting:payment') + '#hosting_payment_error') + return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error') charge = charge_response.get('response_object')