ruby - rails and balanced payments - Tokenize and add bank account


Question: 

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 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

 

More Articles


node.js - Firebase calling `child(all).equalTo(id)`

I like to check all children that contains a child with a value to the variable id.This is what I came up with:usersref.child(formData.openid + '/votes').child().equalTo(formData.appid).once('value', function(snapshot) {});This returned: Firebase.child failed: Was called with 0 arguments. Expects at

node.js - Get data having a maximum attribute from firebase in nodejs

So i am working with firebase in nodejs, there is a "number" attribute in each of my document of a specific table(name generated at runtime). I want to get the data having the attribute "number"'s maximum value.Here is my sample data:- -L1GIb7Vyn6Yhd5gghH0 correct: blah number: 9 questio

node.js - Hand Off Bot to Human using BotFramework (NodeJS)

I have crated a server application using botframework in NodeJS and configured for web chat channel in botframework.I want to validate the every reply that bot delivers to end user and those messages will be validated by human from an external web application.How can we trigger the bot from external


jquery - How do I access a child named with a random id in firebase?

I want to be able to automatically access every child named with random ID keys nested inside "posts", is there a way to get the children of every child inside them?I am completely new to firebase and managing database in general (started learning code 3 months ago), so I might have missed it in the

typescript - How to loop through child records in firebase?

I'm trying to figure out how to loop through sent_request child which is inside users table and get all user_keys. I tried many ways but it's not working.table structure: here is my code:getProfileData(emailID){ var ref = firebase.database().ref('users'); ref.orderByChild("email").equalTo(emai

Firefox extension to get google pagerank and alexa ranking

I am creating a Mozilla Firefox toolbar to show PageRank and Alexa ranking of current website. One way I came to know is to use XMLHttpRequest in my JavaScript file to get information from a PHP page hosted on my website's server. The PHP class has this function:function check($page) {// Open a soc


I came across the new Stack Overflow Bot by Microsoft on the Stack Overflow blog and decided to give it a try.After installing Node V8.1.4 for macOS, I installed the bot by running npm install in the StackBot directory. There weren't many issues, except for a warning returned.MacBook-Air:StackBot US

node.js - BotBuilder TypeError: builder.ConsoleConnector is not a constructor

I'm following the official quick start of Microsoft Bot Builder(SDK v3) for NodeJs: Create a bot with the Bot Builder SDK for Node.js 1- I made a new project with npm init2- then npm install --save botbuilder3- then I created a new file "app.js"var builder = require('botbuilder');var connector =

Logs showing WARNING when trying to credit bank accounts with Balanced Payments

Been seeing this in my logs today. ############################################################# # WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! # ############################################################# Using positional arguments is **DEPRECATED**. Please use the keyword optio

node.js - Correct way to send response when using Actions-on-google with koa.js

I'm trying to use actions-on-google with koa framework. I can't understand which request and response to use with AoG constructor in koa middleware. This is my code:const Koa = require( 'koa' )const koaBody = require('koa-body')const { DialogflowApp } = require('actions-on-google')const koaApp = new