diff --git a/app/controllers/conference_registrations_controller.rb b/app/controllers/conference_registrations_controller.rb index 9d549878..5d9429e7 100644 --- a/app/controllers/conference_registrations_controller.rb +++ b/app/controllers/conference_registrations_controller.rb @@ -29,8 +29,9 @@ class ConferenceRegistrationsController < ApplicationController def show @total_price = Ticket.total_price(@conference, current_user, paid: true) - @tickets = current_user.ticket_purchases.where(conference_id: @conference.id, paid: true) - @ticket_payments = @tickets.group_by(&:payment_id) + @tickets = current_user.ticket_purchases.by_conference(@conference).paid + @ticket_payments = @tickets.group_by(&:ticket_id) + @total_quantity = @tickets.group(:ticket_id).sum(:quantity) end def edit; end diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index e56876f2..aeb2ca49 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -16,9 +16,9 @@ class PaymentsController < ApplicationController @payment = Payment.new(payment_params) @total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false) - if @payment.valid? && @payment.purchase(current_user, @conference, price_in_cents) && @payment.save - 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.' } + if @payment.purchase(current_user, @conference, price_in_cents) && @payment.save + update_purchased_ticket_purchases(@conference, current_user, @payment) + redirect_to conference_conference_registration_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' } else render 'new' end @@ -30,10 +30,8 @@ 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) + def update_purchased_ticket_purchases(conference, user, payment) + paid_ticket_purchases = current_user.ticket_purchases.by_conference(conference).unpaid paid_ticket_purchases.each do |ticket| ticket.paid = true ticket.payment_id = payment.id diff --git a/app/controllers/ticket_purchases_controller.rb b/app/controllers/ticket_purchases_controller.rb index 3c39e76c..7b07f33d 100644 --- a/app/controllers/ticket_purchases_controller.rb +++ b/app/controllers/ticket_purchases_controller.rb @@ -7,7 +7,7 @@ class TicketPurchasesController < ApplicationController TicketPurchase.destroy_all(user_id: current_user.id, conference_id: @conference.id, paid: false) message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0]) if message.blank? - if current_user.ticket_purchases.unpaid.any? + if current_user.ticket_purchases.by_conference(@conference).unpaid.any? redirect_to new_conference_payment_path, notice: 'Please pay here to purchase tickets.' else redirect_to conference_conference_registration_path(@conference.short_title) diff --git a/app/helpers/payments_helper.rb b/app/helpers/payments_helper.rb index 50b53262..bfb242aa 100644 --- a/app/helpers/payments_helper.rb +++ b/app/helpers/payments_helper.rb @@ -8,6 +8,6 @@ module PaymentsHelper end def card_types - [[['Visa', 'visa'], ['MasterCard', 'master'], ['Discover', 'discover'], ['American Express', 'american_express']]] + [['Visa', 'visa'], ['MasterCard', 'master'], ['Discover', 'discover'], ['American Express', 'american_express']] end end diff --git a/app/models/payment.rb b/app/models/payment.rb index 9637013d..b89b8377 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -36,30 +36,25 @@ class Payment < ActiveRecord::Base def purchase(user, conference, price_in_cents) begin - response = GATEWAY.purchase(price_in_cents, credit_card, currency: conference.tickets.first.price_currency) + recieve = GATEWAY.purchase(price_in_cents, credit_card, currency: conference.tickets.first.price_currency) rescue false end - unless response + unless recieve errors.add(:base, 'Unable to recieve any response') return false end - unless response.success? - errors.add(:base, response.message) + unless recieve.success? + errors.add(:base, recieve.message) self.status = 'failure' return false end self.user_id = user.id self.conference_id = conference.id self.last4 = credit_card.display_number - self.authorization_code = response.authorization + self.authorization_code = recieve.authorization 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) + recieve.success? end end diff --git a/app/models/ticket.rb b/app/models/ticket.rb index e2188baa..198c836d 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -18,18 +18,12 @@ class Ticket < ActiveRecord::Base end def paid?(user) - ticket_purchases.find_by(user: user, paid: true).present? + ticket_purchases.paid.by_user(user).present? end def quantity_bought_by(user, paid: false) - purchased_tickets = ticket_purchases.where(user_id: user.id, paid: paid) - quantity = 0 - if purchased_tickets - purchased_tickets.each do |ticket| - quantity += ticket.quantity - end - end - quantity + purchased_tickets = ticket_purchases.paid.by_user(user) + quantity = purchased_tickets.sum(:quantity) end def unpaid?(user) diff --git a/app/models/ticket_purchase.rb b/app/models/ticket_purchase.rb index 032f48c5..b7a8951f 100644 --- a/app/models/ticket_purchase.rb +++ b/app/models/ticket_purchase.rb @@ -13,7 +13,10 @@ class TicketPurchase < ActiveRecord::Base delegate :price_cents, to: :ticket delegate :price_currency, to: :ticket + scope :paid, -> { where(paid: true) } scope :unpaid, -> { where(paid: false) } + scope :by_conference, -> (conference) { where(conference_id: conference.id) } + scope :by_user, -> (user) { where(user_id: user.id) } def self.purchase(conference, user, purchases) errors = [] diff --git a/app/views/conference_registrations/show.html.haml b/app/views/conference_registrations/show.html.haml index 3998d851..0f08b6c9 100644 --- a/app/views/conference_registrations/show.html.haml +++ b/app/views/conference_registrations/show.html.haml @@ -94,18 +94,14 @@ = "(#{@tickets.first.price.symbol}#{humanized_money @total_price})" %ul .col.md-4 - - @ticket_payments.each_pair do |payment, tickets| - %strong - Purchased - - tickets.each do |ticket| - %li - = ticket.quantity - = ticket.title - = word_pluralize(ticket.quantity, 'Ticket') - for - = ticket.price.symbol - = humanized_money ticket.price - %hr + - @ticket_payments.each_pair do |ticket_id, tickets| + %li + = @total_quantity[ticket_id] + = tickets.first.title + = word_pluralize(@total_quantity[ticket_id], 'Ticket') + for + = tickets.first.price.symbol + = humanized_money tickets.first.price - if @tickets.any? = link_to 'Buy more tickets', conference_tickets_path(@conference.short_title), class: "btn btn-default" - else diff --git a/app/views/payments/_payment.html.haml b/app/views/payments/_payment.html.haml index 33e88886..6552622e 100644 --- a/app/views/payments/_payment.html.haml +++ b/app/views/payments/_payment.html.haml @@ -31,4 +31,4 @@ = number_to_currency @total_amount_to_pay .form-group = f.submit "Charge Card", class: "btn btn-primary" - = link_to "Cancel", conference_conference_registrations_path, class: "btn btn-danger" + = link_to "Cancel", conference_conference_registration_path, class: "btn btn-danger" diff --git a/app/views/tickets/index.html.haml b/app/views/tickets/index.html.haml index 1852d60d..8f80e1ac 100644 --- a/app/views/tickets/index.html.haml +++ b/app/views/tickets/index.html.haml @@ -37,7 +37,7 @@ = button_tag(type: 'submit', class: 'btn btn-success btn-lg') do Continue %i.fa.fa-shopping-cart - = link_to 'Cancel registration', conference_conference_registrations_path(@conference.short_title), method: :delete, class: 'btn btn-danger btn-sm' + = link_to 'Cancel registration', conference_conference_registration_path(@conference.short_title), method: :delete, class: 'btn btn-danger btn-sm' .row .col-md-13 %p.text-muted.text-center