Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mfl/fix manual payment #263

Merged
merged 29 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6643c2d
add Payment provider and status enum
beingmattlevy Aug 6, 2024
744f2de
clean unused code
beingmattlevy Aug 6, 2024
2b66f24
remove unused payment helper
beingmattlevy Aug 6, 2024
14c5e3a
payment model updates
beingmattlevy Aug 6, 2024
797da9f
payment controller and views
beingmattlevy Aug 6, 2024
02400e1
payments specs
beingmattlevy Aug 6, 2024
921f2a9
cleanup cancel payment intent
beingmattlevy Aug 6, 2024
20d8b11
add cancel payment intent
beingmattlevy Aug 6, 2024
1782d4e
add refund check
beingmattlevy Aug 6, 2024
1152185
payment updates
beingmattlevy Aug 6, 2024
aca8499
cleanup
beingmattlevy Aug 6, 2024
d181e81
cleanup
beingmattlevy Aug 6, 2024
3bbdaa7
cleanup view logic for ticket request
beingmattlevy Aug 6, 2024
ea9393f
Merge branch 'main' into mfl/fix-manual-payment
beingmattlevy Aug 15, 2024
80d3b05
fix for payment provider other
beingmattlevy Aug 16, 2024
059a5cf
Fix find for ticket request
beingmattlevy Aug 18, 2024
9078884
cleanup
beingmattlevy Aug 18, 2024
f95cce9
show correct status
beingmattlevy Aug 18, 2024
d019d4c
refactor to be able to delete ticket request by admin
beingmattlevy Aug 18, 2024
46068e8
cleanup
beingmattlevy Aug 18, 2024
b553b4c
don't show re-approve if event has no approval
beingmattlevy Aug 19, 2024
1bdad7d
wording
beingmattlevy Aug 19, 2024
fd4d430
fix slug parse
beingmattlevy Aug 19, 2024
04f5122
fix space
beingmattlevy Aug 19, 2024
3f2abb5
bump version
beingmattlevy Aug 19, 2024
613d456
fix stats to include donations
beingmattlevy Aug 20, 2024
573eb27
wording
beingmattlevy Aug 20, 2024
7f47a37
rename for readability
beingmattlevy Aug 20, 2024
596ab50
clean up comments
beingmattlevy Aug 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Rails/BulkChangeTable:
- 'db/migrate/20240728211432_remove_ea_ld.rb'
- 'db/migrate/20240728223048_event_prices_as_integers.rb'
- 'db/migrate/20240729210234_ticket_requests_cleanup.rb'
- 'db/migrate/20240806011401_payment_provider.rb'

# Offense count: 3
# Configuration parameters: Include.
Expand Down
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.5
1.3.6
15 changes: 11 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ def stripe_publishable_api_key
end

def set_event
Rails.logger.debug { "#set_event() permitted params: #{permitted_params.inspect}" }

event_id = permitted_params[:event_id].to_i
event_slug = permitted_params[:event_id].delete("#{event_id}-")
event_slug = permitted_params[:event_id].sub("#{event_id}-", '')

Rails.logger.debug { "#set_event() => event_id = #{event_id}, event_slug = #{event_slug} params[:event_id] => #{permitted_params[:event_id]}" }

Expand All @@ -43,8 +45,7 @@ def set_event
end

@event = Event.where(id: event_id).first

if @event.slug != event_slug
if @event&.slug != event_slug
Rails.logger.warn("Event slug mismatch: [#{event_slug}] != [#{@event&.slug}]")
end

Expand All @@ -59,7 +60,13 @@ def set_ticket_request
Rails.logger.debug { "#set_ticket_request() => ticket_request_id = #{ticket_request_id}" }
return unless ticket_request_id

@ticket_request = TicketRequest.find(ticket_request_id)
# check if ticket request exists
@ticket_request = TicketRequest.find_by(id: ticket_request_id)
if @ticket_request.blank?
Rails.logger.info { "#set_ticket_request() => unknown ticket_request_id: #{ticket_request_id}" }
return redirect_to root_path
end

Rails.logger.debug { "#set_ticket_request() => @ticket_request = #{@ticket_request&.inspect}" }

redirect_to @event unless @ticket_request.event == @event
Expand Down
44 changes: 21 additions & 23 deletions app/controllers/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ class PaymentsController < ApplicationController
before_action :set_event
before_action :set_ticket_request
before_action :set_payment
before_action :validate_payment, except: [:confirm]
before_action :initialize_payment, except: %i[show confirm other]
before_action :validate_payment, except: %i[confirm]

def show
Rails.logger.debug { "#show() => @ticket_request = #{@ticket_request&.inspect} params: #{params}" }
Expand All @@ -15,17 +16,13 @@ def show

def new
Rails.logger.debug { "#new() => @ticket_request = #{@ticket_request&.inspect}" }
initialize_payment
end

# Creates new Payment
# Create Payment Intent and save PaymentIntentId in Payment
def create
Rails.logger.debug { "#create() => @ticket_request = #{@ticket_request&.inspect} params: #{params}" }

# init the payment, user from ticket request
initialize_payment

