Created
March 22, 2013 23:04
-
-
Save mfwarren/5225420 to your computer and use it in GitHub Desktop.
example html/JavaScript usage of using vogogo.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> | |
<title>Vogogo Sample Form</title> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> | |
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js"></script> | |
<script type="text/javascript" src="https://api.vogogo.com/vogogo.js/"></script> | |
<script type="text/javascript"> | |
Vogogo.setPublishableKey('SET YOUR APP TOKEN'); | |
$(document).ready(function() { | |
function addInputNames() { | |
// Not ideal, but jQuery's validate plugin requires fields to have names | |
// so we add them at the last possible minute, in case any javascript | |
// exceptions have caused other parts of the script to fail. | |
$(".card-name").attr("name", "card-name") | |
$(".card-number").attr("name", "card-number") | |
$(".card-ccv").attr("name", "card-ccv") | |
$(".card-expiry-year").attr("name", "card-expiry-year") | |
} | |
function removeInputNames() { | |
$(".card-name").removeAttr("name") | |
$(".card-number").removeAttr("name") | |
$(".card-ccv").removeAttr("name") | |
$(".card-expiry-year").removeAttr("name") | |
} | |
function submit(form) { | |
// remove the input field names for security | |
// we do this *before* anything else which might throw an exception | |
removeInputNames(); // THIS IS IMPORTANT! | |
// given a valid form, submit the payment details to vogogo | |
$(form['submit-button']).attr("disabled", "disabled") | |
Vogogo.createToken({ | |
name: $('.card-name').val(), | |
number: $('.card-number').val(), | |
ccv: $('.card-ccv').val(), | |
exp_month: $('.card-expiry-month').val(), | |
exp_year: $('.card-expiry-year').val() | |
}, function(status, response) { | |
if (response.error) { | |
// re-enable the submit button | |
$(form['submit-button']).removeAttr("disabled") | |
// show the error | |
$(".payment-errors").html(response.error.message); | |
// we add these names back in so we can revalidate properly | |
addInputNames(); | |
} else { | |
// token contains id, last4, and card type | |
var token = response['token']; | |
// insert the vogogo token | |
var input = $("<input name='vogogoToken' value='" + token + "' style='display:none;' />"); | |
form.appendChild(input[0]) | |
// and submit | |
form.submit(); | |
} | |
}); | |
return false; | |
} | |
// add custom rules for credit card validating | |
jQuery.validator.addMethod("cardNumber", Vogogo.validateCardNumber, "Please enter a valid card number"); | |
jQuery.validator.addMethod("cardCCV", Vogogo.validateCCV, "Please enter a valid security code"); | |
jQuery.validator.addMethod("cardExpiry", function() { | |
return Vogogo.validateExpiry($(".card-expiry-month").val(), | |
$(".card-expiry-year").val()) | |
}, "Please enter a valid expiration"); | |
// We use the jQuery validate plugin to validate required params on submit | |
$("#example-form").validate({ | |
submitHandler: submit, | |
rules: { | |
"card-ccv" : { | |
cardCCV: true, | |
required: true | |
}, | |
"card-number" : { | |
cardNumber: true, | |
required: true | |
}, | |
"card-expiry-year" : "cardExpiry" // we don't validate month separately | |
} | |
}); | |
// adding the input field names is the last step, in case an earlier step errors | |
addInputNames(); | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Vogogo Example Form</h1> | |
<form method="post" id="example-form" style="display: none;"> | |
<div class="form-row"> | |
<label for="name" class="vogogoLabel">Your Name</label> | |
<input type="text" name="name" class="card-name required" /> | |
</div> | |
<div class="form-row"> | |
<label for="email">E-mail Address</label> | |
<input type="text" name="email" class="required" /> | |
</div> | |
<div class="form-row"> | |
<label>Card Number</label> | |
<input type="text" maxlength="20" autocomplete="off" class="card-number vogogo-sensitive required" /> | |
</div> | |
<div class="form-row"> | |
<label>ccv</label> | |
<input type="text" maxlength="4" autocomplete="off" class="card-ccv vogogo-sensitive required" /> | |
</div> | |
<div class="form-row"> | |
<label>Expiration</label> | |
<div class="expiry-wrapper"> | |
<select class="card-expiry-month vogogo-sensitive required"> | |
</select> | |
<script type="text/javascript"> | |
var select = $(".card-expiry-month"), | |
month = new Date().getMonth() + 1; | |
for (var i = 1; i <= 12; i++) { | |
select.append($("<option value='"+i+"' "+(month === i ? "selected" : "")+">"+i+"</option>")) | |
} | |
</script> | |
<span> / </span> | |
<select class="card-expiry-year vogogo-sensitive required"></select> | |
<script type="text/javascript"> | |
var select = $(".card-expiry-year"), | |
year = new Date().getFullYear(); | |
for (var i = 0; i < 12; i++) { | |
select.append($("<option value='"+(i + year)+"' "+(i === 0 ? "selected" : "")+">"+(i + year)+"</option>")) | |
} | |
</script> | |
</div> | |
</div> | |
<button type="submit" name="submit-button">Submit</button> | |
<span class="payment-errors"></span> | |
</form> | |
<!-- | |
The easiest way to indicate that the form requires JavaScript is to show | |
the form with JavaScript (otherwise it will not render). You can add a | |
helpful message in a noscript to indicate that users should enable JS. | |
--> | |
<script>if (window.Vogogo) $("#example-form").show()</script> | |
<noscript><p>JavaScript is required for the registration form.</p></noscript> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment