Merge pull request #1108 from rishabhs95/stripe
Online payment feature - direct stripe integration
This commit is contained in:
commit
32019a11e1
35 changed files with 523 additions and 110 deletions
|
|
@ -14,4 +14,5 @@
|
|||
*= require bootstrap-datetimepicker
|
||||
*= require leaflet
|
||||
*= require bootstrap3-switch
|
||||
*= require osem-payments
|
||||
*/
|
||||
|
|
|
|||
3
app/assets/stylesheets/osem-payments.css.scss
Normal file
3
app/assets/stylesheets/osem-payments.css.scss
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
.stripe-button-el {
|
||||
float: right;
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
43
app/controllers/payments_controller.rb
Normal file
43
app/controllers/payments_controller.rb
Normal 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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -81,6 +81,7 @@ class Ability
|
|||
|
||||
can :index, Ticket
|
||||
can :manage, TicketPurchase, user_id: user.id
|
||||
can [:new, :create], Payment, user_id: user.id
|
||||
|
||||
can [:create, :destroy], Subscription, user_id: user.id
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ class Conference < ActiveRecord::Base
|
|||
has_one :program, dependent: :destroy
|
||||
has_one :venue, dependent: :destroy
|
||||
has_many :ticket_purchases, dependent: :destroy
|
||||
has_many :payments, dependent: :destroy
|
||||
has_many :supporters, through: :ticket_purchases, source: :user
|
||||
has_many :tickets, dependent: :destroy
|
||||
|
||||
|
|
|
|||
41
app/models/payment.rb
Normal file
41
app/models/payment.rb
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
class Payment < ActiveRecord::Base
|
||||
has_many :ticket_purchases
|
||||
belongs_to :user
|
||||
belongs_to :conference
|
||||
|
||||
attr_accessor :stripe_customer_email
|
||||
attr_accessor :stripe_customer_token
|
||||
|
||||
validates :status, presence: true
|
||||
validates :user_id, presence: true
|
||||
validates :conference_id, presence: true
|
||||
|
||||
enum status: {
|
||||
unpaid: 0,
|
||||
success: 1,
|
||||
failure: 2
|
||||
}
|
||||
|
||||
def amount_to_pay
|
||||
Ticket.total_price(conference, user, paid: false).cents
|
||||
end
|
||||
|
||||
def purchase
|
||||
gateway_response = Stripe::Charge.create source: stripe_customer_token,
|
||||
receipt_email: stripe_customer_email,
|
||||
description: "ticket purchases(#{user.username})",
|
||||
amount: amount_to_pay,
|
||||
currency: conference.tickets.first.price_currency
|
||||
|
||||
self.amount = gateway_response[:amount]
|
||||
self.last4 = gateway_response[:source][:last4]
|
||||
self.authorization_code = gateway_response[:id]
|
||||
self.status = 'success'
|
||||
true
|
||||
|
||||
rescue Stripe::StripeError => error
|
||||
errors.add(:base, error.message)
|
||||
self.status = 'failure'
|
||||
false
|
||||
end
|
||||
end
|
||||
|
|
@ -20,24 +20,27 @@ class Ticket < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def paid?(user)
|
||||
ticket_purchases.find_by(user: user, paid: true).present?
|
||||
ticket_purchases.paid.by_user(user).present?
|
||||
end
|
||||
|
||||
def quantity_bought_by(user)
|
||||
result = ticket_purchases.where(user_id: user.id).first
|
||||
result ? result.quantity : 0
|
||||
def quantity_bought_by(user, paid: false)
|
||||
ticket_purchases.by_user(user).where(paid: paid).sum(:quantity)
|
||||
end
|
||||
|
||||
def total_price(user)
|
||||
quantity_bought_by(user) * price
|
||||
def unpaid?(user)
|
||||
ticket_purchases.unpaid.by_user(user).present?
|
||||
end
|
||||
|
||||
def self.total_price(conference, user)
|
||||
def total_price(user, paid: false)
|
||||
quantity_bought_by(user, paid: paid) * price
|
||||
end
|
||||
|
||||
def self.total_price(conference, user, paid: false)
|
||||
tickets = Ticket.where(conference_id: conference.id)
|
||||
result = nil
|
||||
begin
|
||||
tickets.each do |ticket|
|
||||
price = ticket.total_price(user)
|
||||
price = ticket.total_price(user, paid: paid)
|
||||
if result
|
||||
result += price unless price.zero?
|
||||
else
|
||||
|
|
|
|||
|
|
@ -7,23 +7,24 @@ class TicketPurchase < ActiveRecord::Base
|
|||
|
||||
validates_numericality_of :quantity, greater_than: 0
|
||||
|
||||
validates_uniqueness_of :user_id,
|
||||
scope: :ticket_id,
|
||||
message: 'already bought this ticket!'
|
||||
|
||||
delegate :title, to: :ticket
|
||||
delegate :description, to: :ticket
|
||||
delegate :price, to: :ticket
|
||||
delegate :price_cents, to: :ticket
|
||||
delegate :price_currency, to: :ticket
|
||||
|
||||
scope :paid, -> { where(paid: true) }
|
||||
scope :unpaid, -> { where(paid: false) }
|
||||
scope :by_conference, -> (conference) { where(conference_id: conference.id) }
|
||||
scope :by_user, -> (user) { where(user_id: user.id) }
|
||||
|
||||
def self.purchase(conference, user, purchases)
|
||||
errors = []
|
||||
ActiveRecord::Base.transaction do
|
||||
conference.tickets.each do |ticket|
|
||||
quantity = purchases[ticket.id.to_s].to_i
|
||||
# if the user bought the ticket, just update the quantity
|
||||
if ticket.bought?(user)
|
||||
if ticket.bought?(user) && ticket.unpaid?(user)
|
||||
purchase = update_quantity(conference, quantity, ticket, user)
|
||||
else
|
||||
purchase = purchase_ticket(conference, quantity, ticket, user)
|
||||
|
|
@ -48,7 +49,8 @@ class TicketPurchase < ActiveRecord::Base
|
|||
def self.update_quantity(conference, quantity, ticket, user)
|
||||
purchase = TicketPurchase.where(ticket_id: ticket.id,
|
||||
conference_id: conference.id,
|
||||
user_id: user.id).first
|
||||
user_id: user.id,
|
||||
paid: false).first
|
||||
|
||||
purchase.quantity = quantity if quantity > 0
|
||||
purchase
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ class User < ActiveRecord::Base
|
|||
has_many :registrations, dependent: :destroy
|
||||
has_many :events_registrations, through: :registrations
|
||||
has_many :ticket_purchases, dependent: :destroy
|
||||
has_many :payments, dependent: :destroy
|
||||
has_many :tickets, through: :ticket_purchases, source: :ticket
|
||||
has_many :votes, dependent: :destroy
|
||||
has_many :voted_events, through: :votes, source: :events
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
.page-header
|
||||
%h1 Tickets
|
||||
%p.text-muted
|
||||
Tickets to purchase during registration
|
||||
Tickets to get during registration
|
||||
- if @conference.tickets.any?
|
||||
.row
|
||||
.col-md-12
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
Support
|
||||
=@conference.short_title
|
||||
%p.lead
|
||||
To support our event you can purchase these tickets
|
||||
To support our event you can get these tickets
|
||||
- @conference.tickets.each_slice(4) do |slice|
|
||||
.row.row-centered
|
||||
- slice.each do |ticket|
|
||||
|
|
@ -20,6 +20,6 @@
|
|||
= markdown(ticket.description)
|
||||
%p.text-center
|
||||
= link_to(conference_tickets_path(@conference.short_title), class: 'btn btn-success') do
|
||||
Buy Ticket
|
||||
Get Ticket
|
||||
= humanized_money_with_symbol ticket.price
|
||||
|
||||
|
|
|
|||
|
|
@ -85,33 +85,31 @@
|
|||
- if @conference.tickets.any?
|
||||
.row
|
||||
.col-md-12
|
||||
%h4
|
||||
%span.fa-stack
|
||||
%i.fa.fa-square-o.fa-stack-2x
|
||||
%i.fa.fa-ticket.fa-stack-1x
|
||||
Tickets
|
||||
-if @tickets.any?
|
||||
= "(#{@total_price} #{@tickets.first.price.symbol})"
|
||||
-if @tickets.any?
|
||||
%h4
|
||||
%span.fa-stack
|
||||
%i.fa.fa-square-o.fa-stack-2x
|
||||
%i.fa.fa-ticket.fa-stack-1x
|
||||
Ticket Purchases
|
||||
= "(#{@tickets.first.price.symbol}#{humanized_money @total_price})"
|
||||
%ul
|
||||
- @tickets.each do |ticket|
|
||||
%li
|
||||
= ticket.quantity
|
||||
= ticket.title
|
||||
= word_pluralize(ticket.quantity, 'Ticket')
|
||||
for
|
||||
= humanized_money ticket.price
|
||||
= ticket.price.symbol
|
||||
= link_to conference_ticket_purchase_path(@conference.short_title, ticket.id), method: :delete,
|
||||
id: "ticket-#{ticket.id}-delete",
|
||||
class: 'btn btn-danger btn-xs',
|
||||
data: { confirm: "Do you really want to delete the #{ticket.title} ticket for #{@conference.title}?" } do
|
||||
%i.fa.fa-trash-o
|
||||
%li
|
||||
- if @tickets.any?
|
||||
= link_to 'Buy more tickets', conference_tickets_path(@conference.short_title)
|
||||
- else
|
||||
You haven't bought any tickets.
|
||||
= link_to 'Please buy some tickets to support us!', conference_tickets_path(@conference.short_title)
|
||||
.col-md-12
|
||||
- @ticket_payments.each_pair do |ticket_id, tickets|
|
||||
%li
|
||||
= @total_quantity[ticket_id]
|
||||
= tickets.first.title
|
||||
= word_pluralize(@total_quantity[ticket_id], 'Ticket')
|
||||
for
|
||||
= tickets.first.price.symbol
|
||||
= humanized_money tickets.first.price
|
||||
%br
|
||||
- if @tickets.any?
|
||||
= link_to 'Get more tickets', conference_tickets_path(@conference.short_title), class: "btn btn-default"
|
||||
- else
|
||||
You haven't bought any tickets.
|
||||
= link_to 'Please get some tickets to support us!', conference_tickets_path(@conference.short_title)
|
||||
%p
|
||||
(Your participation won't be valid without getting a ticket)
|
||||
|
||||
.row
|
||||
.col-md-12
|
||||
|
|
|
|||
27
app/views/payments/_payment.html.haml
Normal file
27
app/views/payments/_payment.html.haml
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
.div
|
||||
.col-md-12.table-responsive
|
||||
%table.table.table-hover
|
||||
%thead
|
||||
%tr
|
||||
%th Ticket
|
||||
%th Quantity
|
||||
%th Price
|
||||
%th Total
|
||||
%tbody
|
||||
- @unpaid_ticket_purchases.each do |ticket|
|
||||
%tr
|
||||
%td
|
||||
= ticket.title
|
||||
%td
|
||||
= ticket.quantity
|
||||
%td
|
||||
= humanized_money_with_symbol ticket.price
|
||||
%td
|
||||
= humanized_money_with_symbol ticket.quantity * ticket.price
|
||||
|
||||
= form_tag conference_payments_path do
|
||||
%script.stripe-button{ src: "https://checkout.stripe.com/checkout.js",
|
||||
data: { amount: @total_amount_to_pay.cents, label: "Pay #{humanized_money_with_symbol @total_amount_to_pay}",
|
||||
email: current_user.email, currency: @total_amount_to_pay.currency, name: ENV['OSEM_NAME'] || 'OSEM',
|
||||
description: "book your tickets", key: Rails.application.secrets.stripe_publishable_key, locale: "auto"}}
|
||||
= link_to 'Edit Purchase', conference_tickets_path(@conference.short_title), class: 'btn btn-default'
|
||||
14
app/views/payments/new.html.haml
Normal file
14
app/views/payments/new.html.haml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
.container
|
||||
.row
|
||||
.col-xs-6.col-xs-offset-3
|
||||
%h1
|
||||
Payment Summary :
|
||||
= humanized_money_with_symbol @total_amount_to_pay
|
||||
.col-xs-8.col-xs-offset-2.well
|
||||
= render partial: 'payment'
|
||||
.row
|
||||
.col-md-13
|
||||
%p.text-muted.text-center
|
||||
%small
|
||||
All payments are handled securely by our payment processor,
|
||||
= link_to 'Stripe', 'https://stripe.com', target: '_blank'
|
||||
|
|
@ -8,12 +8,8 @@
|
|||
- unless ticket.description.blank?
|
||||
= markdown(ticket.description)
|
||||
%td.col-sm-1.col-md-1
|
||||
- if ticket.bought?(current_user)
|
||||
= text_field_tag("tickets[][#{ticket.id}]", ticket.quantity_bought_by(current_user),
|
||||
= text_field_tag("tickets[][#{ticket.id}]", 0,
|
||||
type: 'number', min: 0, class: "form-control quantity", 'data-id' => ticket.id)
|
||||
- else
|
||||
= text_field_tag("tickets[][#{ticket.id}]", 0, type: 'number', min: 0,
|
||||
class: "form-control quantity", 'data-id' => ticket.id)
|
||||
%td.col-sm-1.col-md-1.text-center
|
||||
= ticket.price.symbol
|
||||
%span{id: "price_#{ticket.id}"}
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@
|
|||
%h1
|
||||
Tickets
|
||||
%p.lead
|
||||
If you like, support
|
||||
Please choose your tickets for
|
||||
%strong
|
||||
= @conference.title
|
||||
by buying a ticket*
|
||||
here*
|
||||
=form_tag(conference_ticket_purchases_path, method: :post) do |f|
|
||||
%table.table.table-hover
|
||||
%thead
|
||||
|
|
@ -35,12 +35,11 @@
|
|||
.pull-right
|
||||
.btn-group-vertical
|
||||
= button_tag(type: 'submit', class: 'btn btn-success btn-lg') do
|
||||
Support
|
||||
Continue
|
||||
%i.fa.fa-shopping-cart
|
||||
= link_to 'Continue without a Ticket!', conference_conference_registration_path(@conference.short_title),
|
||||
class: 'btn btn-danger btn-sm'
|
||||
= link_to 'Cancel registration', conference_conference_registration_path(@conference.short_title), method: :delete, class: 'btn btn-danger btn-sm'
|
||||
.row
|
||||
.col-md-13
|
||||
%p.text-muted.text-center
|
||||
%small
|
||||
* Buying a ticket is not mandatory. Checkout will be at the conference registration.
|
||||
* Getting a ticket is mandatory. Your participation will not be valid until you get a ticket.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue