我正在使用平衡支付 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);