repairing code reviews
This commit is contained in:
parent
ee71456060
commit
f202c6b93b
5 changed files with 21 additions and 27 deletions
|
|
@ -5,7 +5,7 @@ class PaymentsController < ApplicationController
|
||||||
authorize_resource :conference_registrations, class: Registration
|
authorize_resource :conference_registrations, class: Registration
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@payments = Payment.where(user_id: current_user.id)
|
@payments = current_user.payments
|
||||||
end
|
end
|
||||||
|
|
||||||
def new
|
def new
|
||||||
|
|
@ -16,18 +16,24 @@ class PaymentsController < ApplicationController
|
||||||
@payment = Payment.new(payment_params)
|
@payment = Payment.new(payment_params)
|
||||||
@total_amount_to_pay = Ticket.total_price(@conference, current_user, 'f')
|
@total_amount_to_pay = Ticket.total_price(@conference, current_user, 'f')
|
||||||
|
|
||||||
if @payment.valid?
|
if @payment.valid? && @payment.purchase(current_user, @conference, price_in_cents)
|
||||||
if @payment.purchase(current_user, @conference)
|
@payment.save
|
||||||
@payment.save
|
@update_ticket_purchases = TicketPurchase.update_paid_ticket_purchases(@conference, current_user, @payment)
|
||||||
@update_ticket_purchases = TicketPurchase.update_paid_ticket_purchases(@conference, current_user, @payment)
|
end
|
||||||
return redirect_to conference_conference_registrations_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
|
|
||||||
end
|
if @payment.save
|
||||||
|
redirect_to conference_conference_registrations_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
|
||||||
|
else
|
||||||
|
render 'new'
|
||||||
end
|
end
|
||||||
render 'new'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def price_in_cents
|
||||||
|
(@payment.amount * 100).round
|
||||||
|
end
|
||||||
|
|
||||||
def payment_params
|
def payment_params
|
||||||
params.require(:payment).permit(:first_name, :last_name, :credit_card_number, :expiration_month, :expiration_year, :card_verification_value, :amount)
|
params.require(:payment).permit(:first_name, :last_name, :credit_card_number, :expiration_month, :expiration_year, :card_verification_value, :amount)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ class TicketPurchasesController < ApplicationController
|
||||||
authorize_resource :conference_registrations, class: Registration
|
authorize_resource :conference_registrations, class: Registration
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
TicketPurchase.destroy_all(user_id: current_user.id, conference_id: @conference.id, paid: 'f')
|
||||||
message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0])
|
message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0])
|
||||||
if message.blank?
|
if message.blank?
|
||||||
if current_user.ticket_purchases.where(paid: 'f').any?
|
if current_user.ticket_purchases.where(paid: 'f').any?
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ class Ability
|
||||||
|
|
||||||
can :index, Ticket
|
can :index, Ticket
|
||||||
can :manage, TicketPurchase, user_id: user.id
|
can :manage, TicketPurchase, user_id: user.id
|
||||||
can [:index, :new, :create], Payment
|
can :manage, Payment, user_id: user.id
|
||||||
|
|
||||||
can [:create, :destroy], Subscription, user_id: user.id
|
can [:create, :destroy], Subscription, user_id: user.id
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,8 +17,6 @@ class Payment < ActiveRecord::Base
|
||||||
validates :expiration_year, presence: true
|
validates :expiration_year, presence: true
|
||||||
validates :amount, presence: true, numericality: { greater_than: 0 }
|
validates :amount, presence: true, numericality: { greater_than: 0 }
|
||||||
|
|
||||||
validate :validate_card, on: create
|
|
||||||
|
|
||||||
enum status: {
|
enum status: {
|
||||||
unpaid: 0,
|
unpaid: 0,
|
||||||
success: 1,
|
success: 1,
|
||||||
|
|
@ -36,20 +34,7 @@ class Payment < ActiveRecord::Base
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_card
|
def purchase(user, conference, price_in_cents)
|
||||||
unless credit_card.validate.empty?
|
|
||||||
credit_card.errors.full_messages.each do |message|
|
|
||||||
errors.add(:base, message)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
credit_card.validate.empty?
|
|
||||||
end
|
|
||||||
|
|
||||||
def price_in_cents
|
|
||||||
(amount * 100).round
|
|
||||||
end
|
|
||||||
|
|
||||||
def purchase(user, conference)
|
|
||||||
begin
|
begin
|
||||||
response = GATEWAY.purchase(price_in_cents, credit_card, currency: conference.tickets.first.price_currency)
|
response = GATEWAY.purchase(price_in_cents, credit_card, currency: conference.tickets.first.price_currency)
|
||||||
rescue
|
rescue
|
||||||
|
|
@ -69,7 +54,7 @@ class Payment < ActiveRecord::Base
|
||||||
self.conference_id = conference.id
|
self.conference_id = conference.id
|
||||||
self.last4 = credit_card.display_number
|
self.last4 = credit_card.display_number
|
||||||
self.authorization_code = response.authorization
|
self.authorization_code = response.authorization
|
||||||
self.status = 1
|
self.status = 'success'
|
||||||
response.success?
|
response.success?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,9 @@ class TicketPurchase < ActiveRecord::Base
|
||||||
paid: 'f')
|
paid: 'f')
|
||||||
begin
|
begin
|
||||||
paid_ticket_purchases.each do |ticket|
|
paid_ticket_purchases.each do |ticket|
|
||||||
ticket.update_columns(paid: 't', payment_id: payment.id)
|
ticket.paid = 't'
|
||||||
|
ticket.payment_id = payment.id
|
||||||
|
ticket.save
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue