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 :index, Ticket
|
||||||
can :manage, TicketPurchase, user_id: user.id
|
can :manage, TicketPurchase, user_id: user.id
|
||||||
|
can [:index, :new, :create], Payment
|
||||||
|
|
||||||
can [:create, :destroy], Subscription, user_id: user.id
|
can [:create, :destroy], Subscription, user_id: user.id
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,4 +53,15 @@ class TicketPurchase < ActiveRecord::Base
|
||||||
purchase.quantity = quantity if quantity > 0
|
purchase.quantity = quantity if quantity > 0
|
||||||
purchase
|
purchase
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -111,6 +111,7 @@ Osem::Application.routes.draw do
|
||||||
resource :conference_registration, path: 'register'
|
resource :conference_registration, path: 'register'
|
||||||
resources :tickets, only: [:index]
|
resources :tickets, only: [:index]
|
||||||
resources :ticket_purchases, only: [:create, :destroy]
|
resources :ticket_purchases, only: [:create, :destroy]
|
||||||
|
resources :payments, only: [:index, :new, :create]
|
||||||
resource :subscriptions, only: [:create, :destroy]
|
resource :subscriptions, only: [:create, :destroy]
|
||||||
|
|
||||||
member do
|
member do
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue