houdini/app/controllers/recurring_donations_controller.rb
2020-06-15 10:26:57 -05:00

60 lines
2.8 KiB
Ruby

# frozen_string_literal: true
# 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
class RecurringDonationsController < ApplicationController
def edit
@data = QueryRecurringDonations.fetch_for_edit params[:id]
if @data && params[:t] == @data['recurring_donation']['edit_token']
@data['change_amount_suggestions'] = CalculateSuggestedAmounts.calculate(@data['recurring_donation']['amount'])
@data['miscellaneous_np_info'] = FetchMiscellaneousNpInfo.fetch(@data['nonprofit']['id'])
if @data['miscellaneous_np_info']['donate_again_url'].blank?
@data['miscellaneous_np_info']['donate_again_url'] = url_for(controller: :nonprofits, action: :show, id: @data['nonprofit']['id'], only_path: false)
end
respond_to do |format|
format.html
end
else
flash[:notice] = 'Unable to find donation. Please follow the exact link provided in your email'
redirect_to root_url
end
end
def destroy
@data = QueryRecurringDonations.fetch_for_edit params[:id]
if params[:edit_token] != @data['recurring_donation']['edit_token']
render json: { error: 'Invalid token' }, status: :unprocessable_entity
else
updated = UpdateRecurringDonations.cancel(params[:id], current_user ? current_user.email : @data['supporter']['email'])
render json: updated
end
end
def update
data = QueryRecurringDonations.fetch_for_edit params[:id]
if data && params[:edit_token] == data['recurring_donation']['edit_token']
data['supporter'] = UpdateSupporter.general_info(params[:supporter][:id], params[:supporter]) if params[:supporter]
data['recurring_donation'] ||= {}
data['recurring_donation'] = UpdateRecurringDonations.update_card_id(data['recurring_donation'], params[:token]) if params[:token]
data['recurring_donation'] = UpdateRecurringDonations.update_paydate(data['recurring_donation'], params[:paydate]) if params[:paydate]
render json: data, status: data.is_a?(ValidationError) ? :unprocessable_entity : :ok
else
render json: { error: 'Invalid token' }, status: :unprocessable_entity
end
end
def update_amount
rd = RecurringDonation.where('id = ?', params[:id]).first
if rd && params[:edit_token] == rd['edit_token']
begin
amount_response = UpdateRecurringDonations.update_amount(rd, params[:token], params[:amount])
flash[:notice] = "Your recurring donation amount has been successfully changed to $#{(amount_response.amount / 100).to_i}"
render_json { amount_response }
rescue StandardError => e
render_json { raise e }
end
else
render json: { error: 'Invalid token' }, status: :unprocessable_entity
end
end
end