Skip to content

Instantly share code, notes, and snippets.

@remear
Last active December 20, 2015 14:59
Show Gist options
  • Save remear/7f6d7e6f24aa7e63fd2b to your computer and use it in GitHub Desktop.
Save remear/7f6d7e6f24aa7e63fd2b to your computer and use it in GitHub Desktop.
balanced.js Rails Ajax Example
%form.form-horizontal{"data-async" => ""}
%h3 Update Payment Method
%fieldset
.control-group
%label.control-label{:for => "name"} Account Holder's Name
.controls
%input.ba-name.input-xlarge{:autocomplete => "off", :type => "text", value: 'Levain Bakery LLC'}
.control-group
%label.control-label{:for => "bank_code"} Routing Number
.controls
%input.ba-rn.input-xlarge{:autocomplete => "off", :type => "text", value: '011401533'}
%a.form-help{"data-toggle" => "modal", :href => "#help-checks"} Help
.control-group
%label.control-label{:for => "account_number"} Account Number
.controls
%input.ba-an.input-xlarge{:autocomplete => "off", :type => "text", value: '121042882'}
%a.form-help{"data-toggle" => "modal", :href => "#help-checks"} Help
.control-group
%label.control-label{:for => "bank_code"} Account Type
.controls
%select1
%option{:selected => "", :value => ""}
Select Account Type
%option{:value => "checking", selected: ''} Checking
%option{:value => "savings"} Savings
%input.btn.btn-primary{:type => "submit", :value => "Save"}
balanced.init(marketplaceUri);
function balancedCallback(response) {
switch (response.status) {
case 201:
$.ajax({ url: '#{settings_addpaymentmethod_path}',
type: 'POST',
beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')},
dataType: "json",
data: 'account_uri=' + response.data.uri
success: function(response) {
// successfully executed the controller method
}
});
break;
default:
// got something else back from balanced than a success
break;
}
};
jQuery(function($) {
$('form[data-async]').on('submit', function(event) {
event.preventDefault();
form = $(this);
var bankAccountData = {
name: form.find('.ba-name').val(),
account_number: form.find('.ba-an').val(),
routing_number: form.find('.ba-rn').val(),
type: form.find('select').val()
};
balanced.bankAccount.create(bankAccountData, balancedCallback);
});
}
def add_payment_method
customer = Balanced::Customer.find(current_user.account_uri)
customer.add_bank_account(params['customer_uri'])
if customer.save
bank_account = Balanced::BankAccount.find(params['customer_uri'])
render json: bank_account
else
render json: {error: "Payment account could not be configured properly"}, status: 401
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment