我编写了一个功能,让电子商务网站成员为他们创建自己的Stripe帐户,以便能够通过Stripe接收付款。我按照在线教程但到目前为止没有工作(导致从stripe_controller.rb调用flash[:danger] = "Unable to create Stripe account!"
)。代码和问题如下。
应用程序/控制器/ stripe_controller.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
应用程序/服务/ stripe_managed.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
问题应该是stripe_managed.rb中的def create_account!
将@account
作为nil
返回。因此我怀疑Stripe::Account.create
失败了。我在这里想到了3个问题。
我目前住在台湾,Stripe尚未支持,并使用Stripe Japan的帐户管理该平台。我提到的教程来自北美,因此最初只关注美国和加拿大,然后我在哈希COUNTRIES
中添加了台湾。但由于Stripe不支持台湾,我继续Stripe::Account.create
与“美国”作为所在国家。
由于问题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.
我无法在网上找到可参考的示例,但这对我来说似乎是最可疑的问题。
如果有人能帮助我解决这个问题会很棒。提前谢谢。
答案 0 :(得分:0)
此处的问题是您尝试通过API创建自定义帐户,但您发送了错误的参数。以这种方式创建帐户时,您应该通过type: "custom"
。参数managed
自2017-05-25以来已被弃用,并且在较新的帐户中不受支持。
您的代码应为:
Stripe::Account.create(
type: "custom",
country: country,
email: org_person.email,
tos_acceptance: {
ip: ip,
date: Time.now.to_i
},
legal_entity: {
type: 'individual'
}
)
对Stripe发出的所有API请求都会返回有效资源,或者在发生错误时引发异常。您的代码需要在调试时至少记录这些错误,以便您可以轻松跟踪Stripe的问题。您还可以直接在dashboard的日志中查看所有API请求。这样可以让您看到发送的参数以及API容易返回的内容。