· 9 years ago · Sep 26, 2016, 09:40 PM
1class StripeManaged < Struct.new( :user )
2
3 ALLOWED = [ 'US', 'FI' ]
4 COUNTRIES = [
5 { name: 'United States', code: 'US' },
6 { name: 'Finland', code: 'FI' },
7 ]
8 def create_account!(company, admin, tos_accepted, ip)
9 return nil unless tos_accepted
10 country_code, currency = country_info(company.company_country)
11 @account = Stripe::Account.create(
12 stripe_account_info(company, country_code, currency, ip, admin)
13 )
14 puts @account
15 update_company_account(@account) if @account
16 @account
17 end
18
19 def update_account!( params: nil )
20 if params
21 if params[:bank_account_token]
22 account.bank_account = params[:bank_account_token]
23 account.save
24 end
25
26 if params[:legal_entity]
27 # clean up dob fields
28 params[:legal_entity][:dob] = {
29 year: params[:legal_entity].delete('dob(1i)'),
30 month: params[:legal_entity].delete('dob(2i)'),
31 day: params[:legal_entity].delete('dob(3i)')
32 }
33
34 # update legal_entity hash from the params
35 params[:legal_entity].entries.each do |key, value|
36 if [ :address, :dob ].include? key.to_sym
37 value.entries.each do |akey, avalue|
38 next if avalue.blank?
39 # Rails.logger.error "#{akey} - #{avalue.inspect}"
40 account.legal_entity[key] ||= {}
41 account.legal_entity[key][akey] = avalue
42 end
43 else
44 next if value.blank?
45 # Rails.logger.error "#{key} - #{value.inspect}"
46 account.legal_entity[key] = value
47 end
48 end
49
50 # copy 'address' as 'personal_address'
51 pa = account.legal_entity['address'].dup.to_h
52 account.legal_entity['personal_address'] = pa
53
54 account.save
55 end
56 end
57
58 user.update_attributes(
59 stripe_account_status: account_status
60 )
61 end
62
63 def legal_entity
64 account.legal_entity
65 end
66
67 def needs?( field )
68 user.stripe_account_status['fields_needed'].grep( Regexp.new( /#{field}/i ) ).any?
69 end
70
71 def supported_bank_account_countries
72 country_codes = case account.country
73 when 'US' then %w{ US }
74 when 'FI' then %w{ FI }
75 end
76 COUNTRIES.select do |country|
77 country[:code].in? country_codes
78 end
79 end
80
81 protected
82
83 def account_status
84 {
85 details_submitted: account.details_submitted,
86 charges_enabled: account.charges_enabled,
87 transfers_enabled: account.transfers_enabled,
88 fields_needed: account.verification.fields_needed,
89 due_by: account.verification.due_by
90 }
91 end
92
93 def account
94 @account ||= Stripe::Account.retrieve(user.stripe_user_id)
95 end
96
97 def country_info(country)
98 case country
99 when 'Finland'
100 %w(FI eur)
101 when 'United States', 'USA'
102 %w(USA eur)
103 end
104 end
105
106 def stripe_account_info(company, country_code, currency, ip, admin)
107 {
108 managed: true,
109 country: country_code,
110 business_name: company.company_legal_name,
111 business_url: company.company_website,
112 external_account: {
113 object: 'bank_account',
114 account_number: company.company_iban,
115 country: country_code,
116 currency: currency,
117 account_holder_name: company.company_legal_name,
118 account_holder_type: 'company'
119 },
120 legal_entity: {
121 additional_owners: nil,
122 dob: {
123 day: admin.admin_birth_day,
124 month: admin.admin_birth_month,
125 year: admin.admin_birth_year
126 },
127 first_name: admin.first_name,
128 last_name: admin.last_name,
129 personal_address: {
130 city: company.company_city,
131 postal_code: company.company_zip,
132 line1: company.company_street_address
133 },
134 address: {
135 city: company.company_city,
136 country: country_code,
137 line1: company.company_street_address,
138 postal_code: company.company_zip
139 },
140 business_name: company.company_legal_name,
141 business_vat_id: company.company_tax_id,
142 business_tax_id: company.company_tax_id,
143 phone_number: company.company_phone,
144 type: 'company'
145 },
146 tos_acceptance: {
147 ip: ip,
148 date: Time.now.to_i
149 }
150 }
151 end
152
153 def update_company_account(stripe_account)
154 user.update_attributes(
155 currency: stripe_account.default_currency,
156 stripe_account_type: 'managed',
157 stripe_user_id: stripe_account.id,
158 secret_key: stripe_account.keys.secret,
159 publishable_key: stripe_account.keys.publishable,
160 stripe_account_status: account_status
161 )
162 end
163end