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 Nonprofits
|
2019-07-30 21:29:24 +00:00
|
|
|
class PayoutsController < ApplicationController
|
2020-05-11 18:38:50 +00:00
|
|
|
include Controllers::Nonprofit::Current
|
|
|
|
include Controllers::Nonprofit::Authorization
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
before_action :authenticate_nonprofit_admin!, only: :create
|
|
|
|
before_action :authenticate_nonprofit_user!, only: %i[index show]
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def create
|
|
|
|
payout = InsertPayout.with_stripe(current_nonprofit.id, {
|
|
|
|
stripe_account_id: current_nonprofit.stripe_account_id,
|
|
|
|
email: current_user.email,
|
|
|
|
user_ip: current_user.current_sign_in_ip,
|
|
|
|
bank_name: current_nonprofit.bank_account.name
|
2019-11-05 17:40:35 +00:00
|
|
|
}, before_date: payout_params[:before_date])
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
if payout['failure_message'].present?
|
|
|
|
flash[:notice] = "The payout failed: #{payout['failure_message']}"
|
|
|
|
render json: payout, status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
flash[:notice] = 'We successfully submitted your payout! View status and receipts below.'
|
|
|
|
render json: payout, status: :ok
|
|
|
|
end
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def index
|
|
|
|
@nonprofit = Nonprofit.find(params[:nonprofit_id])
|
|
|
|
@payouts = @nonprofit.payouts.order('created_at DESC')
|
|
|
|
balances = QueryPayments.nonprofit_balances(params[:nonprofit_id])
|
|
|
|
@available_total = balances['available_gross']
|
|
|
|
@pending_total = balances['pending_gross']
|
|
|
|
@can_make_payouts = @nonprofit.can_make_payouts
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# get /nonprofits/:nonprofit_id/payouts/:id
|
|
|
|
def show
|
|
|
|
payout = current_nonprofit.payouts.find(params[:id])
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render json: payout }
|
|
|
|
format.csv do
|
|
|
|
payments = QueryPayments.for_payout(params[:nonprofit_id], params[:id])
|
|
|
|
filename = "payout-#{payout.created_at.strftime('%m-%d-%Y')}"
|
|
|
|
send_data(Format::Csv.from_vectors(payments), filename: "#{filename}.csv")
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
2019-11-05 17:40:35 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def payout_params
|
|
|
|
params.permit(:before_date)
|
|
|
|
end
|
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|