我编写了一个函数,让电子商务网站成员创建自己的Stripe帐户,让他们能够通过Stripe接收付款。我学习了一个在线教程,但到目前为止还不起作用(结果是
flash[:danger] = "Unable to create Stripe account!"
应用程序/控制器/条带控制器。rb型
class StripeController < ApplicationController
# Create a manage Stripe account for yourself.
# Only works on the currently logged in user.
# See app/services/stripe_managed.rb for details.
def managed
connector = StripeManaged.new( current_org_person )
account = connector.create_account!(params[:country], params[:tos] == '1', request.remote_ip)
if account
flash[:success] = "Managed Stripe account created!"
else
flash[:danger] = "Unable to create Stripe account!"
end
redirect_to org_people_stripe_settings_path
end
end
应用程序/服务/条带管理。rb型
class StripeManaged < Struct.new( :org_person )
ALLOWED = [ 'US', 'CA', 'TW' ] # public beta
COUNTRIES = [
{ name: 'United States', code: 'US' },
{ name: 'Canada', code: 'CA' },
{ name: 'Taiwan', code: 'TW' }
# { name: 'Australia', code: 'AU' },
# { name: 'United Kingdom', code: 'GB' },
# { name: 'Ireland', code: 'IE' }
]
def create_account!( country, tos_accepted, ip )
return nil unless tos_accepted
return nil unless country.in?( COUNTRIES.map { |c| c[:code] } )
begin
@account = Stripe::Account.create(
managed: true,
country: country,
email: org_person.email,
tos_acceptance: {
ip: ip,
date: Time.now.to_i
},
legal_entity: {
type: 'individual'
}
)
rescue
nil # TODO: improve
end
if @account
org_person.update_attributes(
stripe_currency: @account.default_currency,
stripe_account_type: 'managed',
stripe_user_id: @account.id,
stripe_secret_key: @account.keys.secret,
stripe_publishable_key: @account.keys.publishable,
stripe_account_status: account_status
)
end
@account
binding.pry
end
def update_account!( params: nil )
if params
if params[:bank_account_token]
account.bank_account = params[:bank_account_token]
account.save
end
if params[:legal_entity]
# clean up dob fields
params[:legal_entity][:dob] = {
year: params[:legal_entity].delete('dob(1i)'),
month: params[:legal_entity].delete('dob(2i)'),
day: params[:legal_entity].delete('dob(3i)')
}
# update legal_entity hash from the params
params[:legal_entity].entries.each do |key, value|
if [ :address, :dob ].include? key.to_sym
value.entries.each do |akey, avalue|
next if avalue.blank?
# Rails.logger.error "#{akey} - #{avalue.inspect}"
account.legal_entity[key] ||= {}
account.legal_entity[key][akey] = avalue
end
else
next if value.blank?
# Rails.logger.error "#{key} - #{value.inspect}"
account.legal_entity[key] = value
end
end
# copy 'address' as 'personal_address'
pa = account.legal_entity['address'].dup.to_h
account.legal_entity['personal_address'] = pa
account.save
end
end
org_person.update_attributes(
stripe_account_status: account_status
)
end
def legal_entity
account.legal_entity
end
def needs?( field )
org_person.stripe_account_status['fields_needed'].grep( Regexp.new( /#{field}/i ) ).any?
end
def supported_bank_account_countries
country_codes = case account.country
when 'US' then %w{ US }
when 'CA' then %w{ US CA }
when 'IE', 'UK' then %w{ IE UK US }
when 'AU' then %w{ AU }
when 'TW' then %w{ TW }
end
COUNTRIES.select do |country|
country[:code].in? country_codes
end
end
protected
def account_status
{
details_submitted: account.details_submitted,
charges_enabled: account.charges_enabled,
transfers_enabled: account.transfers_enabled,
fields_needed: account.verification.fields_needed,
due_by: account.verification.due_by
}
end
def account
@account ||= Stripe::Account.retrieve( org_person.stripe_user_id )
end
end
问题应该是
def create_account!
在stripe_管理中。rb返回
@account
nil
. 所以我怀疑
Stripe::Account.create
-
我目前住在台湾,哪个Stripe尚不支持,并使用Stripe Japan的帐户来管理平台。我提到的教程来自北美,所以它最初只关注美国和加拿大,然后我在散列中添加了台湾
COUNTRIES
以“美国”为所在国。
-
由于问题1,我怀疑ip地址与所选国家(美国)不符。然后它回来了
[7] pry(#<StripeManaged>)> ip => "::1"
. 我在谷歌上搜索了一下,但看不出“1”在我的语境中意味着什么。
-
在控制台上,我执行
[9] pry(#<StripeManaged>)> Stripe::Account.create(managed: true, country: country, email: org_person.email, tos_acceptance: {ip: ip, date: Time.now.to_i}, legal_entity: {type: 'individual'})
得到了
Stripe::InvalidRequestError: Missing required param: type.
我在网上找不到可参考的例子,但这似乎是我最怀疑的问题。