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
|
|
|
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?
|
2019-07-30 21:29:24 +00:00
|
|
|
@data['miscellaneous_np_info']['donate_again_url'] = url_for(controller: :nonprofits, action: :show, id: @data['nonprofit']['id'], only_path: false)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
end
|
|
|
|
else
|
2019-07-30 21:29:24 +00:00
|
|
|
flash[:notice] = 'Unable to find donation. Please follow the exact link provided in your email'
|
2018-03-25 17:30:42 +00:00
|
|
|
redirect_to root_url
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@data = QueryRecurringDonations.fetch_for_edit params[:id]
|
|
|
|
if params[:edit_token] != @data['recurring_donation']['edit_token']
|
2019-07-30 21:29:24 +00:00
|
|
|
render json: { error: 'Invalid token' }, status: :unprocessable_entity
|
2018-03-25 17:30:42 +00:00
|
|
|
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
|
2019-07-30 21:29:24 +00:00
|
|
|
render json: { error: 'Invalid token' }, status: :unprocessable_entity
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_amount
|
|
|
|
rd = RecurringDonation.where('id = ?', params[:id]).first
|
|
|
|
if rd && params[:edit_token] == rd['edit_token']
|
|
|
|
begin
|
2019-07-30 21:29:24 +00:00
|
|
|
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}"
|
2018-03-25 17:30:42 +00:00
|
|
|
render_json { amount_response }
|
2019-07-30 21:29:24 +00:00
|
|
|
rescue StandardError => e
|
2018-03-25 17:30:42 +00:00
|
|
|
render_json { raise e }
|
|
|
|
end
|
|
|
|
else
|
2019-07-30 21:29:24 +00:00
|
|
|
render json: { error: 'Invalid token' }, status: :unprocessable_entity
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|