refactor payment#purchase method, improve environment variable names
This commit is contained in:
parent
c4d934d711
commit
f00e051eff
8 changed files with 64 additions and 39 deletions
|
|
@ -16,7 +16,7 @@ class PaymentsController < ApplicationController
|
|||
@payment = Payment.new(payment_params)
|
||||
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
|
||||
|
||||
if @payment.purchase(current_user, @conference, price_in_cents) && @payment.save
|
||||
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
|
||||
|
|
@ -26,10 +26,6 @@ class PaymentsController < ApplicationController
|
|||
|
||||
private
|
||||
|
||||
def price_in_cents
|
||||
(@payment.amount * 100).round
|
||||
end
|
||||
|
||||
def update_purchased_ticket_purchases
|
||||
paid_ticket_purchases = current_user.ticket_purchases.by_conference(@conference).unpaid
|
||||
paid_ticket_purchases.each do |ticket|
|
||||
|
|
@ -40,6 +36,8 @@ class PaymentsController < ApplicationController
|
|||
end
|
||||
|
||||
def payment_params
|
||||
params.require(:payment).permit(:first_name, :last_name, :credit_card_number, :expiration_month, :expiration_year, :card_verification_value, :amount)
|
||||
params.require(:payment)
|
||||
.permit(:first_name, :last_name, :credit_card_number, :expiration_month, :expiration_year, :card_verification_value, :amount)
|
||||
.merge(user: current_user, conference: @conference)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ class TicketPurchasesController < ApplicationController
|
|||
authorize_resource :conference_registrations, class: Registration
|
||||
|
||||
def create
|
||||
TicketPurchase.destroy_all(user_id: current_user.id, conference_id: @conference.id, paid: false)
|
||||
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.by_conference(@conference).unpaid.any?
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ class Payment < ActiveRecord::Base
|
|||
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,
|
||||
|
|
@ -34,17 +36,18 @@ class Payment < ActiveRecord::Base
|
|||
)
|
||||
end
|
||||
|
||||
def purchase(user, conference, price_in_cents)
|
||||
def amount_to_pay
|
||||
Ticket.total_price(conference, user, paid: false).cents
|
||||
end
|
||||
|
||||
def purchase
|
||||
gateway_response = begin
|
||||
GATEWAY.purchase(price_in_cents, credit_card, currency: conference.tickets.first.price_currency)
|
||||
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.user_id = user.id
|
||||
self.conference_id = conference.id
|
||||
self.last4 = credit_card.display_number
|
||||
self.authorization_code = gateway_response.authorization
|
||||
self.status = 'success'
|
||||
|
|
@ -56,4 +59,3 @@ class Payment < ActiveRecord::Base
|
|||
success?
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue