Skip to content

Instantly share code, notes, and snippets.

@mikeni
Created April 19, 2013 22:53
Show Gist options
  • Save mikeni/5423785 to your computer and use it in GitHub Desktop.
Save mikeni/5423785 to your computer and use it in GitHub Desktop.
for balanced
balanced.init('/v1/marketplaces/TEST-MP42gu0HtjZsseCT3OJ3X5Ta');
var alertError = function(errors) {
$(".alert").remove();
el = $('<div class="alert alert-error"></div>')
for (var k in errors) {
el.append($("<p>" + errors[k] + "</p>"));
}
el.append($('<button class="close" type="button" data-dismiss="alert">x</button>'));
$('body').prepend(el);
}
var form = $("#new_pledge");
form.submit(function(e){
var cardData = {
card_number: $("#credit_card_card_num").val(),
expiration_month: $("#credit_card_exp_mo").val(),
expiration_year: $("#credit_card_exp_yr").val(),
security_code: $("#credit_card_cvs").val()
};
var errors = balanced.card.validate(cardData);
if (jQuery.isEmptyObject(errors)) {
alert('create card');
balanced.card.create(cardData, function(response) {
alert('got response');
switch (response.status) {
case 400:
// missing or invalid field - check response.error for details
console.log(response.error);
break;
case 404:
// your marketplace URI is incorrect
console.log(response.error);
break;
case 201:
// WOO HOO! MONEY!
// response.data.uri == URI of the bank account resource you
// should store this bank account URI to later credit it
console.log(response.data);
// the uri is an opaque token referencing the tokenized bank account
var cardTokenURI = response.data['uri'];
// append the token as a hidden field to submit to the server
$('<input>').attr({
type: 'hidden',
value: cardTokenURI,
name: 'balancedCreditCardURI'
}).appendTo(form);
form.get(0).submit();
}
});
} else {
alertError(errors);
//e.preventDefault();
return false
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment