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

Rename bogus gateways #2000

Merged
merged 3 commits into from
Jun 21, 2017
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Solidus 2.3.0 (master, unreleased)

- Renamed bogus payment methods [\#2000](https://github.com/solidusio/solidus/pull/2000) ([tvdeyen](https://github.com/tvdeyen))
`Spree::Gateway::BogusSimple` and `Spree::Gateway::Bogus` were renamed into `Spree::PaymentMethod::SimpleBogusCreditCard` and `Spree::PaymentMethod::BogusCreditCard`
Run `rake solidus:migrations:rename_gateways:up` to migrate your data.

- Deprecate `Spree::Core::CurrentStore` in favor of `Spree::CurrentStoreSelector`. [\#1993](https://github.com/solidusio/solidus/pull/1993)
- Deprecate `Spree::Order#assign_default_addresses!` in favor of `Order.new.assign_default_user_addresses`. [\#1954](https://github.com/solidusio/solidus/pull/1954) ([kennyadsl](https://github.com/kennyadsl))
- Change how line item options are allowed in line items controller. [\#1943](https://github.com/solidusio/solidus/pull/1943)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ module Spree

it "can update payment method and transition from payment to confirm" do
order.update_column(:state, "payment")
allow_any_instance_of(Spree::Gateway::Bogus).to receive(:source_required?).and_return(false)
allow_any_instance_of(Spree::PaymentMethod::BogusCreditCard).to receive(:source_required?).and_return(false)
api_put :update, id: order.to_param, order_token: order.guest_token,
order: { payments_attributes: [{ payment_method_id: @payment_method.id }] }
expect(json_response['state']).to eq('confirm')
Expand Down
10 changes: 5 additions & 5 deletions api/spec/controllers/spree/api/payments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Spree

context "payment source is not required" do
before do
allow_any_instance_of(Spree::Gateway::Bogus).to receive(:source_required?).and_return(false)
allow_any_instance_of(Spree::PaymentMethod::BogusCreditCard).to receive(:source_required?).and_return(false)
end

it "can create a new payment" do
Expand Down Expand Up @@ -160,7 +160,7 @@ module Spree
context "authorization fails" do
before do
fake_response = double(success?: false, to_s: "Could not authorize card")
expect_any_instance_of(Spree::Gateway::Bogus).to receive(:authorize).and_return(fake_response)
expect_any_instance_of(Spree::PaymentMethod::BogusCreditCard).to receive(:authorize).and_return(fake_response)
api_put :authorize, id: payment.to_param
end

Expand All @@ -187,7 +187,7 @@ module Spree
context "capturing fails" do
before do
fake_response = double(success?: false, to_s: "Insufficient funds")
expect_any_instance_of(Spree::Gateway::Bogus).to receive(:capture).and_return(fake_response)
expect_any_instance_of(Spree::PaymentMethod::BogusCreditCard).to receive(:capture).and_return(fake_response)
end

it "returns a 422 status" do
Expand All @@ -209,7 +209,7 @@ module Spree
context "purchasing fails" do
before do
fake_response = double(success?: false, to_s: "Insufficient funds")
expect_any_instance_of(Spree::Gateway::Bogus).to receive(:purchase).and_return(fake_response)
expect_any_instance_of(Spree::PaymentMethod::BogusCreditCard).to receive(:purchase).and_return(fake_response)
end

it "returns a 422 status" do
Expand All @@ -231,7 +231,7 @@ module Spree
context "voiding fails" do
before do
fake_response = double(success?: false, to_s: "NO REFUNDS")
expect_any_instance_of(Spree::Gateway::Bogus).to receive(:void).and_return(fake_response)
expect_any_instance_of(Spree::PaymentMethod::BogusCreditCard).to receive(:void).and_return(fake_response)
end

it "returns a 422 status" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ class GatewayWithPassword < PaymentMethod

context "tries to save invalid payment" do
it "doesn't break, responds nicely" do
post :create, params: { payment_method: { name: "", type: "Spree::Gateway::Bogus" } }
post :create, params: { payment_method: { name: "", type: "Spree::PaymentMethod::BogusCreditCard" } }
end
end

it "can create a payment method of a valid type" do
expect {
post :create, params: { payment_method: { name: "Test Method", type: "Spree::Gateway::Bogus" } }
post :create, params: { payment_method: { name: "Test Method", type: "Spree::PaymentMethod::BogusCreditCard" } }
}.to change(Spree::PaymentMethod, :count).by(1)

expect(response).to be_redirect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
context "changing type and payment_source", js: true do
after do
# cleanup
Spree::Config.static_model_preferences.for_class(Spree::Gateway::Bogus).clear
Spree::Config.static_model_preferences.for_class(Spree::PaymentMethod::BogusCreditCard).clear
end

it "displays message when changing type" do
Expand All @@ -81,13 +81,13 @@
expect(page).to have_no_content('Test Mode')

# change back
select2_search 'Spree::Gateway::Bogus', from: 'Provider'
select2_search 'Spree::PaymentMethod::BogusCreditCard', from: 'Provider'
expect(page).to have_no_content('you must save first')
expect(page).to have_content('Test Mode')
end

it "displays message when changing preference source" do
Spree::Config.static_model_preferences.add(Spree::Gateway::Bogus, 'my_prefs', {})
Spree::Config.static_model_preferences.add(Spree::PaymentMethod::BogusCreditCard, 'my_prefs', {})

create(:credit_card_payment_method)
click_link "Payment Methods"
Expand All @@ -105,7 +105,7 @@
end

it "updates successfully and keeps secrets" do
Spree::Config.static_model_preferences.add(Spree::Gateway::Bogus, 'my_prefs', { server: 'secret' })
Spree::Config.static_model_preferences.add(Spree::PaymentMethod::BogusCreditCard, 'my_prefs', { server: 'secret' })

create(:credit_card_payment_method)
click_link "Payment Methods"
Expand Down
90 changes: 7 additions & 83 deletions core/app/models/spree/gateway/bogus.rb
Original file line number Diff line number Diff line change
@@ -1,87 +1,11 @@
module Spree
class Gateway::Bogus < Gateway
TEST_VISA = ['4111111111111111', '4012888888881881', '4222222222222']
TEST_MC = ['5500000000000004', '5555555555554444', '5105105105105100']
TEST_AMEX = ['378282246310005', '371449635398431', '378734493671000', '340000000000009']
TEST_DISC = ['6011000000000004', '6011111111111117', '6011000990139424']

VALID_CCS = ['1', TEST_VISA, TEST_MC, TEST_AMEX, TEST_DISC].flatten

attr_accessor :test

def provider_class
self.class
end

def create_profile(payment)
return if payment.source.has_payment_profile?
# simulate the storage of credit card profile using remote service
if success = VALID_CCS.include?(payment.source.number)
payment.source.update_attributes(gateway_customer_profile_id: generate_profile_id(success))
end
end

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' })
else
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, 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' })
else
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', 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')
end

def capture(_money, authorization, _gateway_options)
if authorization == '12345'
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true)
else
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', error: 'Bogus Gateway: Forced failure', test: true)
end
end

def void(_response_code, _credit_card, _options = {})
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345')
end

def cancel(_response_code)
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345')
end

def test?
# Test mode is not really relevant with bogus gateway (no such thing as live server)
true
end

def payment_profiles_supported?
true
end

def actions
%w(capture void credit)
end

private

def generate_profile_id(success)
record = true
prefix = success ? 'BGS' : 'FAIL'
while record
random = "#{prefix}-#{Array.new(6){ rand(6) }.join}"
record = Spree::CreditCard.where(gateway_customer_profile_id: random).first
end
random
# @deprecated Use Spree::PaymentMethod::BogusCreditCard instead
class Gateway::Bogus < PaymentMethod::BogusCreditCard
def initialize(*args)
Spree::Deprecation.warn \
'Spree::Gateway::Bogus is deprecated. ' \
'Please use Spree::PaymentMethod::BogusCreditCard instead'
super
end
end
end
27 changes: 7 additions & 20 deletions core/app/models/spree/gateway/bogus_simple.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
module Spree
# Bogus Gateway that doesn't support payment profiles.
class Gateway::BogusSimple < Gateway::Bogus
def payment_profiles_supported?
false
end

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' })
else
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, 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' })
else
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true)
end
# @deprecated Use Spree::PaymentMethod::SimpleBogusCreditCard instead
class Gateway::BogusSimple < Spree::PaymentMethod::SimpleBogusCreditCard
def initialize(*args)
Spree::Deprecation.warn \
'Spree::Gateway::BogusSimple is deprecated. ' \
'Please use Spree::PaymentMethod::SimpleBogusCreditCard instead'
super
end
end
end
87 changes: 87 additions & 0 deletions core/app/models/spree/payment_method/bogus_credit_card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
module Spree
class PaymentMethod::BogusCreditCard < Gateway
TEST_VISA = ['4111111111111111', '4012888888881881', '4222222222222']
TEST_MC = ['5500000000000004', '5555555555554444', '5105105105105100']
TEST_AMEX = ['378282246310005', '371449635398431', '378734493671000', '340000000000009']
TEST_DISC = ['6011000000000004', '6011111111111117', '6011000990139424']

VALID_CCS = ['1', TEST_VISA, TEST_MC, TEST_AMEX, TEST_DISC].flatten

attr_accessor :test

def provider_class
self.class
end

def create_profile(payment)
return if payment.source.has_payment_profile?
# simulate the storage of credit card profile using remote service
if success = VALID_CCS.include?(payment.source.number)
payment.source.update_attributes(gateway_customer_profile_id: generate_profile_id(success))
end
end

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' })
else
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, 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' })
else
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', 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')
end

