Skip to content

Commit

Permalink
Payment cleanup
Browse files Browse the repository at this point in the history
Add amount paying to request and payment screens
fix changing amounts for special price with strip payment intent update
various logic fixes
  • Loading branch information
beingmattlevy committed May 13, 2024
1 parent ffb1248 commit 89f320f
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 38 deletions.
16 changes: 8 additions & 8 deletions app/controllers/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def create
def confirm
redirect_path, options = validate_payment_confirmation
if redirect_path
Rails.logger.error("#confirm() => redirect_path: #{redirect_path}")
Rails.logger.info("#confirm() => redirect_path: #{redirect_path}")
return redirect_to redirect_path, options || {}
else
Rails.logger.info("#confirm() => marking payment id #{@payment.id} as received")
Expand Down Expand Up @@ -86,7 +86,7 @@ def sent
Rails.logger.info("#sent() => @payment #{@payment.inspect}")

flash[:notice] = "We've recorded that your payment is en route"
redirect_to edit_event_ticket_request_path(@event, @ticket_request)
redirect_to event_ticket_requests_path(@event, @ticket_request)
end

private
Expand Down Expand Up @@ -173,20 +173,20 @@ def validate_payment

# ensure we have a valid payment in progress that is not received
def validate_payment_confirmation
if @payment.received?
Rails.logger.info("Payment Confirmation already received: #{@payment.id} status: #{@payment.status}")
return root_path, information: 'This payment request has been confirmed! Thank you!'
end

unless @payment.in_progress?
Rails.logger.info("Payment Confirmation not in progress: #{@payment.id} status: #{@payment.status}")
return root_path, alert: 'This payment request can not be confirmed'
end

if @payment.received?
Rails.logger.info("Payment Confirmation already received: #{@payment.id} status: #{@payment.status}")
return root_path, alert: 'This payment request has already been confirmed.'
end

# 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 can not be confirmed.'
return root_path, alert: 'This payment request is missing the stripe payment.'
end

nil
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/ticket_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ def show

# if ticket_request is approved, but guests are not filled out
# we could redirect to the ticket request edit path
if @ticket_request.approved? && !@ticket_request.all_guests_specified?
if !@event.admin?(current_user) && @ticket_request.approved? && !@ticket_request.all_guests_specified?
return redirect_to "#{edit_event_ticket_request_path(@event, @ticket_request)}#guests",
alert: 'Please fill out your guest names and guest emails before purchasing a ticket.'
end

@payment = @ticket_request.payment
end

Expand Down Expand Up @@ -175,7 +176,7 @@ def update

Rails.logger.info("ticket_request_params: #{ticket_request_params.inspect}")

if guests.size != @ticket_request.total_tickets
if !@event.admin?(current_user) && guests.size != @ticket_request.total_tickets
flash.now[:error] = 'Please enter each guest and kid in your party. For the kids include their ages, instead of the emails.'
return render_flash(flash)
end
Expand Down
19 changes: 17 additions & 2 deletions app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def save_with_payment_intent
# calculate cost from Ticket Request
cost = calculate_cost

Rails.logger.debug { "save_with_payment_intent: cost => #{cost}}" }

begin
# Create new Stripe PaymentIntent
self.payment_intent = create_payment_intent(cost)
Expand Down Expand Up @@ -115,14 +117,19 @@ def create_payment_intent(amount)
automatic_payment_methods: { enabled: true },
description: "#{ticket_request.event.name} Tickets",
metadata: {
event_id: ticket_request.event.id,
event_name: ticket_request.event.name,
ticket_request_id: ticket_request.id,
ticket_request_user_id: ticket_request.user_id,
event_id: ticket_request.event.id,
event_name: ticket_request.event.name
payment_id: id
}
})
end

def update_payment_intent_amount(amount)
self.payment_intent = Stripe::PaymentIntent.update(stripe_payment_id, { amount: })
end

def payment_intent_client_secret
payment_intent&.client_secret
end
Expand All @@ -131,6 +138,14 @@ def retrieve_payment_intent
return unless stripe_payment_id

self.payment_intent = Stripe::PaymentIntent.retrieve(stripe_payment_id)

# check if we have the same cost
if payment_intent.amount != (amount = calculate_cost)
self.payment_intent = update_payment_intent_amount(amount)
Rails.logger.debug { "retrieve_payment_intent updated payment intent with new amount [#{amount}] => #{payment_intent}}" }
end

Rails.logger.debug { "retrieve_payment_intent payment => #{inspect}}" }
end

def payment_in_progress?
Expand Down
6 changes: 2 additions & 4 deletions app/views/payments/_payment.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
- if payment.received?
%p Thanks for purchasing your tickets for #{ticket_request.event.name}!

%p Adults Tickets:
%strong
%p Adults Tickets:
= ticket_request.adults

%p
%p Kids Tickets:
%strong
%p Kids Tickets:
= ticket_request.kids

%hr
Expand Down
2 changes: 1 addition & 1 deletion app/views/payments/confirm.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

%h3 Your Payment has Been Confirmed! FnYay!!
%h4 Your Payment has Been Confirmed! FnYay!!

= render partial: "payment", locals: { payment: @payment }
15 changes: 14 additions & 1 deletion app/views/payments/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
= @ticket_request.adults
= 'ticket'.pluralize(@ticket_request.total_tickets)
has been approved!

