diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index 5dae6d19..d2317bbc 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -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) diff --git a/app/controllers/ticket_purchases_controller.rb b/app/controllers/ticket_purchases_controller.rb index 865459f4..58e32feb 100644 --- a/app/controllers/ticket_purchases_controller.rb +++ b/app/controllers/ticket_purchases_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 85584955..955c76d1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/payments/_payment.html.haml b/app/views/payments/_payment.html.haml index 440f9985..bd70d8e8 100644 --- a/app/views/payments/_payment.html.haml +++ b/app/views/payments/_payment.html.haml @@ -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}",