Merge pull request #1108 from rishabhs95/stripe

Online payment feature - direct stripe integration
This commit is contained in:
Hernán Schmidt 2016-08-18 16:04:11 +02:00 committed by GitHub
commit 32019a11e1
35 changed files with 523 additions and 110 deletions

View file

@ -28,8 +28,10 @@ class ConferenceRegistrationsController < ApplicationController
end
def show
@total_price = Ticket.total_price(@conference, current_user)
@tickets = current_user.ticket_purchases.where(conference_id: @conference.id)
@total_price = Ticket.total_price(@conference, current_user, paid: true)
@tickets = current_user.ticket_purchases.by_conference(@conference).paid
@ticket_payments = @tickets.group_by(&:ticket_id)
@total_quantity = @tickets.group(:ticket_id).sum(:quantity)
end
def edit; end

View file

@ -0,0 +1,43 @@
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
@payment = Payment.new payment_params
if @payment.purchase && @payment.save
update_purchased_ticket_purchases
redirect_to conference_conference_registration_path(@conference.short_title),
notice: 'Thanks! Your ticket is booked successfully.'
else
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
@unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference)
flash[:error] = @payment.errors.full_messages.to_sentence + ' Please try again with correct credentials.'
render :new
end
end
private
def payment_params
params.permit(:stripe_customer_email, :stripe_customer_token)
.merge(stripe_customer_email: params[:stripeEmail],
stripe_customer_token: params[:stripeToken],
user: current_user, conference: @conference)
end
def update_purchased_ticket_purchases
current_user.ticket_purchases.by_conference(@conference).unpaid.update_all(paid: true, payment_id: @payment.id)
end
end

View file

@ -4,13 +4,15 @@ class TicketPurchasesController < ApplicationController
authorize_resource :conference_registrations, class: Registration
def create
current_user.ticket_purchases.by_conference(@conference).unpaid.destroy_all
message = TicketPurchase.purchase(@conference, current_user, params[:tickets][0])
if message.blank?
if current_user.ticket_purchases.any?
redirect_to conference_conference_registration_path(@conference.short_title),
notice: "Thank you for supporting #{@conference.title} by purchasing a ticket."
if current_user.ticket_purchases.by_conference(@conference).unpaid.any?
redirect_to new_conference_payment_path,
notice: 'Please pay here to get tickets.'
else
redirect_to conference_conference_registration_path(@conference.short_title)
redirect_to conference_tickets_path(@conference.short_title),
error: 'Please get at least one ticket to continue.'
end
else
redirect_to conference_conference_registration_path(@conference.short_title),
@ -18,18 +20,6 @@ class TicketPurchasesController < ApplicationController
end
end
def destroy
@ticket_purchases = current_user.ticket_purchases.find(params[:id])
if @ticket_purchases.destroy
redirect_to conference_conference_registration_path(@conference.short_title),
notice: 'Ticket successfully deleted.'
else
redirect_to conference_conference_registration_path(@conference.short_title),
error: 'An error prohibited deleting your purchase! '\
"#{@ticket_purchases.errors.full_messages.join('. ')}."
end
end
private
def ticket_purchase_params