Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRY-up BogusCreditCard model and remove invasive refund spec stub #4002

Merged
merged 2 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions core/app/models/spree/payment_method/bogus_credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 28 additions & 32 deletions core/spec/models/spree/refund_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down