forked from iM0hamed/Checker-Stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
76 lines (64 loc) · 1.84 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* =============================================================================
Script for Simple Donation Form
Handles validation and form processing
========================================================================== */
$(function() {
var $form = $('.donation-form');
var $otheramount = $form.find('.other-amount');
var $amount = $form.find('.amount');
var outputError = function(error) {
$('.messages')
.html('<p>' + error + '</p>')
.addClass('active');
$('.submit-button')
.removeProp('disabled')
.val('Submit Donation');
};
var stripeResponseHandler = function(status, response) {
if (response.error) {
outputError(response.error.message);
} else {
var token = response['id'];
$form.append('<input type="hidden" name="stripeToken" value="' + token + '">');
$form.get(0).submit();
}
};
var disableinput = function(amount) {
$amount
.val(amount)
.blur()
.prop('disabled');
};
var enableinput = function() {
$amount
.removeProp('disabled')
.focus();
};
$('.donation-form').on('submit', function(event) {
// Disable processing button to prevent multiple submits
$('.submit-button')
.prop('disabled', true)
.val('Processing...');
// Create Stripe token, check if CC information correct
Stripe.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()
}, stripeResponseHandler);
return false;
});
$('.form-amount label').on('click', function() {
var $label = $(this);
$label.parent().children('label').removeClass('active');
$label.addClass('active');
if ( $label.index() === 6 ) {
enableinput();
} else {
disableinput($label.find('.set-amount').val());
}
});
$amount.on('change', function() {
$otheramount.val($(this).val());
});
});