通过付款API将银行信息保存到您的模型中

时间:2013-08-01 00:36:25

标签: ruby-on-rails balanced-payments

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

这是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);

1 个答案:

答案 0 :(得分:2)

响应回调应该将URI发送到您的一个Rails控制器中的方法,在该控制器中,您将在Account或Customer对象上调用add_bank_account。通常,此时您已为用户创建了帐户或客户对象,并已将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。