diff --git a/app/models/payment.rb b/app/models/payment.rb index 9cab6592..e9386a35 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -33,7 +33,7 @@ class Payment < ActiveRecord::Base self.status = 'success' true - rescue => error + rescue Stripe::StripeError => error errors.add(:base, error.message) self.status = 'failure' false diff --git a/spec/features/ticket_purchases_spec.rb b/spec/features/ticket_purchases_spec.rb index 3d42473f..1d67c285 100644 --- a/spec/features/ticket_purchases_spec.rb +++ b/spec/features/ticket_purchases_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'stripe_mock' feature Registration do let!(:ticket) { create(:ticket) } @@ -8,15 +9,19 @@ feature Registration do context 'as a participant' do before(:each) do sign_in participant + StripeMock.start end after(:each) do + StripeMock.stop sign_out end + let(:stripe_helper) { StripeMock.create_test_helper } + context 'who is not registered' do - scenario 'purchases a ticket', feature: true, js: true do + scenario 'purchases and pays for a ticket succcessfully', feature: true, js: true do visit root_path click_link 'Register' @@ -32,6 +37,12 @@ feature Registration do expect(flash).to eq('Please pay here to purchase tickets.') purchase = TicketPurchase.where(user_id: participant.id, ticket_id: ticket.id).first expect(purchase.quantity).to eq(2) + + # token = stripe_helper.generate_card_token + # merge token, email and submit form + # page.execute_script("$('form').submit()") + + expect(current_path).to eq(conference_conference_registration_path(conference.short_title)) end end end diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index 1a2be88b..71277146 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -45,7 +45,7 @@ describe Payment do after { StripeMock.stop } before { TicketPurchase.purchase(conference, user, tickets) } - let!(:payment) { create(:payment, user: user, conference: conference, stripe_customer_token: stripe_helper.generate_card_token, stripe_customer_email: user.email) } + let(:payment) { create(:payment, user: user, conference: conference, stripe_customer_token: stripe_helper.generate_card_token, stripe_customer_email: user.email) } context 'when the payment is successful' do before { payment.purchase } @@ -68,8 +68,6 @@ describe Payment do end context 'if the payment is not successful' do - before { StripeMock.prepare_card_error(:invalid_number) } - let(:payment) { create(:payment, user: user, conference: conference, stripe_customer_token: 'bogus_card_token', stripe_customer_email: user.email) } before { payment.purchase } @@ -88,6 +86,54 @@ describe Payment do expect(payment.errors[:base].count).to eq(1) end end + + context 'when the connection to Stripe drops' do + it 'raises exception' do + StripeMock.prepare_error(Stripe::APIConnectionError.new) + expect{ Stripe::Charge.create }.to raise_error(Stripe::APIConnectionError) + expect{ payment.purchase }.not_to raise_error + end + end + + context 'when there is a Stripe API Error' do + it 'raises exception' do + StripeMock.prepare_error(Stripe::APIError.new) + expect{ Stripe::Charge.create }.to raise_error(Stripe::APIError) + expect{ payment.purchase }.not_to raise_error + end + end + + context 'when there is authentication error' do + it 'raises exception' do + StripeMock.prepare_error(Stripe::AuthenticationError.new) + expect{ Stripe::Charge.create }.to raise_error(Stripe::AuthenticationError) + expect{ payment.purchase }.not_to raise_error + end + end + + context 'when there is a card error' do + it 'raises exception' do + StripeMock.prepare_card_error(:card_declined) + expect{ Stripe::Charge.create }.to raise_error(Stripe::CardError) + expect{ payment.purchase }.not_to raise_error + end + end + + context 'when the request to Stripe is invalid' do + it 'raises exception' do + StripeMock.prepare_error(Stripe::InvalidRequestError.new('Your request is invalid.', code: 402)) + expect{ Stripe::Charge.create }.to raise_error(Stripe::InvalidRequestError) + expect{ payment.purchase }.not_to raise_error + end + end + + context 'when Stripe rate limit exceeds' do + it 'raises exception' do + StripeMock.prepare_error(Stripe::RateLimitError.new) + expect{ Stripe::Charge.create }.to raise_error(Stripe::RateLimitError) + expect{ payment.purchase }.not_to raise_error + end + end end end end