Merge 4d4736f042 into 424b1302cb
This commit is contained in:
commit
211410b6f0
33 changed files with 621 additions and 91 deletions
BIN
app/assets/images/credit_card.png
Normal file
BIN
app/assets/images/credit_card.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4 KiB |
|
|
@ -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
|
||||
|
|
|
|||
38
app/controllers/payments_controller.rb
Normal file
38
app/controllers/payments_controller.rb
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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)
|
||||
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), flash: { success: 'Thanks! You have purchased your tickets successfully.' }
|
||||
else
|
||||
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
|
||||
render 'new'
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_purchased_ticket_purchases
|
||||
current_user.ticket_purchases.by_conference(@conference).unpaid.update_all(paid: true, payment_id: @payment.id)
|
||||
end
|
||||
|
||||
def payment_params
|
||||
params.require(:payment)
|
||||
.permit(:full_name, :credit_card_number, :expiration_month, :expiration_year, :card_verification_value, :amount)
|
||||
.merge(user: current_user, conference: @conference)
|
||||
end
|
||||
end
|
||||
|
|
@ -4,11 +4,11 @@ class TicketPurchasesController < ApplicationController
|
|||
authorize_resource :conference_registrations, class: Registration
|
||||
|
||||
def create
|
||||
TicketPurchase.by_conference(@conference).unpaid.by_user(current_user).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 purchase tickets.'
|
||||
else
|
||||
redirect_to conference_conference_registration_path(@conference.short_title)
|
||||
end
|
||||
|
|
|
|||
9
app/helpers/payments_helper.rb
Normal file
9
app/helpers/payments_helper.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
module PaymentsHelper
|
||||
def months
|
||||
(1..12).collect{|n| ["#{n} - #{Date::MONTHNAMES[n]}", n]}
|
||||
end
|
||||
|
||||
def years
|
||||
(Date.current.year..Date.current.year + 15)
|
||||
end
|
||||
end
|
||||
|
|
@ -81,6 +81,7 @@ class Ability
|
|||
|
||||
can :index, Ticket
|
||||
can :manage, TicketPurchase, user_id: user.id
|
||||
can [:new], 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
|
||||
|
||||
|
|
|
|||
59
app/models/payment.rb
Normal file
59
app/models/payment.rb
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
class Payment < ActiveRecord::Base
|
||||
has_many :ticket_purchases
|
||||
belongs_to :user
|
||||
belongs_to :conference
|
||||
|
||||
attr_accessor :credit_card_number
|
||||
attr_accessor :credit_card_type
|
||||
attr_accessor :card_verification_value
|
||||
attr_accessor :expiration_month
|
||||
attr_accessor :expiration_year
|
||||
|
||||
validates :full_name, presence: true
|
||||
validates :credit_card_number, presence: true
|
||||
validates :card_verification_value, presence: true, length: { minimum: 3, maximum: 4 }
|
||||
validates :expiration_month, presence: true, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 12 }
|
||||
validates :expiration_year, presence: true
|
||||
validates :amount, presence: true, numericality: { greater_than: 0 }
|
||||
validates :user_id, presence: true
|
||||
validates :conference_id, presence: true
|
||||
|
||||
enum status: {
|
||||
unpaid: 0,
|
||||
success: 1,
|
||||
failure: 2
|
||||
}
|
||||
|
||||
def credit_card
|
||||
@credit_card ||= ActiveMerchant::Billing::CreditCard.new(
|
||||
name: full_name,
|
||||
number: credit_card_number,
|
||||
month: expiration_month,
|
||||
year: expiration_year,
|
||||
verification_value: card_verification_value
|
||||
)
|
||||
end
|
||||
|
||||
def amount_to_pay
|
||||
Ticket.total_price(conference, user, paid: false).cents
|
||||
end
|
||||
|
||||
def purchase
|
||||
gateway_response = begin
|
||||
GATEWAY.purchase(amount_to_pay, credit_card, currency: conference.tickets.first.price_currency)
|
||||
rescue
|
||||
ActiveMerchant::Billing::Response.new(false, 'Unable to receive any response from the payment gateway.')
|
||||
end
|
||||
|
||||
if gateway_response.success?
|
||||
self.last4 = credit_card.display_number
|
||||
self.authorization_code = gateway_response.authorization
|
||||
self.status = 'success'
|
||||
else
|
||||
errors.add(:base, gateway_response.message)
|
||||
self.status = 'failure'
|
||||
end
|
||||
|
||||
success?
|
||||
end
|
||||
end
|
||||
|
|
@ -18,24 +18,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
|
||||
|
|
|
|||
|
|
@ -38,6 +38,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
|
||||
|
|
|
|||
|
|
@ -89,29 +89,27 @@
|
|||
%span.fa-stack
|
||||
%i.fa.fa-square-o.fa-stack-2x
|
||||
%i.fa.fa-ticket.fa-stack-1x
|
||||
Tickets
|
||||
Ticket Purchases
|
||||
-if @tickets.any?
|
||||
= "(#{@total_price} #{@tickets.first.price.symbol})"
|
||||
= "(#{@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 'Buy more tickets', conference_tickets_path(@conference.short_title), class: "btn btn-default"
|
||||
- else
|
||||
You haven't bought any tickets.
|
||||
= link_to 'Please buy some tickets to support us!', conference_tickets_path(@conference.short_title)
|
||||
%p
|
||||
(Your registration won't be complete without buying a ticket)
|
||||
|
||||
.row
|
||||
.col-md-12
|
||||
|
|
|
|||
25
app/views/payments/_payment.html.haml
Normal file
25
app/views/payments/_payment.html.haml
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
= semantic_form_for(@payment, url: conference_payments_path) do |f|
|
||||
.form-group
|
||||
= f.label :full_name, 'Full name (as on card)'
|
||||
= f.text_field :full_name, class: "form-control", placeholder: "John Doe", id: "full_name"
|
||||
%span.pull-right
|
||||
%img.img-responsive{:src => image_url('credit_card.png')}
|
||||
.form-group
|
||||
= f.label :credit_card_number, "Credit Card number (without spaces)"
|
||||
.input-group
|
||||
= f.text_field :credit_card_number, class: "form-control", placeholder: "XXXX XXXX XXXX XXXX", id: "credit_card_number"
|
||||
%span.input-group-addon
|
||||
%i.fa.fa-credit-card
|
||||
.form-group.col-md-6
|
||||
= f.label :expiration_month
|
||||
= f.select :expiration_month, months, {}, class: "form-control", id: "expiration_month"
|
||||
.form-group.col-md-6
|
||||
= f.label :expiration_year
|
||||
= f.select :expiration_year, years, {}, class: "form-control", id: "expiration_year"
|
||||
.form-group.col-md-10
|
||||
= f.label :card_verification_value, 'Security Code (3 on back, AmEx: 4 on front)'
|
||||
= f.text_field :card_verification_value, class: "form-control", placeholder: "XXX", id: "card_verification_value"
|
||||
= f.number_field :amount, value: @total_amount_to_pay, class: "form-control", type: 'hidden'
|
||||
.form-group.text-center
|
||||
= f.submit "Pay #{number_to_currency @total_amount_to_pay, unit: @total_amount_to_pay.symbol}", class: "btn btn-primary", id: "Charge Card"
|
||||
= link_to "Cancel", conference_conference_registration_path, class: "btn btn-danger"
|
||||
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
|
||||
Buy tickets for
|
||||
= @total_amount_to_pay.symbol
|
||||
= humanized_money @total_amount_to_pay
|
||||
- if @payment.errors.any?
|
||||
.alert.alert-danger
|
||||
%ul
|
||||
- @payment.errors.full_messages.each do |msg|
|
||||
%li= msg
|
||||
.col-xs-6.col-xs-offset-3.well
|
||||
= render partial: 'payment'
|
||||
|
|
@ -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.
|
||||
* Buying a ticket is mandatory. Your registration will not complete until you buy a ticket.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue