2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
require 'query/query_payments'
|
|
|
|
require 'fetch/stripe/fetch_stripe_account'
|
|
|
|
module UpdateNonprofit
|
|
|
|
# See the stripe docs for reference: => https://stripe.com/docs/connect/identity-verification
|
2019-07-30 21:29:24 +00:00
|
|
|
def self.verify_identity(np_id, legal_entity, tos = nil)
|
|
|
|
np = Qx.select('*').from(:nonprofits).where(id: np_id).execute.first
|
2018-03-25 17:30:42 +00:00
|
|
|
legal_entity[:address][:country] = 'US' if legal_entity[:address]
|
|
|
|
acct = FetchStripeAccount.with_account_id(np['stripe_account_id'])
|
|
|
|
acct.legal_entity.phone_number = acct.support_phone = legal_entity[:phone_number] if legal_entity[:phone_number]
|
|
|
|
acct.legal_entity.business_tax_id = legal_entity[:business_tax_id] if legal_entity[:business_tax_id]
|
|
|
|
acct.legal_entity.address = legal_entity[:address] if legal_entity[:address]
|
|
|
|
acct.legal_entity.first_name = legal_entity[:first_name] if legal_entity[:first_name]
|
|
|
|
acct.legal_entity.last_name = legal_entity[:last_name] if legal_entity[:last_name]
|
|
|
|
acct.legal_entity.dob = legal_entity[:dob] if legal_entity[:dob]
|
|
|
|
acct.legal_entity.ssn_last_4 = legal_entity[:ssn_last_4] if legal_entity[:ssn_last_4]
|
|
|
|
acct.legal_entity.personal_id_number = legal_entity[:personal_id_number] if legal_entity[:personal_id_number]
|
|
|
|
acct.legal_entity.type = 'company'
|
|
|
|
acct.legal_entity.business_name = np['name']
|
2019-07-30 21:29:24 +00:00
|
|
|
acct.tos_acceptance = tos if tos
|
2018-03-25 17:30:42 +00:00
|
|
|
acct.save
|
|
|
|
|
|
|
|
# Might as well update the nonprofit info
|
|
|
|
if legal_entity[:address] && legal_entity[:business_tax_id]
|
2019-07-30 21:29:24 +00:00
|
|
|
np = Qx.update(:nonprofits).set(
|
|
|
|
address: legal_entity[:address][:line1],
|
|
|
|
city: legal_entity[:address][:city],
|
|
|
|
state_code: legal_entity[:address][:state],
|
|
|
|
zip_code: legal_entity[:address][:postal_code],
|
|
|
|
ein: legal_entity[:business_tax_id],
|
|
|
|
verification_status: 'pending',
|
|
|
|
phone: legal_entity[:phone_number]
|
|
|
|
)
|
|
|
|
.where(id: np_id)
|
|
|
|
.returning('*')
|
|
|
|
.execute.first
|
2018-03-25 17:30:42 +00:00
|
|
|
else
|
|
|
|
np = Qx.update(:nonprofits).set(verification_status: 'pending').where(id: np_id).returning('*').first
|
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
np
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Update charges from pending to available if the nonprofit's balance on stripe can accommodate them
|
|
|
|
# First, get net balance on Stripe, then get net balance on CC
|
|
|
|
# Take the difference of those two, and mark as many oldest pending charges as 'available' as are less than or equal to that difference
|
|
|
|
def self.mark_available_charges(npo_id)
|
2019-07-30 21:29:24 +00:00
|
|
|
stripe_account_id = Qx.select('stripe_account_id').from(:nonprofits).where(id: npo_id).ex.first['stripe_account_id']
|
2018-03-25 17:30:42 +00:00
|
|
|
stripe_net_balance = Stripe::Balance.retrieve(stripe_account: stripe_account_id).available.first.amount
|
|
|
|
cc_net_balance = QueryPayments.get_payout_totals(QueryPayments.ids_for_payout(npo_id))['net_amount']
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
pending_payments = Qx.select('payments.net_amount', 'charges.id AS charge_id')
|
|
|
|
.from(:payments)
|
|
|
|
.where("charges.status='pending'")
|
|
|
|
.and_where('payments.nonprofit_id=$id', id: npo_id)
|
|
|
|
.join('charges', 'charges.payment_id=payments.id')
|
|
|
|
.order_by('payments.date ASC')
|
|
|
|
.execute
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
return if pending_payments.empty?
|
|
|
|
|
|
|
|
remaining_balance = stripe_net_balance - cc_net_balance
|
|
|
|
charge_ids = pending_payments.take_while do |payment|
|
|
|
|
if payment['net_amount'] <= remaining_balance
|
|
|
|
remaining_balance -= payment['net_amount']
|
|
|
|
true
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end.map { |h| h['charge_id'] }
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
Qx.update(:charges).set(status: 'available').where('id IN ($ids)', ids: charge_ids).execute if charge_ids.any?
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|