2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-12 20:03:43 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
|
|
|
|
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
|
2018-03-25 17:30:42 +00:00
|
|
|
module InsertBankAccount
|
|
|
|
# @param [Nonprofit] nonprofit
|
|
|
|
#
|
|
|
|
# stripe_bank_account_token: data.stripe_resp.id,
|
2019-07-30 21:29:24 +00:00
|
|
|
# stripe_bank_account_id: data.stripe_resp.bank_account.id,
|
|
|
|
# name: data.stripe_resp.bank_account.bank_name + ' *' + data.stripe_resp.bank_account.last4,
|
|
|
|
# email: app.user.email
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
def self.with_stripe(nonprofit, user, params)
|
2019-07-30 21:29:24 +00:00
|
|
|
ParamValidation.new({ nonprofit: nonprofit, user: user },
|
|
|
|
nonprofit: {
|
|
|
|
required: true,
|
|
|
|
is_a: Nonprofit
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
required: true,
|
|
|
|
is_a: User
|
|
|
|
})
|
|
|
|
ParamValidation.new(params || {},
|
|
|
|
stripe_bank_account_token: {
|
|
|
|
required: true,
|
|
|
|
not_blank: true
|
|
|
|
})
|
|
|
|
|
|
|
|
unless nonprofit.vetted
|
|
|
|
raise ArgumentError, "#{nonprofit.id} is not vetted."
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
stripe_acct = Stripe::Account.retrieve(StripeAccount.find_or_create(nonprofit.id))
|
|
|
|
nonprofit.reload
|
2019-07-30 21:29:24 +00:00
|
|
|
# this shouldn't be possible but we'll check any who
|
|
|
|
if nonprofit.stripe_account_id.blank?
|
|
|
|
raise ArgumentError, "#{nonprofit.id} does not have a valid stripe_account_id associated with it"
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Qx.transaction do
|
2019-07-30 21:29:24 +00:00
|
|
|
ba = stripe_acct.external_accounts.create(external_account: params[:stripe_bank_account_token])
|
|
|
|
ba.default_for_currency = true
|
|
|
|
ba.save
|
|
|
|
|
|
|
|
BankAccount.where('nonprofit_id = ?', nonprofit.id).update_all(deleted: true)
|
|
|
|
|
|
|
|
bank_account = BankAccount.create(
|
|
|
|
stripe_bank_account_id: ba.id,
|
|
|
|
stripe_bank_account_token: params[:stripe_bank_account_token],
|
|
|
|
confirmation_token: SecureRandom.uuid,
|
|
|
|
nonprofit: nonprofit,
|
|
|
|
name: params[:name] || "Bank #{SecureRandom.uuid}",
|
|
|
|
email: user.email,
|
|
|
|
pending_verification: true
|
|
|
|
)
|
|
|
|
|
2019-11-07 19:58:09 +00:00
|
|
|
BankAccountCreateJob.perform_later(bank_account)
|
2019-07-30 21:29:24 +00:00
|
|
|
return bank_account
|
|
|
|
rescue Stripe::StripeError => error
|
|
|
|
params[:failure_message] = "Failed to connect the bank account: #{error.inspect}"
|
|
|
|
raise ArgumentError, "Failed to connect the bank account: #{error.inspect}"
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|