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
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue