mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-02 02:11:33 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user