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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex ae87e742ec..8112780547 100644--- a/core/app/models/spree/payment/processing.rb+++ b/core/app/models/spree/payment/processing.rb@@ -152,6 +152,7 @@ module Spreedef protect_from_connection_erroryieldrescue ActiveMerchant::ConnectionError => e+ failure!gateway_error(e)enddiff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rbindex 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 doendcontext '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)+ endendendend@@ -312,19 +327,34 @@ describe Spree::Payment, type: :model doendcontext '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)+ endend- 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)+ endendendend@@ -386,11 +416,26 @@ describe Spree::Payment, type: :model doendcontext '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)+ endendendend@@ -447,10 +492,25 @@ describe Spree::Payment, type: :model doendcontext '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)+ endendend