Skip to content

Commit 1270541

Browse files
committed
Added Stripe response handler and error handling js
1 parent 4e0bc4b commit 1270541

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

saasapp/Requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Memberships
66

77
Stripe Integration
88
* Users should have a Stripe customer token in the users table.
9-
* Javascript should prevent Pro form from submitting, when user fills it out,
9+
* Javascript should prevent Pro form from submitting, after user fills it out,
1010
and should send card info to Stripe. Stripe will return with a card token.
1111
* Javascript should send user fields and card token to our rails app. Rails app
1212
should validate the user fields. Rails app will note whether plan 1 or plan 2.

saasapp/app/assets/javascripts/users.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,60 @@ $(document).on('turbolinks:load', function(){
1313
submitBtn.click(function(event){
1414
// prevent default submission behavior.
1515
event.preventDefault();
16+
submitBtn.val("Processing").prop('disabled', true);
1617

1718
// Collect the credit card fields.
1819
var ccNum = $('#card_number').val(),
1920
cvcNum = $('#card_code').val(),
2021
expMonth = $('#card_month').val(),
2122
expYear = $('#card_year').val();
2223

23-
// Send the card info to Stripe.
24-
Stripe.createToken({
25-
number: ccNum,
26-
cvc: cvcNum,
27-
exp_month: expMonth,
28-
exp_year: expYear
29-
}, stripeResponseHandler);
24+
// Use Stripe JS library to check for card errors.
25+
var error = false;
26+
27+
// Validate card number.
28+
if (!Stripe.card.validateCardNumber(ccNum)) {
29+
error = true;
30+
alert('The credit card number appears to be invalid.');
31+
}
32+
33+
// Validate CVC number.
34+
if (!Stripe.card.validateCVC(cvcNum)) {
35+
error = true;
36+
alert('The CVC number appears to be invalid.');
37+
}
38+
39+
// Validate expiration date.
40+
if (!Stripe.card.validateExpiry(expMonth, expYear)) {
41+
error = true;
42+
alert('The expiration date appears to be invalid.');
43+
}
44+
45+
if (error) {
46+
// If there are card errors, don't send to Stripe.
47+
submitBtn.prop('disabled', false).val("Sign Up");
48+
} else {
49+
// Send the card info to Stripe.
50+
Stripe.createToken({
51+
number: ccNum,
52+
cvc: cvcNum,
53+
exp_month: expMonth,
54+
exp_year: expYear
55+
}, stripeResponseHandler);
56+
}
57+
58+
return false;
3059
});
31-
3260

3361
// Stripe will return a card token.
34-
// Inject card token as hidden field into form.
35-
// Submit form to our Rails app.
62+
function stripeResponseHandler(status, response) {
63+
// Get the token from the response.
64+
var token = response.id;
65+
66+
// Inject card token as hidden field into form.
67+
theForm.append( $('<input type="hidden" name="user[stripe_card_token]">').val(token) );
68+
69+
// Submit form to our Rails app.
70+
theForm.get(0).submit();
71+
}
3672
});

0 commit comments

Comments
 (0)