refactor payment#purchase method, improve environment variable names

This commit is contained in:
Rishabh Saxena 2016-07-21 10:15:17 +05:30
parent c4d934d711
commit f00e051eff
8 changed files with 64 additions and 39 deletions

View file

@ -16,7 +16,7 @@ class PaymentsController < ApplicationController
@payment = Payment.new(payment_params)
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
if @payment.purchase(current_user, @conference, price_in_cents) && @payment.save
if @payment.purchase && @payment.save
update_purchased_ticket_purchases
redirect_to conference_conference_registration_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
else
@ -26,10 +26,6 @@ class PaymentsController < ApplicationController
private
def price_in_cents
(@payment.amount * 100).round
end
def update_purchased_ticket_purchases
paid_ticket_purchases = current_user.ticket_purchases.by_conference(@conference).unpaid
paid_ticket_purchases.each do |ticket|
@ -40,6 +36,8 @@ class PaymentsController < ApplicationController
end
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)
.merge(user: current_user, conference: @conference)
end
end

View file

@ -4,7 +4,7 @@ class TicketPurchasesController < ApplicationController
authorize_resource :conference_registrations, class: Registration
def create
TicketPurchase.destroy_all(user_id: current_user.id, conference_id: @conference.id, paid: false)
TicketPurchase.by_conference(@conference).unpaid.by_user(current_user).destroy_all
message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0])
if message.blank?
if current_user.ticket_purchases.by_conference(@conference).unpaid.any?