From ab4d1cf9c9294427f2d8e9ea2391561719fca0b4 Mon Sep 17 00:00:00 2001 From: Levi Date: Sun, 4 Jun 2017 17:04:38 -0500 Subject: [PATCH] Changes stripe v2 to stripe v3 and added localization support --- Changelog | 2 + hosting/static/hosting/js/payment.js | 76 +++++++++++++++-------- hosting/templates/hosting/base_short.html | 3 +- hosting/templates/hosting/payment.html | 26 ++++++-- utils/stripe_utils.py | 1 + 5 files changed, 77 insertions(+), 31 deletions(-) diff --git a/Changelog b/Changelog index 444c34a6..e928d964 100644 --- a/Changelog +++ b/Changelog @@ -12,3 +12,5 @@ next * [opennebula_api] Improve testing, add ssh key functions * [opennebula_api] Remove template views * [datacenterlight] Allow user to have multiple ssh keys + * [datacenterlight] Changed stripe.js v2 to v3 + * [datacenterlight] Added support for stripe payment errors on current user language diff --git a/hosting/static/hosting/js/payment.js b/hosting/static/hosting/js/payment.js index d06a5a3d..dc4023e8 100644 --- a/hosting/static/hosting/js/payment.js +++ b/hosting/static/hosting/js/payment.js @@ -1,5 +1,11 @@ $( document ).ready(function() { + + var stripe = Stripe(window.stripeKey); + var elements = stripe.elements({locale: window.current_lan}); + var card = elements.create('card', options={hidePostalCode: true}); + card.mount('#card-element'); + $.ajaxSetup({ beforeSend: function(xhr, settings) { function getCookie(name) { @@ -38,10 +44,8 @@ $( document ).ready(function() { console.log("creditcard sdasd"); // if (hasCreditcard) { $('#billing-form').submit(); - console.log("has creditcard2"); // } - // $form.submit(); } @@ -53,33 +57,55 @@ $( document ).ready(function() { function payWithStripe(e) { e.preventDefault(); - /* Visual feedback */ - $form.find('[type=submit]').html('Validating '); + function stripeTokenHandler(token) { + // Insert the token ID into the form so it gets submitted to the server + var form = document.getElementById('payment-form'); + var hiddenInput = document.createElement('input'); + $('#id_token').val(token.id); - var PublishableKey = window.stripeKey; - Stripe.setPublishableKey(PublishableKey); - Stripe.card.createToken($form, function stripeResponseHandler(status, response) { - if (response.error) { - /* Visual feedback */ - $form.find('[type=submit]').html('Try again'); - /* Show Stripe errors on the form */ - $form.find('.payment-errors').text(response.error.message); - $form.find('.payment-errors').closest('.row').show(); + $('#billing-form').submit(); + } + + + stripe.createToken(card).then(function(result) { + if (result.error) { + // Inform the user if there was an error + var errorElement = document.getElementById('card-errors'); + errorElement.textContent = result.error.message; } else { - /* Visual feedback */ $form.find('[type=submit]').html('Processing '); - /* Hide Stripe errors on the form */ - $form.find('.payment-errors').closest('.row').hide(); - $form.find('.payment-errors').text(""); - // response contains id and card, which contains additional card details - var token = response.id; - // AJAX - - //set token on a hidden input - $('#id_token').val(token); - $('#billing-form').submit(); + // Send the token to your server + stripeTokenHandler(result.token); } }); + + // /* Visual feedback */ + // $form.find('[type=submit]').html('Validating '); + + // var PublishableKey = window.stripeKey; + // Stripe.setPublishableKey(PublishableKey); + // Stripe.card.createToken($form, function stripeResponseHandler(status, response) { + // if (response.error) { + // /* Visual feedback */ + // $form.find('[type=submit]').html('Try again'); + // /* Show Stripe errors on the form */ + // $form.find('.payment-errors').text(response.error.message); + // $form.find('.payment-errors').closest('.row').show(); + // } else { + // /* Visual feedback */ + // $form.find('[type=submit]').html('Processing '); + // /* Hide Stripe errors on the form */ + // $form.find('.payment-errors').closest('.row').hide(); + // $form.find('.payment-errors').text(""); + // // response contains id and card, which contains additional card details + // var token = response.id; + // // AJAX + + // //set token on a hidden input + // $('#id_token').val(token); + // $('#billing-form').submit(); + // } + // }); } /* Form validation */ @@ -131,7 +157,7 @@ $( document ).ready(function() { } else { return false; } - } + }; // $form.find('[type=submit]').prop('disabled', true); // var readyInterval = setInterval(function() { diff --git a/hosting/templates/hosting/base_short.html b/hosting/templates/hosting/base_short.html index 022629b0..5f493e7d 100644 --- a/hosting/templates/hosting/base_short.html +++ b/hosting/templates/hosting/base_short.html @@ -184,8 +184,9 @@ + + - diff --git a/hosting/templates/hosting/payment.html b/hosting/templates/hosting/payment.html index 7db83f10..88dd5ba7 100644 --- a/hosting/templates/hosting/payment.html +++ b/hosting/templates/hosting/payment.html @@ -62,7 +62,16 @@
-
+
+
+
+ +
+
+
+ + +
-
- +
+
@@ -125,8 +135,14 @@ {% if stripe_key %} +{% get_current_language as LANGUAGE_CODE %} {%endif%} diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index 8f017fbd..e1d86cf5 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -10,6 +10,7 @@ def handleStripeError(f): 'response_object': None, 'error': None } + import pdb;pdb.set_trace() common_message = "Currently its not possible to make payments." try: response_object = f(*args, **kwargs)