2016-07-27 19:37:02 +05:30
|
|
|
class PaymentsController < ApplicationController
|
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
load_and_authorize_resource
|
|
|
|
|
load_resource :conference, find_by: :short_title
|
|
|
|
|
authorize_resource :conference_registrations, class: Registration
|
|
|
|
|
|
|
|
|
|
def index
|
|
|
|
|
@payments = current_user.payments
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def new
|
|
|
|
|
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
|
|
|
|
|
@unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
@unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference)
|
|
|
|
|
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
|
|
|
|
|
|
2016-07-28 20:42:04 +05:30
|
|
|
@payment = Payment.new payment_params.merge(amount: @total_amount_to_pay.cents,
|
|
|
|
|
user: current_user,
|
|
|
|
|
conference: @conference)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
render :new
|
|
|
|
|
end
|
2016-07-27 19:37:02 +05:30
|
|
|
end
|
|
|
|
|
|
2016-07-28 15:27:30 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def payment_params
|
|
|
|
|
params.permit :stripeEmail, :stripeToken
|
|
|
|
|
end
|
2016-07-27 19:37:02 +05:30
|
|
|
|
2016-07-28 15:27:30 +05:30
|
|
|
def update_purchased_ticket_purchases
|
|
|
|
|
current_user.ticket_purchases.by_conference(@conference).unpaid.update_all(paid: true, payment_id: @payment.id)
|
2016-07-27 19:37:02 +05:30
|
|
|
end
|
|
|
|
|
end
|