%td.text-end.small.p-2.px-4.text-nowrap= "Total Ticket Price:"
%td
%strong
%span{ class: ('label label-info' if @ticket_request.special_price) }
= number_to_currency(@ticket_request.price, precision: 0)
%p

%p
Now all that's left is to pay for your tickets.
If you are paying via credit card, please fill out the form ➜
Expand All @@ -36,7 +44,12 @@
.col-sm-12.col-md-12.col-lg-8.col-xl-8
.card.my-2
.card-header.bg-body-secondary
%h4 Please Enter Your Payment Details
%h5 Please Enter Your Payment Details
%strong
Total Ticket Price:
= number_to_currency(@ticket_request.price, precision: 0)
%p

.card-body
%div#stripe-form{"data-controller" => "checkout",
"data-checkout-site-url-value" => "#{controller.send(:site_url)}",
Expand Down
8 changes: 4 additions & 4 deletions app/views/ticket_requests/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
%li DJ Carl Cox (as himself) <[email protected]>
%li John Digweed <[email protected]>
%li Kid: Taylor Swift (11yo kid, requires no supervision)
%li Kid: Jonah Hill (12yo kid with bad manners)
%li Kid: Channing Tatum (5-month old baby, still breast-fed)
%li Kid: Jonah Hill (12yo kid with socks untied)
%li Kid: Channing Tatum (5-month old baby, cute and funny)
- list_finalized = (@event.start_time - Time.current) < ::Event::GUEST_LIST_FINAL_WITHIN
- if list_finalized
Expand All @@ -71,7 +71,7 @@
%br


%h5 How are you contributing to the campout this year?
%h5 How are you contributing to the event?
.content-fluid
.row
.col-lg-6.col-xl-6.col-md-12.col-sm-12
Expand Down Expand Up @@ -130,7 +130,7 @@

- if signed_in? && @event.admin?(current_user) && form_action_name == :update
.well
= f.label :special_price, 'Special Total Price ($)'
= f.label :special_price, 'Total Special Price ($)'

%p.text-error
Editable by event admins only. Leave blank to use default price.
Expand Down
8 changes: 4 additions & 4 deletions app/views/ticket_requests/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@
- if @event.eald?
%th.bg-dark-subtle EA/LD
%th.bg-dark-subtle.text-right Price
%th.bg-dark-subtle Date Requested
%th.bg-dark-subtle Status
%th.bg-dark-subtle
%th.bg-dark-subtle.text-center Date Requested
%th.bg-dark-subtle.text-right Status
%th.bg-dark-subtle.text-center Payment
%tbody
- @ticket_requests.each do |ticket_request|
%tr{ class: class_for_table_row(ticket_request) }
Expand Down Expand Up @@ -166,7 +166,7 @@
Resend Approval
= button_to decline_event_ticket_request_path(@event, ticket_request),
method: :post,
class: 'btn btn-primary btn-sm ',
class: 'btn btn-danger btn-sm ',
data: { confirm: "Are you sure you want to decline #{ticket_request.user.name}'s already approved request?" } do
%span.text-error Decline
= button_to manual_confirmation_event_ticket_request_payments_path(@event, ticket_request),
Expand Down
26 changes: 14 additions & 12 deletions app/views/ticket_requests/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
= @ticket_request.status_name

- if @ticket_request.can_purchase?
%tr
%td.text-end.small.p-2.px-4.text-nowrap= "Total Tickets Price:"
%td
%strong
%span{ class: ('label label-info' if @ticket_request.special_price) }
= number_to_currency(@ticket_request.price, precision: 0)

%tr
%td.text-end.small.p-2.px-4.text-nowrap= "Payment:"
%td
Expand All @@ -72,7 +79,7 @@
- if @ticket_request.refunded?
%span.bg-success Refunded
- else
.bg-success
.text-success
= link_to 'Received', event_ticket_request_payment_path(@payment, @ticket_request)

- if @event.admin?(current_user)
Expand All @@ -86,9 +93,12 @@
Free
- else
- if @event.ticket_sales_open?
= link_to 'Purchase your ticket now',
new_event_ticket_request_payment_path(@event, @ticket_request),
class: 'btn btn-primary m-0'
- if @ticket_request.payment.present? && @ticket_request.payment.explanation.present?
%span.bg-warning Paying with #{@ticket_request.payment.explanation}
- else
= link_to 'Purchase your ticket now',
new_event_ticket_request_payment_path(@event, @ticket_request),
class: 'btn btn-primary m-0'
- else
%span.bg-warning
Unfortunately, ticket sales are now closed. You can no longer purchase your
Expand Down Expand Up @@ -146,14 +156,6 @@
%strong Cabins:
= @ticket_request.cabins

- if @event.admin?(current_user)
%tr
%td.text-end.small.p-2.px-4.text-nowrap= "Total Tickets Price:"
%td
%strong
%span{ class: ('label label-info' if @ticket_request.special_price) }
= number_to_currency(@ticket_request.price, precision: 0)

- if @ticket_request.previous_contribution.present?
%tr
%td.text-end.small.p-2.px-4.text-nowrap= "Contribution(s) last year:"
Expand Down

0 comments on commit 89f320f

Please sign in to comment.