def capture(_money, authorization, _gateway_options)
if authorization == '12345'
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true)
else
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', error: 'Bogus Gateway: Forced failure', test: true)
end
end

def void(_response_code, _credit_card, _options = {})
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345')
end

def cancel(_response_code)
ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345')
end

def test?
# Test mode is not really relevant with bogus gateway (no such thing as live server)
true
end

def payment_profiles_supported?
true
end

def actions
%w(capture void credit)
end

private

def generate_profile_id(success)
record = true
prefix = success ? 'BGS' : 'FAIL'
while record
random = "#{prefix}-#{Array.new(6){ rand(6) }.join}"
record = Spree::CreditCard.where(gateway_customer_profile_id: random).first
end
random
end
end
end
24 changes: 24 additions & 0 deletions core/app/models/spree/payment_method/simple_bogus_credit_card.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Spree
# Bogus Gateway that doesn't support payment profiles.
class PaymentMethod::SimpleBogusCreditCard < PaymentMethod::BogusCreditCard
def payment_profiles_supported?
false
end

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' })
else
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', { message: 'Bogus Gateway: Forced failure' }, 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' })
else
ActiveMerchant::Billing::Response.new(false, 'Bogus Gateway: Forced failure', message: 'Bogus Gateway: Forced failure', test: true)
end
end
end
end
13 changes: 13 additions & 0 deletions core/db/migrate/20170608074534_rename_bogus_gateways.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class RenameBogusGateways < ActiveRecord::Migration[5.0]
def up
say_with_time 'Renaming bogus gateways into payment methods' do
Rake::Task['solidus:migrations:rename_gateways:up'].invoke
end
end

def down
say_with_time 'Renaming bogus payment methods into gateways' do
Rake::Task['solidus:migrations:rename_gateways:down'].invoke
end
end
end
Loading