[feat] remove unintended check; add auto-redirect to non-free ticket

This commit is contained in:
Jimmy 2021-03-04 21:50:26 -08:00
parent 1ca58961c1
commit da8939dd3a
4 changed files with 24 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,17 @@ 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.present? && 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
@has_registration_ticket = nil
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

@ -25,30 +25,25 @@ class TicketPurchasesController < ApplicationController
# 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,
notice: 'You already have tickets for the conference.'
error: 'You already have one registration ticket for the conference.'
return
end
# Conference requires a registration ticket but the current user wants to purchase a non-registration ticket
# and does not have a registration ticket
if @conference.registration_ticket_required? && count_registration_tickets_after == 0
redirect_to conference_tickets_path(@conference.short_title),
error: 'Please get at least one registration ticket to continue.'
return
end
# Ticket purchase created but not paid
# User needs to pay for tickets if any of them is not free.
if current_user.ticket_purchases.by_conference(@conference).unpaid.any?
redirect_to new_conference_payment_path,
has_registration_ticket = count_registration_tickets_before == 0 && 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
# Current user didn't have a registration ticket and is purchasing one
# Redirect to registration page for a user who didn't have a registration ticket and is purchasing one
if count_registration_tickets_before == 0 && count_registration_tickets_after == 1
redirect_to new_conference_conference_registration_path(@conference.short_title)
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
redirect_to conference_physical_tickets_path,
notice: 'Thanks! Your ticket is booked successfully.'
end
end

View file

@ -349,10 +349,8 @@ class User < ApplicationRecord
def count_registration_tickets(conference)
count = 0
ticket_purchases.by_conference(conference).each do |ticket_purchase|
ticket_purchase.physical_tickets.each do |physical_ticket|
if physical_ticket.ticket.registration_ticket
count += 1
end
if ticket_purchase.ticket.registration_ticket
count += 1
end
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}",