Skip to content

Instantly share code, notes, and snippets.

@sfkaos
Created Jun 17, 2013
Embed
What would you like to do?
<script type="text/javascript" src="https://js.balancedpayments.com/v1/balanced.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
var balanced_library = require('balanced');
var balanced = new balanced_library({
marketplace_uri: "##############################",
secret: "#########################"
});
function balancedCallback(response) {
var tag = (response.status < 300) ? 'pre' : 'code';
debug(tag, JSON.stringify(response));
switch (response.status) {
case 201:
var request;
request = $.ajax({
type: 'POST',//method,
beforeSend: function(xhr){
//append a loading message here too
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))
},
url: "<%= user_bank_accounts_path %>",//parameter,
dataType: 'json',
data: { uri: response.data.uri }
});
// callback handler that will be called on success
request.done(function (object, textStatus, jqXHR){
// log a message to the console
window.location = "<%= thank_you_path %>"
});
request.fail(function(jqXHR, textStatus, errorThrown){
});
case 400:
case 403:
// missing/malformed data - check response.error for details
break;
case 404:
// your marketplace URI is incorrect
break;
default:
// we did something unexpected - check response.error for details
break;
}
}
var tokenizeBankAccount = function(e) {
e.preventDefault();
var $form = $('form#payment');
var bankAccountData = {
name: $form.find('[name="name"]').val(),
account_number: $form.find('[name="account_number"]').val(),
routing_number: $form.find('[name="routing_number"]').val()
};
balanced.Customers.create({
name: $form.find('[name="name"]').val(),
email: '<%= @user.email %>'
}, balancedCallback);
};
$('#payment').submit(tokenizeBankAccount);
if (window.location.protocol === 'file:') {
alert("balanced.js does not work when included in pages served over file:// URLs. Try serving this page over a webserver. Contact support@balancedpayments.com if you need assistance.");
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment