Created
February 17, 2022 14:44
-
-
Save cjavilla-stripe/62061d9bb6aafcae19c5e5a4651c7edb to your computer and use it in GitHub Desktop.
Create the connect and financial accounts for the user lazily, any time they are committed to db
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# After commit, we always check to make sure each user has both | |
# a Stripe Account and a Financial Account for that Stripe Account. | |
after_commit :ensure_stripe_account | |
after_commit :ensure_stripe_financial_account | |
def ensure_stripe_account | |
return if stripe_account_id.present? | |
# Create the Connected account | |
# This must be a custom connect account, but for | |
# Simple Treasury, you can also use Standard. | |
# There is currently no support for Treasury on Express. | |
account = Stripe::Account.create( | |
type: 'custom', | |
country: 'US', | |
email: email, | |
business_type: 'non_profit', | |
metadata: { | |
user_id: id, | |
}, | |
capabilities: { | |
card_payments: { requested: true }, | |
transfers: { requested: true }, | |
# These are the new capabilities we're requesting | |
# See the config/initializers/stripe.rb to see the | |
# beta headers in this case: `money_flows_beta=v1` | |
treasury: { requested: true }, | |
card_issuing: { requested: true }, | |
}, | |
individual: { | |
first_name: 'Jenny', | |
last_name: 'Rosen', | |
email: email, | |
dob: { | |
day: '1', | |
month: '1', | |
year: '1901' | |
}, | |
ssn_last_4: '0000', | |
phone: '8005551212', | |
address: { | |
line1: 'address_full_match', | |
line2: '', | |
city: 'San Francisco', | |
state: 'CA', | |
postal_code: '94103', | |
country: 'US' | |
} | |
} | |
) | |
self.update(stripe_account_id: account.id) | |
end | |
def ensure_stripe_financial_account | |
return if stripe_financial_account_id.present? | |
financial_account = Stripe::FinancialAccount.create({ | |
supported_currencies: ['usd'], | |
features: { | |
card_issuing: { requested: true }, | |
deposit_insurance: { requested: true }, | |
financial_addresses: { aba: { requested: true } }, | |
inbound_transfers: { ach: { requested: true } }, | |
intra_stripe_flows: { requested: true }, | |
outbound_payments: { | |
ach: { requested: true }, | |
us_domestic_wire: { requested: true } | |
}, | |
outbound_transfers: { | |
ach: { requested: true }, | |
us_domestic_wire: { requested: true } | |
}, | |
} | |
}, { | |
# Note we're creating the FinancialAccount _on_ the Connected Account. | |
stripe_account: stripe_account_id | |
}) | |
self.update(stripe_financial_account_id: financial_account.id) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment