solve code reviews, resolve rebase conflicts
This commit is contained in:
parent
9b704e483e
commit
ae44abc7e6
10 changed files with 32 additions and 45 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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 = []
|
||||
|
|
|
|||
|
|
@ -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|
|
||||
- @ticket_payments.each_pair do |ticket_id, tickets|
|
||||
%li
|
||||
= ticket.quantity
|
||||
= ticket.title
|
||||
= word_pluralize(ticket.quantity, 'Ticket')
|
||||
= @total_quantity[ticket_id]
|
||||
= tickets.first.title
|
||||
= word_pluralize(@total_quantity[ticket_id], 'Ticket')
|
||||
for
|
||||
= ticket.price.symbol
|
||||
= humanized_money ticket.price
|
||||
%hr
|
||||
= 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
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue