Merge branch 'master' of https://github.com/CactusPuppy/snapcon into 177195121-improve-email-templates

This commit is contained in:
Jimmy 2021-03-11 23:23:24 -08:00
commit fc0494c1f3
6 changed files with 116 additions and 21 deletions

View file

@ -16,6 +16,7 @@ class PaymentsController < ApplicationController
raise CanCan::AccessDenied.new('Nothing to pay for!', :new, Payment)
end
@has_registration_ticket = params[:has_registration_ticket]
@unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference)
end
@ -24,8 +25,15 @@ class PaymentsController < ApplicationController
if @payment.purchase && @payment.save
update_purchased_ticket_purchases
redirect_to conference_physical_tickets_path,
notice: 'Thanks! Your ticket is booked successfully.'
has_registration_ticket = params[:has_registration_ticket]
if has_registration_ticket == 'true'
redirect_to new_conference_conference_registration_path(@conference.short_title),
notice: 'Thanks! Your ticket is booked successfully. Please register for the conference.'
else
redirect_to conference_physical_tickets_path,
notice: 'Thanks! Your ticket is booked successfully.'
end
else
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
@unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference)

View file

@ -8,23 +8,42 @@ class TicketPurchasesController < ApplicationController
def create
current_user.ticket_purchases.by_conference(@conference).unpaid.destroy_all
# Create a ticket purchase which can be paid or unpaid
count_registration_tickets_before = current_user.count_registration_tickets(@conference)
message = TicketPurchase.purchase(@conference, current_user, params[:tickets].try(:first))
if message.blank?
if current_user.ticket_purchases.by_conference(@conference).unpaid.any?
redirect_to new_conference_payment_path,
notice: 'Please pay here to get tickets.'
elsif current_user.ticket_purchases.by_conference(@conference).paid.any?
redirect_to conference_physical_tickets_path,
notice: 'You already have tickets for the conference.'
elsif @conference.tickets.for_registration.any?
redirect_to conference_tickets_path(@conference.short_title),
error: 'Please get at least one ticket to continue.'
else
redirect_to conference_conference_registration_path(@conference.short_title)
end
else
# The new ticket_purchase has been added to the database. current_user.ticket_purchases contains the new one.
count_registration_tickets_after = current_user.count_registration_tickets(@conference)
# Failed to create ticket purchase
unless message.blank?
redirect_to conference_tickets_path(@conference.short_title),
error: "Oops, something went wrong with your purchase! #{message}"
return
end
# Current user already paid for a registration ticket and the current ticket purchase contains one
if count_registration_tickets_before == 1 && count_registration_tickets_after > 1
redirect_to conference_physical_tickets_path,
error: 'You already have one registration ticket for the conference.'
return
end
# User needs to pay for tickets if any of them is not free.
if current_user.ticket_purchases.by_conference(@conference).unpaid.any?
has_registration_ticket = count_registration_tickets_before.zero? && count_registration_tickets_after == 1
redirect_to new_conference_payment_path(has_registration_ticket: has_registration_ticket),
notice: 'Please pay here to get tickets.'
return
end
# Redirect to registration page for a user who didn't have a registration ticket and is purchasing one
if count_registration_tickets_before.zero? && count_registration_tickets_after == 1
redirect_to new_conference_conference_registration_path(@conference.short_title),
notice: 'Thanks! Your ticket is booked successfully. Please register for the conference.'
else
redirect_to conference_physical_tickets_path,
notice: 'Thanks! Your ticket is booked successfully.'
end
end

View file

@ -346,6 +346,17 @@ class User < ApplicationRecord
events.where(program_id: conference.program.id, 'event_users.event_role': 'volunteer')
end
def count_registration_tickets(conference)
count = 0
ticket_purchases.by_conference(conference).each do |ticket_purchase|
if ticket_purchase.ticket.registration_ticket
count += 1
end
end
count
end
def self.empty?
User.count == 1 && User.first.email == 'deleted@localhost.osem'
end

View file

@ -19,7 +19,7 @@
%td
= humanized_money_with_symbol ticket.quantity * ticket.price
= form_tag conference_payments_path do
= form_tag conference_payments_path(@conference.short_title, :has_registration_ticket => @has_registration_ticket) do
%script.stripe-button{'src': "https://checkout.stripe.com/checkout.js",
'data': {amount: @total_amount_to_pay.cents,
label: "Pay #{humanized_money_with_symbol @total_amount_to_pay}",