diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb new file mode 100644 index 00000000..bd633ed2 --- /dev/null +++ b/app/controllers/payments_controller.rb @@ -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 diff --git a/app/models/ability.rb b/app/models/ability.rb index fb48808a..047812a6 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -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 diff --git a/app/models/ticket_purchase.rb b/app/models/ticket_purchase.rb index 8b0cd555..af5d7da6 100644 --- a/app/models/ticket_purchase.rb +++ b/app/models/ticket_purchase.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 3e83fd1b..73943cb7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -111,6 +111,7 @@ Osem::Application.routes.draw do resource :conference_registration, path: 'register' resources :tickets, only: [:index] resources :ticket_purchases, only: [:create, :destroy] + resources :payments, only: [:index, :new, :create] resource :subscriptions, only: [:create, :destroy] member do