# check to see if we have an existing stripe payment intent. retrieve or save
@payment.retrieve_or_save_payment_intent

Expand Down Expand Up @@ -62,14 +59,19 @@ def confirm
mark_payment_completed
end

# init and mark the payment as completed.
# human has marked the payment as confirmed received and completed.
# Annotate that manual was set
def manual_confirmation
initialize_payment
@payment.update(explanation: 'manual confirm')
@payment.reload
Rails.logger.info("#manual_confirm() => @payment #{@payment.inspect}")
# cancel any stripe payment intent, so there is no orphaned intent
@payment.cancel_payment_intent

# update payment with explanation.
@payment.update(explanation: 'Marked Received', provider: :other)

# mark payment and ticket request completed
mark_payment_completed
Rails.logger.info("#manual_confirmation() => @payment #{@payment.inspect}")

redirect_to event_ticket_requests_path(@event, @ticket_request)
end

Expand All @@ -78,9 +80,13 @@ def other
end

# handle other forms of payment than credit card / stripe
# set payment provider and cancel the Stripe PaymentIntent
def sent
initialize_payment
@payment.update(explanation: permitted_params[:explanation], status: Payment::STATUS_IN_PROGRESS)
# set explanation, provider, status.
@payment.update(explanation: permitted_params[:explanation],
status: :in_progress,
provider: :other)
@payment.cancel_payment_intent
@payment.reload

Rails.logger.info("#sent() => @payment #{@payment.inspect}")
Expand All @@ -93,6 +99,7 @@ def sent

def mark_payment_completed
Payment.transaction do
# if we have a payment explanation, mark as other. else mark received
@payment.mark_received
@payment.ticket_request.mark_complete
@payment.reload
Expand Down Expand Up @@ -128,16 +135,6 @@ def set_payment
end
end

# initialize payment and save stripe payment intent
def save_payment_intent
initialize_payment
return redirect_to root_path unless @payment.present? && @payment.can_view?(current_user)

@payment.save_with_payment_intent.tap do |result|
flash.now[:error] = @payment.errors.full_messages.to_sentence unless result
end
end

def initialize_payment
@stripe_publishable_api_key ||= Rails.configuration.stripe[:publishable_api_key]

Expand Down Expand Up @@ -186,7 +183,7 @@ def validate_payment_confirmation
# has to have a stripe payment id and payment must not already be received
unless @payment.stripe_payment?
Rails.logger.error("Invalid payment confirmation id: #{@payment.id} missing stripe_payment_id")
return root_path, alert: 'This payment request is missing the stripe payment.'
return root_path, alert: 'This payment request is not a valid stripe payment.'
end

nil
Expand All @@ -199,6 +196,7 @@ def permitted_params
:ticket_request_id,
:stripe_payment_id,
:payment_intent,
:provider,
:explanation,
payment: %i[
ticket_request
Expand Down
17 changes: 6 additions & 11 deletions app/controllers/ticket_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ def index
requests: requests.count,
adults: requests.sum(&:adults),
kids: requests.sum(&:kids),
donations: requests.sum(&:donation),
addon_passes: requests.sum(&:active_addon_pass_sum),
addon_camping: requests.sum(&:active_addon_camp_sum),
raised: requests.sum(&:price)
raised: requests.sum(&:cost)
}
end

@stats[:total] ||= Hash.new { |h, k| h[k] = 0 }
%i[requests adults kids addon_passes addon_camping raised].each do |measure|
%i[requests adults kids donations addon_passes addon_camping raised].each do |measure|
%i[pending awaiting_payment completed].each do |status|
@stats[:total][measure] += @stats[status][measure]
end
Expand Down Expand Up @@ -182,19 +183,13 @@ def update
def destroy
Rails.logger.error("destroy# params: #{permitted_params}".colorize(:yellow))

unless @event.admin?(current_user) || current_user == @ticket_request.user
Rails.logger.error("ATTEMPT TO DELETE TICKET REQUEST #{@ticket_request} by #{current_user}".colorize(:red))
flash.now[:error] = 'You do not have sufficient privileges to delete this request.'
return render_flash(flash)
end

if @ticket_request.payment_received?
flash.now[:error] = 'Can not delete request when payment has been received. It must be refunded instead.'
unless @event.admin?(current_user) || @ticket_request.can_be_cancelled?(by_user: current_user)
Rails.logger.warn("Failed to delete ticket request #{@ticket_request} status: #{@ticket_request.status_name} by #{current_user}".colorize(:red))
flash.now[:error] = 'You do not have permission to delete this ticket'
return render_flash(flash)
end

@ticket_request.destroy! if @ticket_request&.persisted?

redirect_to new_event_ticket_request_path(@event), notice: 'Ticket Request was successfully cancelled.'
end

Expand Down
7 changes: 0 additions & 7 deletions app/helpers/payments_helper.rb

This file was deleted.

2 changes: 0 additions & 2 deletions app/mailers/payment_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

class PaymentMailer < ApplicationMailer
include PaymentsHelper

def payment_received(payment)
self.payment = payment

Expand Down
Loading