rails and balanced payments - Tokenize and add bank account

1k Views Asked by At

I'm using balanced payments with my rails 4 app and I need some help on how to tokenize and add a bank account. I'm not sure what I'm doing wrong. I read the docs and followed this gist . I'm getting a 200 ok status and I don't see any errors in the logs. However, when i check the response on my test marketplace, it's not adding the bank account.

My code is as follows:

controller:

class BalancedController < ApplicationController
  require 'balanced'
  Balanced.configure('ak-test-API# HERE')

  def create_balanced_account
      current_user.balanced_account_uri = Balanced::Marketplace.my_marketplace.create_customer(:email => current_user.email, :type => 'person').uri
      current_user.save
      redirect_to root_url
    end

  def bankaccount_form
     #render the form to collect bank account info. 
  end

  def store_bank_account
      balanced_account = Balanced::Customer.find(current_user.balanced_account_uri)
      balanced_account.add_bank_account(params[:balancedBankAccountURI]) 
       redirect_to root_url
    end


    def process_payment
      balanced_account = Balanced::Account.find(current_user.balanced_account_uri)
      account.debit(
        :amount => 1000, # or params[:amount]
        :description => "Balanced Payments transaction",
        :appears_on_statement_as => "Balanced Payments")
      # add a redirect to the desired path
        redirect_to root_url
    end


end

bankaccount_form.html.haml

= form_tag :action => "store_bank_account", :method => :post, :id => "bank-account-form", :class => "form-horizontal" do
  .control-group
    = label_tag "ba-name", "Account Holder's Name", :class => "control-label"
    .controls
      = text_field_tag "ba-name", nil, :placeholder => "Account Holder's Name", :class => "ba-name", :autocomplete => "off"
  .control-group
    = label_tag "ba-rn", "Routing Number", :class => "control-label"
    .controls
      = text_field_tag "ba-rn", nil, :placeholder => "Routing Number", :class => "ba-rn", :autocomplete => "off"
  .control-group
    = label_tag "ba-an", "Account Number", :class => "control-label"
    .controls
      = text_field_tag "ba-an", nil, :placeholder => "Account Number", :class => "ba-an", :autocomplete => "off"
  .control-group
    = label_tag "ba-type", "Type", :class => "control-label"
    .controls
      = select_tag "ba-type", "<option value='' disabled selected style='display:none;'>Select Account Type</option><option value=\"checking\">CHECKING</option><option value=\"savings\">SAVINGS</option>".html_safe
  .control-group
    .controls
      = submit_tag "Submit", :class => "btn btn-primary btn-large"

bank_account_submission.js

var marketplaceUri = '/v1/marketplaces/TEST-marketplaceUri';
var requestBinUrl = '/store_bank_account'

var debug = function(tag, content) {
  $('<' + tag + '>' + content + '</' + tag + '>').appendTo('#result');
};

try {
    balanced.init(marketplaceUri);
} catch (e) {
    debug('code', 'balanced.init error!');
}

function balancedCallback(response) {
    var tag = (response.status < 300) ? 'pre' : 'code';
    debug(tag, JSON.stringify(response));
    switch(response.status) {
        case 201:
            console.log(response.data);
            var $form = $("#bank-account-form");
            var bank_account_uri = response.data['uri'];
            $('<input>').attr({
                type: 'visible',
                value: bank_account_uri,
                name: 'balancedBankAccountURI'
            }).appendTo($form);
            $form.attr({action: requestBinUrl});
            $form.get(0).submit();
            break;
        case 400:
            console.log(response.error);
            break;
        case 404:
            console.log(response.error);
            break;
    }
}

var tokenizeBankAccount = 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, balancedCallback);
};

$(function(){
    $('#bank-account-form').submit(tokenizeBankAccount);
});

test marketplace response

PUT
URI/v1/customers/token_id_here
Status200 OK


    {
      "twitter": null,
      "meta": {},
      "id": "token_id_here",
      "destination": null,
      "source": null,
      "email": "email here",
      "_type": "customer",
      "bank_accounts_uri": "/v1/customers/token_id_here/bank_accounts",
      "phone": null,
      "_uris": {
        "transactions_uri": {
          "_type": "page",
          "key": "transactions"
        },
        "bank_accounts_uri": {
          "_type": "page",
          "key": "bank_accounts"
        },
        "refunds_uri": {
          "_type": "page",
          "key": "refunds"
        },
        "debits_uri": {
          "_type": "page",
          "key": "debits"
        },
        "holds_uri": {
          "_type": "page",
          "key": "holds"
        },
        "reversals_uri": {
          "_type": "page",
          "key": "reversals"
        },
        "credits_uri": {
          "_type": "page",
          "key": "credits"
        },
        "cards_uri": {
          "_type": "page",
          "key": "cards"
        }
      },
      "facebook": null,
      "address": {
        "country_code": "USA"
      },
      "business_name": null,
      "reversals_uri": "/v1/customers/token_id_here/reversals",
      "credits_uri": "/v1/customers/token_id_here/credits",
      "cards_uri": "/v1/customers/token_id_here/cards",
      "holds_uri": "/v1/customers/token_id_here/holds",
      "name": null,
      "dob": null,
      "created_at": "2014-01-04T18:11:19.498812Z",
      "is_identity_verified": false,
      "uri": "/v1/customers/token_id_here",
      "refunds_uri": "/v1/customers/token_id_here/refunds",
      "debits_uri": "/v1/customers/token_id_here/debits",
      "transactions_uri": "/v1/customers/token_id_here/transactions",
      "ssn_last4": null,
      "ein": null
    }
1

There are 1 best solutions below

1
On BEST ANSWER

if you're receiving a 200 created status that means a card object is being created somewhere, it is possible that it's being posted to the wrong marketplace. Are you sure that you're initializing the correct marketplace URI? Also are you using Balanced.js to tokenize your cards? https://docs.balancedpayments.com/current/#balanced-js

I'd also take a quick look at our example rails app: https://github.com/balanced/rentmybikes-rails