Handle all errors when dealing with payment event

This basically catches ActiveRecord::RecordInvalid caused by an invalid
credit record, for instance, but also other situations we haven't
forseen.
This commit is contained in:
Pau Perez
2020-07-17 14:06:55 +02:00
parent 26ed601996
commit 59da07de66
4 changed files with 15 additions and 4 deletions

View File

@@ -61,7 +61,7 @@ module Spree
else
flash[:error] = t(:cannot_perform_operation)
end
rescue Spree::Core::GatewayError => e
rescue StandardError => e
flash[:error] = e.message
ensure
redirect_to request.referer

View File

@@ -108,7 +108,7 @@ module Spree
record_response(response)
if response.success?
self.class.create(
self.class.create!(
order: order,
source: self,
payment_method: payment_method,

View File

@@ -148,8 +148,9 @@ describe Spree::Admin::PaymentsController, type: :controller do
let(:params) { { e: 'credit', order_id: order.number, id: payment.id } }
before { allow(request).to receive(:referer) { 'http://foo.com' } }
it 'handles gateway errors' do
allow(request).to receive(:referer) { 'http://foo.com' }
allow_any_instance_of(payment_method.class)
.to receive(:credit).and_raise(Spree::Core::GatewayError, 'error message')
@@ -158,6 +159,16 @@ describe Spree::Admin::PaymentsController, type: :controller do
expect(flash[:error]).to eq('error message')
expect(response).to redirect_to('http://foo.com')
end
it 'handles validation errors' do
allow(Spree::Payment).to receive(:find).with(payment.id.to_s) { payment }
allow(payment).to receive(:credit!).and_raise(StandardError, 'validation error')
spree_put :fire, params
expect(flash[:error]).to eq('validation error')
expect(response).to redirect_to('http://foo.com')
end
end
end
end

View File

@@ -391,7 +391,7 @@ describe Spree::Payment do
context "when response is successful" do
it "should create an offsetting payment" do
Spree::Payment.should_receive(:create)
Spree::Payment.should_receive(:create!)
payment.credit!
end