铁路和平衡支付 - Tokenize并添加银行账户

时间:2014-01-04 19:21:23

标签: ruby-on-rails ruby balanced-payments

我正在使用我的rails 4应用程序进行均衡支付,我需要一些关于如何标记和添加银行帐户的帮助。我不确定我做错了什么。我阅读了文档并按照gist进行了操作。我获得了200 ok状态,我没有在日志中看到任何错误。但是,当我在我的测试市场上检查响应时,它没有添加银行帐户。

我的代码如下:

控制器:

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);
});

测试市场反应

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 个答案:

答案 0 :(得分:2)

如果您收到200创建的状态,这意味着某个地方正在创建卡片对象,则可能会将其发布到错误的市场。您确定要初始化正确的市场URI吗?你也使用Balanced.js来标记你的卡? https://docs.balancedpayments.com/current/#balanced-js

我还会快速浏览一下示例rails app:https://github.com/balanced/rentmybikes-rails