Added View to render payment page, Added Payment and summary forms, Added Payment.js library to request stripe token , Added jQuery validator for handling payment form errors
This commit is contained in:
parent
4e23adcea6
commit
38801abed7
13 changed files with 480 additions and 71 deletions
110
hosting/static/hosting/js/payment.js
Normal file
110
hosting/static/hosting/js/payment.js
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
$( document ).ready(function() {
|
||||
|
||||
var $form = $('#payment-form');
|
||||
$form.submit(payWithStripe);
|
||||
|
||||
/* If you're using Stripe for payments */
|
||||
function payWithStripe(e) {
|
||||
e.preventDefault();
|
||||
|
||||
/* Visual feedback */
|
||||
$form.find('[type=submit]').html('Validating <i class="fa fa-spinner fa-pulse"></i>');
|
||||
|
||||
var PublishableKey = 'pk_test_6pRNASCoBOKtIshFeQd4XMUh'; // Replace with your API publishable key
|
||||
Stripe.setPublishableKey(PublishableKey);
|
||||
Stripe.card.createToken($form, function stripeResponseHandler(status, response) {
|
||||
console.log
|
||||
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 <i class="fa fa-spinner fa-pulse"></i>');
|
||||
/* 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;
|
||||
console.log(token);
|
||||
// AJAX
|
||||
$.post('/account/stripe_card_token', {
|
||||
token: token
|
||||
})
|
||||
// Assign handlers immediately after making the request,
|
||||
.done(function(data, textStatus, jqXHR) {
|
||||
$form.find('[type=submit]').html('Payment successful <i class="fa fa-check"></i>').prop('disabled', true);
|
||||
})
|
||||
.fail(function(jqXHR, textStatus, errorThrown) {
|
||||
$form.find('[type=submit]').html('There was a problem').removeClass('success').addClass('error');
|
||||
/* Show Stripe errors on the form */
|
||||
$form.find('.payment-errors').text('Try refreshing the page and trying again.');
|
||||
$form.find('.payment-errors').closest('.row').show();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* Form validation */
|
||||
$.validator.addMethod("month", function(value, element) {
|
||||
return this.optional(element) || /^(01|02|03|04|05|06|07|08|09|10|11|12)$/.test(value);
|
||||
}, "Please specify a valid 2-digit month.");
|
||||
|
||||
$.validator.addMethod("year", function(value, element) {
|
||||
return this.optional(element) || /^[0-9]{2}$/.test(value);
|
||||
}, "Please specify a valid 2-digit year.");
|
||||
|
||||
validator = $form.validate({
|
||||
rules: {
|
||||
cardNumber: {
|
||||
required: true,
|
||||
creditcard: true,
|
||||
digits: true
|
||||
},
|
||||
expMonth: {
|
||||
required: true,
|
||||
month: true
|
||||
},
|
||||
expYear: {
|
||||
required: true,
|
||||
year: true
|
||||
},
|
||||
cvCode: {
|
||||
required: true,
|
||||
digits: true
|
||||
}
|
||||
},
|
||||
highlight: function(element) {
|
||||
$(element).closest('.form-control').removeClass('success').addClass('error');
|
||||
},
|
||||
unhighlight: function(element) {
|
||||
$(element).closest('.form-control').removeClass('error').addClass('success');
|
||||
},
|
||||
errorPlacement: function(error, element) {
|
||||
$(element).closest('.form-group').append(error);
|
||||
}
|
||||
});
|
||||
|
||||
paymentFormReady = function() {
|
||||
if ($form.find('[name=cardNumber]').hasClass("success") &&
|
||||
$form.find('[name=expMonth]').hasClass("success") &&
|
||||
$form.find('[name=expYear]').hasClass("success") &&
|
||||
$form.find('[name=cvCode]').val().length > 1) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$form.find('[type=submit]').prop('disabled', true);
|
||||
var readyInterval = setInterval(function() {
|
||||
if (paymentFormReady()) {
|
||||
$form.find('[type=submit]').prop('disabled', false);
|
||||
clearInterval(readyInterval);
|
||||
}
|
||||
}, 250);
|
||||
|
||||
});
|
||||
|
||||
|
|
@ -8,7 +8,7 @@ $( document ).ready(function() {
|
|||
function calculate_price(vm_type){
|
||||
|
||||
var ID_SELECTOR = "#";
|
||||
var CURRENCY = "$";
|
||||
var CURRENCY = "CHF";
|
||||
var final_price_selector = ID_SELECTOR.concat(vm_type.concat('-final-price'));
|
||||
var core_selector = ID_SELECTOR.concat(vm_type.concat('-cores'));
|
||||
var memory_selector = ID_SELECTOR.concat(vm_type.concat('-memory'));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue