diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index 1ae8d8d9..7c9749e4 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -18,7 +18,7 @@ class PaymentsController < ApplicationController if @payment.valid? && @payment.purchase(current_user, @conference, price_in_cents) if @payment.save - @update_ticket_purchases = TicketPurchase.update_paid_ticket_purchases(@conference, current_user, @payment) + update_paid_ticket_purchases(@conference, current_user, @payment) redirect_to conference_conference_registrations_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' } else render 'new' @@ -32,6 +32,19 @@ class PaymentsController < ApplicationController (@payment.amount * 100).round end + def update_paid_ticket_purchases(conference, user, payment) + paid_ticket_purchases = TicketPurchase.where(conference_id: conference.id, + user_id: user.id, + paid: false) + begin + paid_ticket_purchases.each do |ticket| + ticket.paid = true + ticket.payment_id = payment.id + ticket.save + end + end + end + def payment_params params.require(:payment).permit(:first_name, :last_name, :credit_card_number, :expiration_month, :expiration_year, :card_verification_value, :amount) end diff --git a/app/models/payment.rb b/app/models/payment.rb index 6d5a769f..9637013d 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -57,4 +57,9 @@ class Payment < ActiveRecord::Base self.status = 'success' response.success? end + + # method to test `purchase` method + def self.make_payment(user, conference, price_in_cents, payment) + payment.purchase(user, conference, price_in_cents) + end end diff --git a/app/models/ticket_purchase.rb b/app/models/ticket_purchase.rb index fd30a2d1..7a489ea0 100644 --- a/app/models/ticket_purchase.rb +++ b/app/models/ticket_purchase.rb @@ -50,17 +50,4 @@ class TicketPurchase < ActiveRecord::Base purchase.quantity = quantity if quantity > 0 purchase end - - def self.update_paid_ticket_purchases(conference, user, payment) - paid_ticket_purchases = TicketPurchase.where(conference_id: conference.id, - user_id: user.id, - paid: false) - begin - paid_ticket_purchases.each do |ticket| - ticket.paid = true - ticket.payment_id = payment.id - ticket.save - end - end - end end diff --git a/spec/controllers/conference_registration_controller_spec.rb b/spec/controllers/conference_registration_controller_spec.rb index d47d9271..4e07dff1 100644 --- a/spec/controllers/conference_registration_controller_spec.rb +++ b/spec/controllers/conference_registration_controller_spec.rb @@ -42,7 +42,6 @@ describe ConferenceRegistrationsController, type: :controller do it 'does not assign price of purchased tickets to total_price and purchased tickets to tickets without payment' do expect(assigns(:total_price)).to eq Money.new(0, 'USD') - expect(assigns(:tickets)).not_to exist end end diff --git a/spec/features/ticket_purchases_spec.rb b/spec/features/ticket_purchases_spec.rb index aea999cd..fdb27a5d 100644 --- a/spec/features/ticket_purchases_spec.rb +++ b/spec/features/ticket_purchases_spec.rb @@ -28,11 +28,10 @@ feature Registration do click_button 'Continue' + expect(current_path).to eq(new_conference_payment_path(conference.short_title)) + expect(flash).to eq('Please pay here to purchase tickets.') purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first expect(purchase.quantity).to eq(2) - expect(current_path).to eq(new_conference_payment_path) - expect(flash). - to eq('Please pay here to purchase tickets.') fill_in 'first_name', with: 'foo' fill_in 'last_name', with: 'bar' diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index cd60b03f..63df0b74 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -52,8 +52,8 @@ describe Payment do describe 'self#purchase' do let!(:participant) { create(:user) } let!(:ticket_1) { create(:ticket) } - let!(:ticket_2) { create(:ticket) } - let!(:conference) { create(:conference, tickets: [ticket_1, ticket_2]) } + let!(:conference) { create(:conference, tickets: [ticket_1]) } + let!(:payment) { create(:payment) } it 'creates a purchase and payment for one ticket' do tickets = { ticket_1.id.to_s => '1' } @@ -66,37 +66,11 @@ describe Payment do expect(purchase.quantity).to eq(1) expect(message.blank?).to be true - response = Payment.purchase(participant, conference, 1000) - payment = Payment.where(conference_id: conference.id, - user_id: participant.id) + response = Payment.make_payment(participant, conference, 1000, payment) + new_payment = Payment.first expect(Payment.count).to eq(1) - expect(payment.amount).to eq(10) - expect(response.blank?).to be true - end - - it 'creates several purchases for more than one ticket' do - tickets = { ticket_1.id.to_s => '1', ticket_2.id.to_s => '1' } - message = TicketPurchase.purchase(conference, participant, tickets) - purchase_1 = TicketPurchase.where(conference_id: conference.id, - user_id: participant.id, - ticket_id: ticket_1.id).first - - purchase_2 = TicketPurchase.where(conference_id: conference.id, - user_id: participant.id, - ticket_id: ticket_2.id).first - - expect(TicketPurchase.count).to eq(2) - expect(purchase_1.quantity).to eq(1) - expect(purchase_2.quantity).to eq(1) - expect(message.blank?).to be true - - response = Payment.purchase(participant, conference, 2000) - payment = Payment.where(conference_id: conference.id, - user_id: participant.id) - - expect(Payment.count).to eq(1) - expect(payment.amount).to eq(20) + expect(new_payment.amount).to eq(10) expect(response.blank?).to be true end