diff --git a/core/app/models/spree/payment_method/bogus_credit_card.rb b/core/app/models/spree/payment_method/bogus_credit_card.rb index ffe3a23ab1a..d3e0bbc4323 100644 --- a/core/app/models/spree/payment_method/bogus_credit_card.rb +++ b/core/app/models/spree/payment_method/bogus_credit_card.rb @@ -9,6 +9,10 @@ class PaymentMethod::BogusCreditCard < PaymentMethod::CreditCard VALID_CCS = ['1', TEST_VISA, TEST_MC, TEST_AMEX, TEST_DISC].flatten + AUTHORIZATION_CODE = '12345' + FAILURE_MESSAGE = 'Bogus Gateway: Forced failure' + SUCCESS_MESSAGE = 'Bogus Gateway: Forced success' + attr_accessor :test def gateway_class @@ -26,40 +30,40 @@ def create_profile(payment) def authorize(_money, credit_card, _options = {}) profile_id = credit_card.gateway_customer_profile_id if VALID_CCS.include?(credit_card.number) || (profile_id && profile_id.starts_with?('BGS-')) - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'D' }) + ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE, avs_result: { code: 'D' }) else - ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, test: true) + ActiveMerchant::Billing::Response.new(false, FAILURE_MESSAGE, { message: FAILURE_MESSAGE }, test: true) end end def purchase(_money, credit_card, _options = {}) profile_id = credit_card.gateway_customer_profile_id if VALID_CCS.include?(credit_card.number) || (profile_id && profile_id.starts_with?('BGS-')) - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'M' }) + ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE, avs_result: { code: 'M' }) else - ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true) + ActiveMerchant::Billing::Response.new(false, FAILURE_MESSAGE, message: FAILURE_MESSAGE, test: true) end end def credit(_money, _credit_card, _response_code, _options = {}) - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345') + ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE) end def capture(_money, authorization, _gateway_options) if authorization == '12345' - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true) + ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true) else - ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', error: 'Bogus Gateway: Forced failure', test: true) + ActiveMerchant::Billing::Response.new(false, FAILURE_MESSAGE, error: FAILURE_MESSAGE, test: true) end end def void(_response_code, _credit_card, _options = {}) - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345') + ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE) end # @see Spree::PaymentMethod#try_void def try_void(_payment) - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345') + ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE) end def test? diff --git a/core/app/models/spree/payment_method/simple_bogus_credit_card.rb b/core/app/models/spree/payment_method/simple_bogus_credit_card.rb index 22786f51f38..f959c30a923 100644 --- a/core/app/models/spree/payment_method/simple_bogus_credit_card.rb +++ b/core/app/models/spree/payment_method/simple_bogus_credit_card.rb @@ -9,17 +9,17 @@ def payment_profiles_supported? def authorize(_money, credit_card, _options = {}) if VALID_CCS.include? credit_card.number - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'A' }) + ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE, avs_result: { code: 'A' }) else - ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, test: true) + ActiveMerchant::Billing::Response.new(false, FAILURE_MESSAGE, { message: FAILURE_MESSAGE }, test: true) end end def purchase(_money, credit_card, _options = {}) if VALID_CCS.include? credit_card.number - ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345', avs_result: { code: 'A' }) + ActiveMerchant::Billing::Response.new(true, SUCCESS_MESSAGE, {}, test: true, authorization: AUTHORIZATION_CODE, avs_result: { code: 'A' }) else - ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true) + ActiveMerchant::Billing::Response.new(false, FAILURE_MESSAGE, message: FAILURE_MESSAGE, test: true) end end end diff --git a/core/spec/models/spree/refund_spec.rb b/core/spec/models/spree/refund_spec.rb index 8d45a9ccd8a..d1867761286 100644 --- a/core/spec/models/spree/refund_spec.rb +++ b/core/spec/models/spree/refund_spec.rb @@ -6,27 +6,12 @@ let(:amount) { 100.0 } let(:amount_in_cents) { amount * 100 } - let(:authorization) { generate(:refund_transaction_id) } - let(:payment) { create(:payment, amount: payment_amount, payment_method: payment_method) } let(:payment_amount) { amount * 2 } let(:payment_method) { create(:credit_card_payment_method) } let(:refund_reason) { create(:refund_reason) } - let(:gateway_response) { - ActiveMerchant::Billing::Response.new( - gateway_response_success, - gateway_response_message, - gateway_response_params, - gateway_response_options - ) - } - let(:gateway_response_success) { true } - let(:gateway_response_message) { "" } - let(:gateway_response_params) { {} } - let(:gateway_response_options) { { authorization: authorization } } - let(:transaction_id) { nil } let(:refund) do @@ -39,13 +24,6 @@ ) end - before do - allow(payment.payment_method) - .to receive(:credit) - .with(amount_in_cents, payment.source, payment.transaction_id, { originator: an_instance_of(Spree::Refund) }) - .and_return(gateway_response) - end - describe 'create' do subject { refund } @@ -67,7 +45,10 @@ subject { refund.perform! } it "sets #perform_response with the gateway response from the payment provider" do - expect { subject }.to change { refund.perform_response }.from(nil).to(gateway_response) + expect { subject }.to change { refund.perform_response }.from(nil) + + expect(refund.perform_response).to be_a(ActiveMerchant::Billing::Response) + expect(refund.perform_response.message).to eq(Spree::PaymentMethod::BogusCreditCard::SUCCESS_MESSAGE) end it "sets a transaction_id" do @@ -100,8 +81,7 @@ end it 'saves the returned authorization value' do - subject - expect(refund.reload.transaction_id).to eq authorization + expect { subject }.to change { refund.reload.transaction_id }.from(nil).to(Spree::PaymentMethod::BogusCreditCard::AUTHORIZATION_CODE) end it 'saves the passed amount as the refund amount' do @@ -115,7 +95,7 @@ end it "attempts to process a transaction" do - expect(payment.payment_method).to receive(:credit).once + expect(payment.payment_method).to receive(:credit).once.and_call_original subject end @@ -125,13 +105,29 @@ end end - context "processing fails" do - let(:gateway_response_success) { false } - let(:gateway_response_message) { "failure message" } + context "when processing fails" do + let(:failure_message) { Spree::PaymentMethod::BogusCreditCard::FAILURE_MESSAGE } + let(:gateway_response) { + ActiveMerchant::Billing::Response.new( + false, + failure_message, + {}, + test: true, + authorization: Spree::PaymentMethod::BogusCreditCard::AUTHORIZATION_CODE + ) + } + + before do + allow(payment.payment_method) + .to receive(:credit) + .with(amount_in_cents, payment.source, payment.transaction_id, { originator: an_instance_of(Spree::Refund) }) + .and_return(gateway_response) + end + context 'without performing after create' do it 'raises a GatewayError' do - expect { subject }.to raise_error(Spree::Core::GatewayError, gateway_response_message) + expect { subject }.to raise_error(Spree::Core::GatewayError, failure_message) end end end @@ -145,7 +141,7 @@ expect(payment.payment_method) .to receive(:credit) .with(amount * 100, payment.transaction_id, { originator: an_instance_of(Spree::Refund) }) - .and_return(gateway_response) + .and_call_original subject end @@ -160,7 +156,7 @@ expect(payment.payment_method) .to receive(:credit) .with(amount_in_cents, payment.source, payment.transaction_id, { originator: an_instance_of(Spree::Refund) }) - .and_return(gateway_response) + .and_call_original subject end