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
|
|
|
module Nonprofits
|
2019-07-30 21:29:24 +00:00
|
|
|
class DonationsController < ApplicationController
|
|
|
|
include Controllers::NonprofitHelper
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
before_action :authenticate_nonprofit_user!, only: %i[index update]
|
|
|
|
before_action :authenticate_campaign_editor!, only: [:create_offsite]
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# get /nonprofit/:nonprofit_id/donations
|
|
|
|
def index
|
|
|
|
redirect_to controller: :payments, action: :index
|
|
|
|
end # def index
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# post /nonprofits/:nonprofit_id/donations
|
|
|
|
def create
|
|
|
|
if params[:token]
|
2019-08-06 14:06:02 +00:00
|
|
|
donations_params[:token] = params[:token]
|
|
|
|
render_json { InsertDonation.with_stripe(donations_params, current_user) }
|
2019-07-30 21:29:24 +00:00
|
|
|
elsif params[:direct_debit_detail_id]
|
2019-08-06 14:06:02 +00:00
|
|
|
render JsonResp.new(donations_params) do |_data|
|
2019-07-30 21:29:24 +00:00
|
|
|
requires(:amount).as_int
|
|
|
|
requires(:supporter_id, :nonprofit_id)
|
|
|
|
# TODO
|
|
|
|
# requires_either(:card_id, :direct_debit_detail_id).as_int
|
|
|
|
optional(:dedication, :designation).as_string
|
|
|
|
optional(:campaign_id, :event_id).as_int
|
|
|
|
end.when_valid do |data|
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
InsertDonation.with_sepa(data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# post /nonprofits/:nonprofit_id/donations/create_offsite
|
|
|
|
def create_offsite
|
2019-08-06 14:06:02 +00:00
|
|
|
render JsonResp.new(donations_params) do |_data|
|
2018-03-25 17:30:42 +00:00
|
|
|
requires(:amount).as_int.min(1)
|
|
|
|
requires(:supporter_id, :nonprofit_id).as_int
|
|
|
|
optional(:dedication, :designation).as_string
|
|
|
|
optional(:campaign_id, :event_id).as_int
|
|
|
|
optional(:date).as_date
|
2019-07-30 21:29:24 +00:00
|
|
|
optional(:offsite_payment).nested do
|
2018-03-25 17:30:42 +00:00
|
|
|
optional(:kind).one_of('cash', 'check')
|
|
|
|
optional(:check_number)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
|
|
|
end.when_valid { |data| InsertDonation.offsite(data) }
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def update
|
2019-08-06 14:06:02 +00:00
|
|
|
render_json { UpdateDonation.update_payment(params[:id], donations_params) }
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# put /nonprofits/:nonprofit_id/donations/:id
|
|
|
|
# update designation, dedication, or comment on a donation in the followup
|
|
|
|
def followup
|
|
|
|
nonprofit = Nonprofit.find(params[:nonprofit_id])
|
|
|
|
donation = nonprofit.donations.find(params[:id])
|
2019-08-06 14:06:02 +00:00
|
|
|
json_saved UpdateDonation.from_followup(donation, donations_params)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
# this is a special, weird case
|
|
|
|
private
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def current_campaign
|
2019-08-06 14:06:02 +00:00
|
|
|
if !@campaign && donations_params && donations_params[:campaign_id]
|
|
|
|
@campaign = Campaign.where('id = ? ', donations_params[:campaign_id]).first
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
|
|
|
@campaign
|
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def current_campaign_editor?
|
|
|
|
!params[:preview] && (current_nonprofit_user? || (current_campaign && current_role?(:campaign_editor, current_campaign.id)) || current_role?(:super_admin))
|
|
|
|
end
|
2018-08-30 20:04:11 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def authenticate_campaign_editor!
|
|
|
|
unless current_campaign_editor?
|
|
|
|
block_with_sign_in 'You need to be a campaign editor to do that.'
|
|
|
|
end
|
|
|
|
end
|
2019-08-06 14:06:02 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def donations_params
|
|
|
|
params.require(:donation).permit(:date, :amount, :recurring, :anonymous, :email, :designation, :dedication, :comment, :origin_url, :nonprofit_id, :card_id, :supporter_id, :profile_id, :campaign_id, :payment_id, :event_id, :direct_debit_detail_id, :payment_provider)
|
|
|
|
end
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|