Question

I'm using balanced payment API. I'm trying to get the users bank account information so I'm using their example jsfiddle as a guide. So I know I must save the bank account URI to my user model. The question is how do I pull it off in Rails?

This is what my rails version of the jsfiddle.

%form#bank-account-form{:action => "#", :method => "POST"}
  = label_tag :bank_name, "Account Holder's Name"
  = text_field_tag :bank_name, nil, name: nil, :value => "John Q. TaxPayer", class: "ba-name"
  %p
  = label_tag :route_num, "Routing Number"
  = text_field_tag :route_num, nil, name: nil, :value => "121000358", class: "ba-rn"
  %p
  = label_tag :acct_num, "Account Number"
  = text_field_tag :acct_num, nil, name: nil, :value => "9900000001", class: "ba-an"
  %p
  %button.btn{:type => "submit"}
    tokenize


%script{:charset => "utf-8", :type => "text/javascript"}
  \// FOR DEMONSTRATION PURPOSES ONLY - if you already have a server you can POST to, replace
  \//                                   the URL with the URL to post to.
  \// go to http://requestb.in/
  \// click create new request bin and COPY that URL without the ?inspect at the end
  var requestBinURL = 'http://requestb.in/1jwwlla1';  // make sure it doesn't end in ?inspect

  var marketplaceUri = '/v1/marketplaces/TEST-MPg9bCIQUZMBoiPMnvWkQJW';
  balanced.init(marketplaceUri);

  function responseCallbackHandler(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);
  var $form = $("#bank-account-form");
  \// the uri is an opaque token referencing the tokenized bank account
  var bank_account_uri = response.data['uri'];
  \// append the token as a hidden field to submit to the server
  $('
  %input>/
  ').attr({
  type: 'hidden',
  value: bank_account_uri,
  name: 'balancedBankAccountURI'
  }).appendTo($form);
  $form.attr({action: requestBinURL});
  $form.get(0).submit();
  }
  }

  var tokenizeInstrument = function(e) {
  e.preventDefault();

  var $form = $('#bank-account-form');
  var bankAccountData = {
  name: $form.find('.ba-name').val(),
  account_number: $form.find('.ba-an').val(),
  bank_code: $form.find('.ba-rn').val(),
  type: $form.find('select').val()
  };


  balanced.bankAccount.create(bankAccountData, responseCallbackHandler);
  };
  $('#bank-account-form').submit(tokenizeInstrument);
Was it helpful?

Solution

The response callback should send the URI to a method in one of your Rails controllers where you'll call add_bank_account on either an Account or Customer object. Generally by this point you have already created an Account or Customer object for the user and have stored the account_uri or customer_uri in a column in your database. So, something like

customer = Balanced::Customer.find(current_user.customer_uri)
customer.add_bank_account(bank_account_uri)

If you're planning on only having one bank account per user, then you can store the bank_account_uri in the database as well if you like, but the Balanced API always uses the last added bank account by default.

Also, if you weren't already aware, there's an example Rails application you can reference. https://github.com/balanced/rentmybikes-rails

If you have any other issues, feel free to drop by #balanced on freenode IRC.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top