improve Stripe error rescue. modify tests.
This commit is contained in:
parent
52c30af642
commit
1f9338735c
6 changed files with 43 additions and 36 deletions
|
|
@ -17,14 +17,17 @@ class PaymentsController < ApplicationController
|
|||
@unpaid_ticket_purchases = current_user.ticket_purchases.unpaid.by_conference(@conference)
|
||||
@total_amount_to_pay = Ticket.total_price(@conference, current_user, paid: false)
|
||||
|
||||
@payment = Payment.new payment_params.merge(user: current_user, conference: @conference)
|
||||
@payment.purchase
|
||||
@payment.save
|
||||
@payment = Payment.new payment_params.merge(amount: @total_amount_to_pay.cents,
|
||||
user: current_user,
|
||||
conference: @conference)
|
||||
|
||||
update_purchased_ticket_purchases
|
||||
|
||||
redirect_to conference_conference_registration_path(@conference.short_title), flash:
|
||||
{ success: 'Thanks! You have purchased your tickets successfully.' }
|
||||
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
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ class Payment < ActiveRecord::Base
|
|||
attr_accessor :stripeEmail
|
||||
attr_accessor :stripeToken
|
||||
|
||||
validates :last4, presence: true
|
||||
validates :authorization_code, presence: true
|
||||
validates :status, presence: true
|
||||
validates :amount, presence: true, numericality: { greater_than: 0 }
|
||||
validates :user_id, presence: true
|
||||
|
|
@ -24,24 +22,26 @@ class Payment < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def purchase
|
||||
customer = Stripe::Customer.create email: stripeEmail,
|
||||
source: stripeToken,
|
||||
description: user.name
|
||||
begin
|
||||
customer = Stripe::Customer.create email: stripeEmail,
|
||||
source: stripeToken,
|
||||
description: user.name
|
||||
|
||||
gateway_response = Stripe::Charge.create customer: customer.id,
|
||||
receipt_email: stripeEmail,
|
||||
description: 'ticket purchases',
|
||||
amount: amount_to_pay,
|
||||
currency: conference.tickets.first.price_currency
|
||||
gateway_response = Stripe::Charge.create customer: customer.id,
|
||||
receipt_email: stripeEmail,
|
||||
description: 'ticket purchases',
|
||||
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
|
||||
self.last4 = gateway_response[:source][:last4]
|
||||
self.authorization_code = gateway_response[:id]
|
||||
self.status = 'success'
|
||||
true
|
||||
|
||||
rescue Stripe::CardError => e
|
||||
flash[:error] = e.message
|
||||
false
|
||||
rescue => error
|
||||
errors.add(:base, error.message)
|
||||
self.status = 'failure'
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue