mirror of
https://github.com/openSUSE/osem.git
synced 2026-08-13 19:54:02 +00:00
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
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ class CreatePayments < ActiveRecord::Migration
|
|||
def change
|
||||
create_table :payments do |t|
|
||||
t.string :last4
|
||||
t.integer :amount
|
||||
t.integer :amount, null: false
|
||||
t.string :authorization_code
|
||||
t.integer :status, default: 0, null: false
|
||||
t.integer :user_id, null: false
|
||||
|
|
|
|||
|
|
@ -265,7 +265,7 @@ ActiveRecord::Schema.define(version: 20160704092023) do
|
|||
|
||||
create_table "payments", force: :cascade do |t|
|
||||
t.string "last4"
|
||||
t.integer "amount"
|
||||
t.integer "amount", null: false
|
||||
t.string "authorization_code"
|
||||
t.integer "status", default: 0, null: false
|
||||
t.integer "user_id", null: false
|
||||
|
|
|
|||
|
|
@ -2,8 +2,7 @@ FactoryGirl.define do
|
|||
factory :payment do
|
||||
user
|
||||
conference
|
||||
last4 '4242'
|
||||
authorization_code '1234567890'
|
||||
amount 10
|
||||
status 'unpaid'
|
||||
amount 1000
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -14,12 +14,8 @@ describe Payment do
|
|||
expect(build(:payment)).to be_valid
|
||||
end
|
||||
|
||||
it { is_expected.to validate_presence_of(:last4) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:amount) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:authorization_code) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:status) }
|
||||
|
||||
it { is_expected.to validate_presence_of(:user_id) }
|
||||
|
|
@ -37,8 +33,17 @@ describe Payment do
|
|||
it 'is valid with a amount greater than zero' do
|
||||
should allow_value(1).for(:amount)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe 'self#purchase'
|
||||
describe '#amount_to_pay' do
|
||||
let!(:user) { create(:user) }
|
||||
let!(:conference) { create(:conference) }
|
||||
let(:ticket_1) { create(:ticket, price: 10, price_currency: 'USD', conference: conference) }
|
||||
let(:payment) { create(:payment, user: user, conference: conference) }
|
||||
|
||||
it ' returns correct unpaid amount' do
|
||||
create(:ticket_purchase, ticket: ticket_1, user: user, quantity: 8)
|
||||
expect(payment.amount_to_pay).to eq(8000)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue