diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index 5dae6d19..ffbfec15 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,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) diff --git a/app/controllers/ticket_purchases_controller.rb b/app/controllers/ticket_purchases_controller.rb index 1a1bc449..3034d742 100644 --- a/app/controllers/ticket_purchases_controller.rb +++ b/app/controllers/ticket_purchases_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 9bd1bf80..955c76d1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 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}", diff --git a/spec/features/ticket_purchases_spec.rb b/spec/features/ticket_purchases_spec.rb index 0a4e4ff8..cc6b66b5 100644 --- a/spec/features/ticket_purchases_spec.rb +++ b/spec/features/ticket_purchases_spec.rb @@ -7,16 +7,17 @@ feature Registration, feature: true, js: true do let!(:free_ticket) { create(:ticket, price_cents: 0) } let!(:first_registration_ticket) { create(:registration_ticket, price_cents: 0) } let!(:second_registration_ticket) { create(:registration_ticket, price_cents: 0) } - let!(:conference) { create(:conference, title: 'ExampleCon', tickets: [ticket, free_ticket, first_registration_ticket, second_registration_ticket], registration_period: create(:registration_period, start_date: 3.days.ago)) } + let!(:third_registration_ticket) { create(:registration_ticket, price_cents: 2000) } + let!(:conference) { create(:conference, title: 'ExampleCon', tickets: [ticket, free_ticket, first_registration_ticket, second_registration_ticket, third_registration_ticket], registration_period: create(:registration_period, start_date: 3.days.ago)) } let!(:participant) { create(:user) } - def make_stripe_purchase(card_number='4242424242424242') + def make_stripe_purchase(card_number = '4242424242424242') find('.stripe-button-el').click stripe_iframe = all('iframe[name=stripe_checkout_app]').last sleep(5) Capybara.within_frame stripe_iframe do - expect(page).to have_content("#{ENV['OSEM_NAME']} tickets") + expect(page).to have_content(:all, "#{ENV['OSEM_NAME']} tickets") fill_in 'Card number', with: card_number fill_in 'Expiry', with: '08/22' fill_in 'CVC', with: '123' @@ -108,6 +109,49 @@ feature Registration, feature: true, js: true do expect(purchase.paid).to be true end + scenario 'purchases a free registartion ticket' do + visit root_path + click_link 'Register' + + expect(current_path).to eq(new_conference_conference_registration_path(conference.short_title)) + click_button 'Register' + + fill_in "tickets__#{first_registration_ticket.id}", with: '1' + expect(current_path).to eq(conference_tickets_path(conference.short_title)) + + click_button 'Continue' + expect(current_path).to eq(new_conference_conference_registration_path(conference.short_title)) + expect(flash).to eq('Thanks! Your ticket is booked successfully. Please register for the conference.') + + purchase = TicketPurchase.where(user_id: participant.id, ticket_id: first_registration_ticket.id).first + expect(purchase.quantity).to eq(1) + expect(purchase.paid).to be true + end + + scenario 'purchases a non-free registartion ticket' do + visit root_path + click_link 'Register' + + expect(current_path).to eq(new_conference_conference_registration_path(conference.short_title)) + click_button 'Register' + + fill_in "tickets__#{third_registration_ticket.id}", with: '1' + expect(current_path).to eq(conference_tickets_path(conference.short_title)) + + click_button 'Continue' + page.find('#flash') + expect(current_path).to eq(new_conference_payment_path(conference.short_title)) + expect(flash).to eq('Please pay here to get tickets.') + purchase = TicketPurchase.where(user_id: participant.id, ticket_id: third_registration_ticket.id).first + expect(purchase.quantity).to eq(1) + + if ENV['STRIPE_PUBLISHABLE_KEY'] || Rails.application.secrets.stripe_publishable_key + make_stripe_purchase + expect(current_path).to eq(new_conference_conference_registration_path(conference.short_title)) + expect(page).to have_content 'Your ticket is booked successfully.' + end + end + scenario 'purchases more than one registration tickets of a single type' do visit root_path click_link 'Register' @@ -144,7 +188,7 @@ feature Registration, feature: true, js: true do context 'who is registered' do scenario 'unregisters from conference, but ticket purchases dont delete' do - pending('SNAPCON: Investigate failure on the unregister button') + skip('SNAPCON: Investigate failure on the unregister button') visit root_path click_link 'Register' diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index eac86bfe..2e890949 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -441,6 +441,19 @@ describe User do end end end + + describe '#count_registration_tickets' do + let(:registration_ticket) { create(:registration_ticket, price_cents: 0) } + let(:conference3) { create(:conference, short_title: 'oSC17', title: 'openSUSE Conference 2017', tickets: [registration_ticket]) } + let(:ticket_purchase) { create(:ticket_purchase, user: user, conference: conference3, ticket: registration_ticket, quantity: 1) } + + it 'counts the number of registration tickets of a conference held by user' do + user.ticket_purchases << ticket_purchase + + expect(user.count_registration_tickets(conference3)).to eq(1) + expect(user.count_registration_tickets(conference2)).to eq(0) + end + end end describe 'rolify' do