1

我正在使用平衡支付 API。我正在尝试获取用户的银行帐户信息,因此我使用他们的示例 jsfiddle作为指南。所以我知道我必须将银行帐户 URI 保存到我的用户模型中。问题是如何在 Rails 中实现它?

这就是我的jsfiddle的 rails 版本。

%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);
4

1 回答 1

2

响应回调应该将 URI 发送到您的 Rails 控制器之一中的方法,您将在其中调用帐户或客户对象的 add_bank_account。通常,此时您已经为用户创建了 Account 或 Customer 对象,并将 account_uri 或 customer_uri 存储在数据库的列中。所以,像

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

如果您计划每个用户只有一个银行账户,那么您也可以将 bank_account_uri 存储在数据库中,但默认情况下,Balanced API 始终使用最后添加的银行账户。

此外,如果您还不知道,您可以参考一个示例 Rails 应用程序。https://github.com/balanced/rentmybikes-rails

如果您有任何其他问题,请随时通过 freenode IRC 上的#balanced 访问。

于 2013-08-01T01:10:11.793 回答