add payment resources, controllers and method for updating purchased ticket's parameters
This commit is contained in:
parent
36f2cf4373
commit
900a1b32a8
4 changed files with 47 additions and 0 deletions
34
app/controllers/payments_controller.rb
Normal file
34
app/controllers/payments_controller.rb
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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 = Payment.where(user_id: current_user.id)
|
||||
end
|
||||
|
||||
def new
|
||||
@total_amount_to_pay = Ticket.total_price(@conference, current_user, 'f')
|
||||
end
|
||||
|
||||
def create
|
||||
@payment = Payment.new(payment_params)
|
||||
@total_amount_to_pay = Ticket.total_price(@conference, current_user, 'f')
|
||||
|
||||
if @payment.valid?
|
||||
if @payment.purchase(current_user, @conference)
|
||||
@payment.save
|
||||
@update_ticket_purchases = TicketPurchase.update_paid_ticket_purchases(@conference, current_user, @payment)
|
||||
return redirect_to conference_conference_registrations_path(@conference.short_title), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
|
||||
end
|
||||
end
|
||||
render 'new'
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def payment_params
|
||||
params.require(:payment).permit(:first_name, :last_name, :credit_card_number, :expiration_month, :expiration_year, :card_verification_value, :amount)
|
||||
end
|
||||
end
|
||||
|
|
@ -81,6 +81,7 @@ class Ability
|
|||
|
||||
can :index, Ticket
|
||||
can :manage, TicketPurchase, user_id: user.id
|
||||
can [:index, :new, :create], Payment
|
||||
|
||||
can [:create, :destroy], Subscription, user_id: user.id
|
||||
|
||||
|
|
|
|||
|
|
@ -53,4 +53,15 @@ class TicketPurchase < ActiveRecord::Base
|
|||
purchase.quantity = quantity if quantity > 0
|
||||
purchase
|
||||
end
|
||||
|
||||
def self.update_paid_ticket_purchases(conference, user, payment)
|
||||
paid_ticket_purchases = TicketPurchase.where(conference_id: conference.id,
|
||||
user_id: user.id,
|
||||
paid: 'f')
|
||||
begin
|
||||
paid_ticket_purchases.each do |ticket|
|
||||
ticket.update_columns(paid: 't', payment_id: payment.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue