Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

failure payment when connection error is raised (#11532)

Kacper Mękarski11fd3979a446
diff --git a/core/app/models/spree/payment/processing.rb b/core/app/models/spree/payment/processing.rb
index ae87e742ec..8112780547 100644
--- a/core/app/models/spree/payment/processing.rb
+++ b/core/app/models/spree/payment/processing.rb
@@ -152,6 +152,7 @@ module Spree
def protect_from_connection_error
yield
rescue ActiveMerchant::ConnectionError => e
+ failure!
gateway_error(e)
end
diff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rb
index 9c2ce7b22f..d86caa3f3a 100644
--- a/core/spec/models/spree/payment_spec.rb
+++ b/core/spec/models/spree/payment_spec.rb
@@ -258,13 +258,28 @@ describe Spree::Payment, type: :model do
end
context 'if unsuccessful' do
- it 'marks payment as failed' do
- allow(gateway).to receive(:authorize).and_return(failed_response)
- expect(payment).to receive(:failure)
- expect(payment).not_to receive(:pend)
- expect do
- payment.authorize!
- end.to raise_error(Spree::Core::GatewayError)
+ context 'when response is returned from gateway' do
+ it 'marks payment as failed' do
+ allow(gateway).to receive(:authorize).and_return(failed_response)
+ expect(payment).to receive(:failure)
+ expect(payment).not_to receive(:pend)
+ expect do
+ payment.authorize!
+ end.to raise_error(Spree::Core::GatewayError)
+ end
+ end
+
+ context 'when there is an error connecting to the gateway' do
+ let(:connection_error_message) { 'gateway_error' }
+ let(:connection_error) { ActiveMerchant::ConnectionError.new(connection_error_message, nil) }
+
+ before { allow(gateway).to receive(:authorize).and_raise(connection_error) }
+
+ it 'raises ActiveMerchant::ConnectionError and marks payment as failed' do
+ expect(payment).to receive(:failure!)
+ expect(payment).not_to receive(:pend)
+ expect { payment.authorize! }.to raise_error(Spree::Core::GatewayError)
+ end
end
end
end
@@ -312,19 +327,34 @@ describe Spree::Payment, type: :model do
end
context 'if unsuccessful' do
- before do
- allow(gateway).to receive(:purchase).and_return(failed_response)
- expect(payment).to receive(:failure)
- expect(payment).not_to receive(:pend)
- end
+ context 'when response is returned from gateway' do
+ before do
+ allow(gateway).to receive(:purchase).and_return(failed_response)
+ expect(payment).to receive(:failure)
+ expect(payment).not_to receive(:pend)
+ end
- it 'makes payment failed' do
- expect { payment.purchase! }.to raise_error(Spree::Core::GatewayError)
+ it 'makes payment failed' do
+ expect { payment.purchase! }.to raise_error(Spree::Core::GatewayError)
+ end
+
+ it 'does not log a capture event' do
+ expect { payment.purchase! }.to raise_error(Spree::Core::GatewayError)
+ expect(payment.capture_events.count).to eq(0)
+ end
end
- it 'does not log a capture event' do
- expect { payment.purchase! }.to raise_error(Spree::Core::GatewayError)
- expect(payment.capture_events.count).to eq(0)
+ context 'when there is an error connecting to the gateway' do
+ let(:connection_error_message) { 'gateway_error' }
+ let(:connection_error) { ActiveMerchant::ConnectionError.new(connection_error_message, nil) }
+
+ before { allow(gateway).to receive(:purchase).and_raise(connection_error) }
+
+ it 'raises ActiveMerchant::ConnectionError and marks payment as failed' do
+ expect(payment).to receive(:failure!)
+ expect(payment).not_to receive(:pend)
+ expect { payment.purchase! }.to raise_error(Spree::Core::GatewayError)
+ end
end
end
end
@@ -386,11 +416,26 @@ describe Spree::Payment, type: :model do
end
context 'if unsuccessful' do
- it 'does not make payment complete' do
- allow(gateway).to receive_messages capture: failed_response
- expect(payment).to receive(:failure)
- expect(payment).not_to receive(:complete)
- expect { payment.capture! }.to raise_error(Spree::Core::GatewayError)
+ context 'when response is returned from gateway' do
+ it 'does not make payment complete' do
+ allow(gateway).to receive_messages capture: failed_response
+ expect(payment).to receive(:failure)
+ expect(payment).not_to receive(:complete)
+ expect { payment.capture! }.to raise_error(Spree::Core::GatewayError)
+ end
+ end
+
+ context 'when there is an error connecting to the gateway' do
+ let(:connection_error_message) { 'gateway_error' }
+ let(:connection_error) { ActiveMerchant::ConnectionError.new(connection_error_message, nil) }
+
+ before { allow(gateway).to receive(:capture).and_raise(connection_error) }
+
+ it 'raises ActiveMerchant::ConnectionError and marks payment as failed' do
+ expect(payment).to receive(:failure!)
+ expect(payment).not_to receive(:pend)
+ expect { payment.capture! }.to raise_error(Spree::Core::GatewayError)
+ end
end
end
end
@@ -447,10 +492,25 @@ describe Spree::Payment, type: :model do
end
context 'if unsuccessful' do
- it 'does not void the payment' do
- allow(gateway).to receive_messages void: failed_response
- expect(payment).not_to receive(:void)
- expect { payment.void_transaction! }.to raise_error(Spree::Core::GatewayError)
+ context 'when response is returned from gateway' do
+ it 'does not void the payment' do
+ allow(gateway).to receive_messages void: failed_response
+ expect(payment).not_to receive(:void)
+ expect { payment.void_transaction! }.to raise_error(Spree::Core::GatewayError)
+ end
+ end
+
+ context 'when there is an error connecting to the gateway' do
+ let(:connection_error_message) { 'gateway_error' }
+ let(:connection_error) { ActiveMerchant::ConnectionError.new(connection_error_message, nil) }
+
+ before { allow(gateway).to receive(:void).and_raise(connection_error) }
+
+ it 'raises ActiveMerchant::ConnectionError and marks payment as failed' do
+ expect(payment).to receive(:failure!)
+ expect(payment).not_to receive(:pend)
+ expect { payment.void_transaction! }.to raise_error(Spree::Core::GatewayError)
+ end
end
end