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 Gateway into PaymentMethod::CreditCard #2001

Merged
merged 11 commits into from
Jun 21, 2017
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Solidus 2.3.0 (master, unreleased)

- Renamed `Spree::Gateway` payment method into `Spree::PaymentMethod::CreditCard` [\#2001](https://github/com/solidusio/solidus/pull/2001) ([tvdeyen](https://github.com/tvdeyen))
Run `rake solidus:migrations:rename_gateways:up` to migrate your data.

- 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
11 changes: 4 additions & 7 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ integration.
* `Spree::Payment` - Manage and process a payment for an order, from a specific
source (e.g. `Spree::CreditCard`) using a specific payment method (e.g
`Solidus::Gateway::Braintree`).
* `Spree::PaymentMethod` - An abstract class which is implemented most commonly
as a `Spree::Gateway`.
* `Spree::Gateway` - A concrete implementation of `Spree::PaymentMethod`
intended to provide a base for extension. See
https://github.com/solidusio/solidus_gateway/ for offically supported payment
gateway implementations.
* `Spree::CreditCard` - The default `source` of a `Spree::Payment`.
* `Spree::PaymentMethod` - A base class which is used for implementing payment methods.
* `Spree::PaymentMethod::CreditCard` - An implementation of a `Spree::PaymentMethod` for credit card payments.
See https://github.com/solidusio/solidus_gateway/ for officially supported payment method implementations.
* `Spree::CreditCard` - The `source` of a `Spree::Payment` using `Spree::PaymentMethod::CreditCard` as payment method.

## The Inventory Sub-System
* `Spree::ReturnAuthorization` - Models the return of Inventory Units to
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/billing_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class BillingIntegration < PaymentMethod
preference :server, :string, default: 'test'
preference :test_mode, :boolean, default: true

def provider
def gateway
integration_options = options
ActiveMerchant::Billing::Base.integration_mode = integration_options[:server].to_sym
integration_options[:test] = true if integration_options[:test_mode]
@provider ||= provider_class.new(integration_options)
@gateway ||= gateway_class.new(integration_options)
end

def options
Expand Down
66 changes: 8 additions & 58 deletions core/app/models/spree/gateway.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,12 @@
module Spree
# A concrete implementation of `Spree::PaymentMethod` intended to provide a
# base for extension. See https://github.com/solidusio/solidus_gateway/ for
# offically supported payment gateway implementations.
#
class Gateway < PaymentMethod
delegate :authorize, :purchase, :capture, :void, :credit, to: :provider

validates :name, :type, presence: true

preference :server, :string, default: 'test'
preference :test_mode, :boolean, default: true

def payment_source_class
CreditCard
end

def provider
gateway_options = options
gateway_options.delete :login if gateway_options.key?(:login) && gateway_options[:login].nil?
if gateway_options[:server]
ActiveMerchant::Billing::Base.mode = gateway_options[:server].to_sym
end
@provider ||= provider_class.new(gateway_options)
end

def options
preferences.to_hash
end

def payment_profiles_supported?
false
end

def method_type
'gateway'
end

def supports?(source)
return true unless provider_class.respond_to? :supports?
return true if source.brand && provider_class.supports?(source.brand)
source.has_payment_profile?
end

def reusable_sources_by_order(order)
source_ids = order.payments.where(payment_method_id: id).pluck(:source_id).uniq
payment_source_class.where(id: source_ids).select(&:reusable?)
end
alias_method :sources_by_order, :reusable_sources_by_order
deprecate sources_by_order: :reusable_sources_by_order, deprecator: Spree::Deprecation

def reusable_sources(order)
if order.completed?
reusable_sources_by_order(order)
elsif order.user_id
order.user.wallet.wallet_payment_sources.map(&:payment_source).select(&:reusable?)
else
[]
end
# @deprecated Use Spree::PaymentMethod::CreditCard or Spree::PaymentMethod instead
class Gateway < PaymentMethod::CreditCard
def initialize(*args)
Spree::Deprecation.warn \
"Using Spree::Gateway as parent class of payment methods is deprecated. " \
"Please use Spree::PaymentMethod::CreditCard for credit card based payment methods " \
"or Spree::PaymentMethod for non credit card payment methods instead."
super
end
end
end
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
Loading