diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e131df3475..1413f510f79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/api/spec/controllers/spree/api/checkouts_controller_spec.rb b/api/spec/controllers/spree/api/checkouts_controller_spec.rb index 0755fc809bd..764bd68dc8f 100644 --- a/api/spec/controllers/spree/api/checkouts_controller_spec.rb +++ b/api/spec/controllers/spree/api/checkouts_controller_spec.rb @@ -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') diff --git a/api/spec/controllers/spree/api/payments_controller_spec.rb b/api/spec/controllers/spree/api/payments_controller_spec.rb index 372af60cf47..6b63e5bf2f4 100644 --- a/api/spec/controllers/spree/api/payments_controller_spec.rb +++ b/api/spec/controllers/spree/api/payments_controller_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/backend/spec/controllers/spree/admin/payment_methods_controller_spec.rb b/backend/spec/controllers/spree/admin/payment_methods_controller_spec.rb index 0bbe044bb97..d09d931bad9 100644 --- a/backend/spec/controllers/spree/admin/payment_methods_controller_spec.rb +++ b/backend/spec/controllers/spree/admin/payment_methods_controller_spec.rb @@ -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 diff --git a/backend/spec/features/admin/configuration/payment_methods_spec.rb b/backend/spec/features/admin/configuration/payment_methods_spec.rb index b4fbce13544..a9d2c3179a5 100644 --- a/backend/spec/features/admin/configuration/payment_methods_spec.rb +++ b/backend/spec/features/admin/configuration/payment_methods_spec.rb @@ -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 @@ -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" @@ -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" diff --git a/core/README.md b/core/README.md index da3208ea11d..f048031a5d3 100644 --- a/core/README.md +++ b/core/README.md @@ -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 diff --git a/core/app/models/spree/billing_integration.rb b/core/app/models/spree/billing_integration.rb index 53287742af2..0c1018d5b03 100644 --- a/core/app/models/spree/billing_integration.rb +++ b/core/app/models/spree/billing_integration.rb @@ -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 diff --git a/core/app/models/spree/gateway.rb b/core/app/models/spree/gateway.rb index 35976afa7df..ac02cc262fc 100644 --- a/core/app/models/spree/gateway.rb +++ b/core/app/models/spree/gateway.rb @@ -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 diff --git a/core/app/models/spree/gateway/bogus.rb b/core/app/models/spree/gateway/bogus.rb index 7d1946801a8..269704565e7 100644 --- a/core/app/models/spree/gateway/bogus.rb +++ b/core/app/models/spree/gateway/bogus.rb @@ -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 diff --git a/core/app/models/spree/gateway/bogus_simple.rb b/core/app/models/spree/gateway/bogus_simple.rb index 568353967ae..3328cf0e7e4 100644 --- a/core/app/models/spree/gateway/bogus_simple.rb +++ b/core/app/models/spree/gateway/bogus_simple.rb @@ -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 diff --git a/core/app/models/spree/payment_method.rb b/core/app/models/spree/payment_method.rb index d6d0d7d7901..db5abe7a3f8 100644 --- a/core/app/models/spree/payment_method.rb +++ b/core/app/models/spree/payment_method.rb @@ -1,12 +1,23 @@ module Spree - # An abstract class which is implemented most commonly as a `Spree::Gateway`. + # A base class which is used for implementing payment methods. + # + # See https://github.com/solidusio/solidus_gateway/ for + # offically supported payment method implementations. + # + # Uses STI (single table inheritance) to store all implemented payment methods + # in one table (+spree_payment_methods+). + # + # This class is not meant to be instantiated. Please create instances of concrete payment methods. # class PaymentMethod < Spree::Base + preference :server, :string, default: 'test' + preference :test_mode, :boolean, default: true + acts_as_paranoid acts_as_list DISPLAY = [:both, :front_end, :back_end] - validates :name, presence: true + validates :name, :type, presence: true has_many :payments, class_name: "Spree::Payment", inverse_of: :payment_method has_many :credit_cards, class_name: "Spree::CreditCard" @@ -22,19 +33,83 @@ class PaymentMethod < Spree::Base store.payment_methods.empty? ? all : where(id: store.payment_method_ids) end + delegate :authorize, :purchase, :capture, :void, :credit, to: :gateway + include Spree::Preferences::StaticallyConfigurable - def self.providers - Rails.application.config.spree.payment_methods + class << self + def providers + Rails.application.config.spree.payment_methods + end + + def available(display_on = nil, store: nil) + Spree::Deprecation.warn "Spree::PaymentMethod.available is deprecated."\ + "Please use .active, .available_to_users, and .available_to_admin scopes instead."\ + "For payment methods associated with a specific store, use Spree::PaymentMethod.available_to_store(your_store)"\ + " as the base applying any further filtering" + + display_on = display_on.to_s + + available_payment_methods = + case display_on + when 'front_end' + active.available_to_users + when 'back_end' + active.available_to_admin + else + active.available_to_users.available_to_admin + end + available_payment_methods.select do |p| + store.nil? || store.payment_methods.empty? || store.payment_methods.include?(p) + end + end + + def active? + where(type: to_s, active: true).count > 0 + end + + def find_with_destroyed(*args) + unscoped { find(*args) } + end + end + + # Represents the gateway of this payment method + # + # The gateway is responsible for communicating with the providers API. + # + # It implements methods for: + # + # - authorize + # - purchase + # - capture + # - void + # - credit + # + def gateway + 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 + @gateway ||= gateway_class.new(gateway_options) end + alias_method :provider, :gateway + deprecate provider: :gateway, deprecator: Spree::Deprecation - def provider_class - raise ::NotImplementedError, "You must implement provider_class method for #{self.class}." + # Represents all preferences as a Hash + # + # Each preference is a key holding the value(s) and gets passed to the gateway via +gateway_options+ + # + # @return Hash + def options + preferences.to_hash end - # The class that will process payments for this payment type, used for @payment.source - # e.g. CreditCard in the case of a the Gateway payment type - # nil means the payment method doesn't require a source e.g. check + # The class that will store payment sources (re)usable with this payment method + # + # Used by Spree::Payment as source (e.g. Spree::CreditCard in the case of a credit card payment method). + # + # Returning nil means the payment method doesn't support storing sources (e.g. Spree::PaymentMethod::Check) def payment_source_class raise ::NotImplementedError, "You must implement payment_source_class method for #{self.class}." end @@ -62,40 +137,26 @@ def display_on end end - def self.available(display_on = nil, store: nil) - Spree::Deprecation.warn "Spree::PaymentMethod.available is deprecated."\ - "Please use .active, .available_to_users, and .available_to_admin scopes instead."\ - "For payment methods associated with a specific store, use Spree::PaymentMethod.available_to_store(your_store)"\ - " as the base applying any further filtering" - - display_on = display_on.to_s - - available_payment_methods = - case display_on - when 'front_end' - active.available_to_users - when 'back_end' - active.available_to_admin - else - active.available_to_users.available_to_admin - end - available_payment_methods.select do |p| - store.nil? || store.payment_methods.empty? || store.payment_methods.include?(p) - end - end - - def self.active? - where(type: to_s, active: true).count > 0 - end - + # Used as partial name for your payment method + # + # Currently your payment method needs to provide these partials: + # + # 1. app/views/spree/checkout/payment/_{method_type}.html.erb + # The form your customer enters the payment information in during checkout + # + # 2. app/views/spree/checkout/existing_payment/_{method_type}.html.erb + # The payment information of your customers reusable sources during checkout + # + # 3. app/views/spree/admin/payments/source_forms/_{method_type}.html.erb + # The form an admin enters payment information in when creating orders in the backend + # + # 4. app/views/spree/admin/payments/source_views/_{method_type}.html.erb + # The view that represents your payment method on orders in the backend + # def method_type type.demodulize.downcase end - def self.find_with_destroyed(*args) - unscoped { find(*args) } - end - def payment_profiles_supported? false end @@ -114,6 +175,11 @@ def auto_capture? auto_capture.nil? ? Spree::Config[:auto_capture] : auto_capture end + # Check if given source is supported by this payment method + # + # Please implement validation logic in your payment method implementation + # + # @see Spree::PaymentMethod::CreditCard#supports? def supports?(_source) true end @@ -125,5 +191,21 @@ def cancel(_response) def store_credit? is_a? Spree::PaymentMethod::StoreCredit end + + protected + + # Represents the gateway class of this payment method + # + def gateway_class + if respond_to? :provider_class + Spree::Deprecation.warn \ + "provider_class is deprecated and will be removed from Solidus 3.0 " \ + "(use gateway_class instead)" + public_send :provider_class + else + raise ::NotImplementedError, "You must implement gateway_class method for #{self.class}." + end + end + deprecate provider_class: :gateway_class, deprecator: Spree::Deprecation end end diff --git a/core/app/models/spree/payment_method/bogus_credit_card.rb b/core/app/models/spree/payment_method/bogus_credit_card.rb new file mode 100644 index 00000000000..4c374f73a93 --- /dev/null +++ b/core/app/models/spree/payment_method/bogus_credit_card.rb @@ -0,0 +1,87 @@ +module Spree + class PaymentMethod::BogusCreditCard < PaymentMethod::CreditCard + 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 gateway_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 diff --git a/core/app/models/spree/payment_method/credit_card.rb b/core/app/models/spree/payment_method/credit_card.rb new file mode 100644 index 00000000000..31187e84ab1 --- /dev/null +++ b/core/app/models/spree/payment_method/credit_card.rb @@ -0,0 +1,41 @@ +module Spree + # An implementation of a `Spree::PaymentMethod` for credit card payments. + # + # It's a good candidate as base class for other credit card based payment methods. + # + # See https://github.com/solidusio/solidus_gateway/ for + # officially supported payment method implementations. + # + class PaymentMethod::CreditCard < PaymentMethod + def payment_source_class + CreditCard + end + + def method_type + 'gateway' + end + + def supports?(source) + return true unless gateway_class.respond_to? :supports? + return true if source.brand && gateway_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 + end + end +end 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 new file mode 100644 index 00000000000..10e3803f892 --- /dev/null +++ b/core/app/models/spree/payment_method/simple_bogus_credit_card.rb @@ -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 diff --git a/core/db/migrate/20170608074534_rename_bogus_gateways.rb b/core/db/migrate/20170608074534_rename_bogus_gateways.rb new file mode 100644 index 00000000000..7a3da21eb61 --- /dev/null +++ b/core/db/migrate/20170608074534_rename_bogus_gateways.rb @@ -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 diff --git a/core/lib/solidus/migrations/rename_gateways.rb b/core/lib/solidus/migrations/rename_gateways.rb new file mode 100644 index 00000000000..2655597356e --- /dev/null +++ b/core/lib/solidus/migrations/rename_gateways.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +module Solidus + module Migrations + class RenameGateways + DEFAULT_MAPPING = { + 'Spree::Gateway' => 'Spree::PaymentMethod::CreditCard', + 'Spree::Gateway::Bogus' => 'Spree::PaymentMethod::BogusCreditCard', + 'Spree::Gateway::BogusSimple' => 'Spree::PaymentMethod::SimpleBogusCreditCard' + } + + attr_reader :gateway_mapping + + def initialize(gateway_mapping = DEFAULT_MAPPING) + @gateway_mapping = gateway_mapping + end + + def up + gateway_mapping.inject(0) do |count, mapping| + count + update(from: mapping[0], to: mapping[1]) + end + end + + def down + gateway_mapping.inject(0) do |count, mapping| + count + update(from: mapping[1], to: mapping[0]) + end + end + + private + + def update(from:, to:) + ActiveRecord::Base.connection.update <<-SQL.strip_heredoc + UPDATE spree_payment_methods SET type = '#{to}' WHERE type = '#{from}'; + SQL + end + end + end +end diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb index f098b013fc7..d7e4bd81fd0 100644 --- a/core/lib/spree/core/engine.rb +++ b/core/lib/spree/core/engine.rb @@ -43,8 +43,8 @@ class Engine < ::Rails::Engine initializer "spree.register.payment_methods", before: :load_config_initializers do |app| app.config.spree.payment_methods = %w[ - Spree::Gateway::Bogus - Spree::Gateway::BogusSimple + Spree::PaymentMethod::BogusCreditCard + Spree::PaymentMethod::SimpleBogusCreditCard Spree::PaymentMethod::StoreCredit Spree::PaymentMethod::Check ] diff --git a/core/lib/spree/testing_support/factories/payment_method_factory.rb b/core/lib/spree/testing_support/factories/payment_method_factory.rb index ff84ef4a061..5f4ccd85d97 100644 --- a/core/lib/spree/testing_support/factories/payment_method_factory.rb +++ b/core/lib/spree/testing_support/factories/payment_method_factory.rb @@ -1,5 +1,5 @@ FactoryGirl.define do - factory :payment_method, aliases: [:credit_card_payment_method], class: Spree::Gateway::Bogus do + factory :payment_method, aliases: [:credit_card_payment_method], class: Spree::PaymentMethod::BogusCreditCard do name 'Credit Card' available_to_admin true available_to_users true @@ -13,7 +13,7 @@ # authorize.net was moved to spree_gateway. # Leaving this factory in place with bogus in case anyone is using it. - factory :simple_credit_card_payment_method, class: Spree::Gateway::BogusSimple do + factory :simple_credit_card_payment_method, class: Spree::PaymentMethod::SimpleBogusCreditCard do name 'Credit Card' available_to_admin true available_to_users true diff --git a/core/lib/tasks/migrations/rename_gateways.rake b/core/lib/tasks/migrations/rename_gateways.rake new file mode 100644 index 00000000000..a62bed55a5e --- /dev/null +++ b/core/lib/tasks/migrations/rename_gateways.rake @@ -0,0 +1,19 @@ +require 'solidus/migrations/rename_gateways' + +namespace 'solidus:migrations:rename_gateways' do + task up: :environment do + count = Solidus::Migrations::RenameGateways.new.up + + unless ENV['VERBOSE'] == 'false' || !verbose + puts "Renamed #{count} gateways into payment methods." + end + end + + task down: :environment do + count = Solidus::Migrations::RenameGateways.new.down + + unless ENV['VERBOSE'] == 'false' || !verbose + puts "Renamed #{count} payment methods into gateways." + end + end +end diff --git a/core/spec/models/spree/gateway/bogus_simple.rb b/core/spec/models/spree/gateway/bogus_simple.rb index fb1423d9e9d..d6105b15431 100644 --- a/core/spec/models/spree/gateway/bogus_simple.rb +++ b/core/spec/models/spree/gateway/bogus_simple.rb @@ -1,18 +1,12 @@ require 'spec_helper' -describe Spree::Gateway::BogusSimple, type: :model do - subject { Spree::Gateway::BogusSimple.new } - - # regression test for https://github.com/spree/spree/issues/3824 - describe "#capture" do - it "returns success with the right response code" do - response = subject.capture(123, '12345', {}) - expect(response.message).to include("success") - end +describe Spree::Gateway::BogusSimple do + it 'is deprecated' do + expect(Spree::Deprecation).to receive(:warn) + described_class.new + end - it "returns failure with the wrong response code" do - response = subject.capture(123, 'wrong', {}) - expect(response.message).to include("failure") - end + it 'has Spree::PaymentMethod::SimpleBogusCreditCard as superclass' do + expect(described_class.ancestors).to include(Spree::PaymentMethod::SimpleBogusCreditCard) end end diff --git a/core/spec/models/spree/gateway/bogus_spec.rb b/core/spec/models/spree/gateway/bogus_spec.rb index e56887435c3..82cca16eba1 100644 --- a/core/spec/models/spree/gateway/bogus_spec.rb +++ b/core/spec/models/spree/gateway/bogus_spec.rb @@ -1,8 +1,12 @@ require 'spec_helper' -module Spree - describe Gateway::Bogus, type: :model do - let(:bogus) { create(:credit_card_payment_method) } - let!(:cc) { create(:credit_card, payment_method: bogus, gateway_customer_profile_id: "BGS-RERTERT") } +describe Spree::Gateway::Bogus do + it 'is deprecated' do + expect(Spree::Deprecation).to receive(:warn) + described_class.new + end + + it 'has Spree::PaymentMethod::BogusCreditCard as superclass' do + expect(described_class.ancestors).to include(Spree::PaymentMethod::BogusCreditCard) end end diff --git a/core/spec/models/spree/gateway_spec.rb b/core/spec/models/spree/gateway_spec.rb index 814aba26ca9..f2c9c64f6af 100644 --- a/core/spec/models/spree/gateway_spec.rb +++ b/core/spec/models/spree/gateway_spec.rb @@ -1,111 +1,12 @@ require 'spec_helper' -describe Spree::Gateway, type: :model do - class Provider - def initialize(options) - end - - def authorize; 'authorize'; end - - def purchase; 'purchase'; end - - def capture; 'capture'; end - - def void; 'void'; end - - def credit; 'credit'; end - end - - class TestGateway < Spree::Gateway - def provider_class - Provider - end +describe Spree::Gateway do + it 'is deprecated' do + expect(Spree::Deprecation).to receive(:warn) + described_class.new end - describe 'ActiveMerchant methods' do - let(:gateway) { TestGateway.new } - - it "passes through authorize" do - expect(gateway.authorize).to eq 'authorize' - end - - it "passes through purchase" do - expect(gateway.purchase).to eq 'purchase' - end - - it "passes through capture" do - expect(gateway.capture).to eq 'capture' - end - - it "passes through void" do - expect(gateway.void).to eq 'void' - end - - it "passes through credit" do - expect(gateway.credit).to eq 'credit' - end - end - - context "fetching payment sources" do - let(:store) { create :store } - let(:user) { create :user } - let(:order) { Spree::Order.create(user: user, completed_at: completed_at, store: store) } - - let(:payment_method) { create(:credit_card_payment_method) } - - let(:cc) do - create(:credit_card, - payment_method: payment_method, - gateway_customer_profile_id: "EFWE", - user: cc_user) - end - - let(:payment) do - create(:payment, order: order, source: cc, payment_method: payment_method) - end - - context 'order is not complete and credit card user is nil' do - let(:cc_user) { nil } - let(:completed_at) { nil } - - it "finds no credit cards associated to the order" do - expect(payment_method.reusable_sources(payment.order)).to be_empty - end - end - - context 'order is complete but credit card user is nil' do - let(:cc_user) { nil } - let(:completed_at) { Date.yesterday } - - it "finds credit cards associated on a order completed" do - expect(payment_method.reusable_sources(payment.order)).to eq [cc] - end - end - - context 'order is not complete but credit card has user' do - let(:cc_user) { user } - let(:completed_at) { nil } - before do - cc_user.wallet.add(cc) - end - it "finds credit cards associated to the user" do - expect(payment_method.reusable_sources(payment.order)).to eq [cc] - end - end - end - - context 'using preference_source' do - let(:klass){ Spree::Gateway::Bogus } - before do - Spree::Config.static_model_preferences.add(klass, 'test_preference_source', server: 'bar') - end - after do - Spree::Config.static_model_preferences.for_class(klass).clear - end - let(:payment_method){ create(:credit_card_payment_method, preference_source: 'test_preference_source') } - - it "reads static preferences" do - expect(payment_method.options).to eq({ server: "bar" }) - end + it 'has Spree::PaymentMethod::CreditCard as superclass' do + expect(described_class.ancestors).to include(Spree::PaymentMethod::CreditCard) end end diff --git a/core/spec/models/spree/order_capturing_spec.rb b/core/spec/models/spree/order_capturing_spec.rb index efae69eda34..9d358bde196 100644 --- a/core/spec/models/spree/order_capturing_spec.rb +++ b/core/spec/models/spree/order_capturing_spec.rb @@ -58,10 +58,10 @@ context "payment method ordering" do let(:secondary_payment_method) { SecondaryBogusPaymentMethod } - class SecondaryBogusPaymentMethod < Spree::Gateway::Bogus; end + class SecondaryBogusPaymentMethod < Spree::PaymentMethod::BogusCreditCard; end context "SecondaryBogusPaymentMethod payments are prioritized" do - let(:payment_methods) { [SecondaryBogusPaymentMethod, Spree::Gateway::Bogus] } + let(:payment_methods) { [SecondaryBogusPaymentMethod, Spree::PaymentMethod::BogusCreditCard] } it "captures SecondaryBogusPaymentMethod payments first" do @bogus_payment.update!(amount: bogus_total + 100) @@ -72,7 +72,7 @@ class SecondaryBogusPaymentMethod < Spree::Gateway::Bogus; end end context "Bogus payments are prioritized" do - let(:payment_methods) { [Spree::Gateway::Bogus, SecondaryBogusPaymentMethod] } + let(:payment_methods) { [Spree::PaymentMethod::BogusCreditCard, SecondaryBogusPaymentMethod] } it "captures Bogus payments first" do @secondary_bogus_payment.update!(amount: secondary_total + 100) @@ -89,7 +89,7 @@ class SecondaryBogusPaymentMethod < Spree::Gateway::Bogus; end before do allow(Spree::OrderCapturing).to receive(:sorted_payment_method_classes).and_return( - [SecondaryBogusPaymentMethod, Spree::Gateway::Bogus] + [SecondaryBogusPaymentMethod, Spree::PaymentMethod::BogusCreditCard] ) end @@ -103,7 +103,7 @@ class SecondaryBogusPaymentMethod < Spree::Gateway::Bogus; end context "when a payment is not needed to capture the entire order" do let(:secondary_payment_method) { SecondaryBogusPaymentMethod } - let(:payment_methods) { [Spree::Gateway::Bogus, SecondaryBogusPaymentMethod] } + let(:payment_methods) { [Spree::PaymentMethod::BogusCreditCard, SecondaryBogusPaymentMethod] } before do @bogus_payment.update!(amount: order.total) @@ -132,9 +132,9 @@ class SecondaryBogusPaymentMethod < Spree::Gateway::Bogus; end let(:secondary_payment_method) { ExceptionallyBogusPaymentMethod } let(:bogus_total) { order.total - 1 } let(:secondary_total) { 1 } - let(:payment_methods) { [Spree::Gateway::Bogus, ExceptionallyBogusPaymentMethod] } + let(:payment_methods) { [Spree::PaymentMethod::BogusCreditCard, ExceptionallyBogusPaymentMethod] } - class ExceptionallyBogusPaymentMethod < Spree::Gateway::Bogus + class ExceptionallyBogusPaymentMethod < Spree::PaymentMethod::BogusCreditCard def capture(*_args) raise ActiveMerchant::ConnectionError.new("foo", nil) end diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb index 78ec30795eb..52959074e2a 100644 --- a/core/spec/models/spree/order_spec.rb +++ b/core/spec/models/spree/order_spec.rb @@ -523,7 +523,7 @@ def merge!(other_order, user = nil) # Regression test for https://github.com/spree/spree/issues/4199 context "#available_payment_methods" do it "includes frontend payment methods" do - payment_method = Spree::PaymentMethod.create!({ + payment_method = Spree::PaymentMethod::Check.create!({ name: "Fake", active: true, available_to_users: true, @@ -533,7 +533,7 @@ def merge!(other_order, user = nil) end it "includes 'both' payment methods" do - payment_method = Spree::PaymentMethod.create!({ + payment_method = Spree::PaymentMethod::Check.create!({ name: "Fake", active: true, available_to_users: true, @@ -543,7 +543,7 @@ def merge!(other_order, user = nil) end it "does not include a payment method twice" do - payment_method = Spree::PaymentMethod.create!({ + payment_method = Spree::PaymentMethod::Check.create!({ name: "Fake", active: true, available_to_users: true, @@ -554,7 +554,7 @@ def merge!(other_order, user = nil) end it "does not include inactive payment methods" do - Spree::PaymentMethod.create!({ + Spree::PaymentMethod::Check.create!({ name: "Fake", active: false, available_to_users: true, diff --git a/core/spec/models/spree/payment_method/bogus_credit_card_spec.rb b/core/spec/models/spree/payment_method/bogus_credit_card_spec.rb new file mode 100644 index 00000000000..700c357e685 --- /dev/null +++ b/core/spec/models/spree/payment_method/bogus_credit_card_spec.rb @@ -0,0 +1,8 @@ +require 'spec_helper' + +module Spree + describe PaymentMethod::BogusCreditCard, type: :model do + let(:bogus) { create(:credit_card_payment_method) } + let!(:cc) { create(:credit_card, payment_method: bogus, gateway_customer_profile_id: "BGS-RERTERT") } + end +end diff --git a/core/spec/models/spree/payment_method/credit_card_spec.rb b/core/spec/models/spree/payment_method/credit_card_spec.rb new file mode 100644 index 00000000000..ad8bced9694 --- /dev/null +++ b/core/spec/models/spree/payment_method/credit_card_spec.rb @@ -0,0 +1,66 @@ +require 'spec_helper' + +describe Spree::PaymentMethod::CreditCard, type: :model do + context "fetching payment sources" do + let(:store) { create :store } + let(:user) { create :user } + let(:order) { Spree::Order.create(user: user, completed_at: completed_at, store: store) } + + let(:payment_method) { create(:credit_card_payment_method) } + + let(:cc) do + create(:credit_card, + payment_method: payment_method, + gateway_customer_profile_id: "EFWE", + user: cc_user) + end + + let(:payment) do + create(:payment, order: order, source: cc, payment_method: payment_method) + end + + context 'order is not complete and credit card user is nil' do + let(:cc_user) { nil } + let(:completed_at) { nil } + + it "finds no credit cards associated to the order" do + expect(payment_method.reusable_sources(payment.order)).to be_empty + end + end + + context 'order is complete but credit card user is nil' do + let(:cc_user) { nil } + let(:completed_at) { Date.yesterday } + + it "finds credit cards associated on a order completed" do + expect(payment_method.reusable_sources(payment.order)).to eq [cc] + end + end + + context 'order is not complete but credit card has user' do + let(:cc_user) { user } + let(:completed_at) { nil } + before do + cc_user.wallet.add(cc) + end + it "finds credit cards associated to the user" do + expect(payment_method.reusable_sources(payment.order)).to eq [cc] + end + end + end + + context 'using preference_source' do + let(:klass){ Spree::PaymentMethod::BogusCreditCard } + before do + Spree::Config.static_model_preferences.add(klass, 'test_preference_source', server: 'bar') + end + after do + Spree::Config.static_model_preferences.for_class(klass).clear + end + let(:payment_method){ create(:credit_card_payment_method, preference_source: 'test_preference_source') } + + it "reads static preferences" do + expect(payment_method.options).to eq({ server: "bar" }) + end + end +end diff --git a/core/spec/models/spree/payment_method/simple_bogus_credit_card_spec.rb b/core/spec/models/spree/payment_method/simple_bogus_credit_card_spec.rb new file mode 100644 index 00000000000..39bfab5d7ef --- /dev/null +++ b/core/spec/models/spree/payment_method/simple_bogus_credit_card_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe Spree::PaymentMethod::SimpleBogusCreditCard, type: :model do + subject { Spree::PaymentMethod::SimpleBogusCreditCard.new } + + # regression test for https://github.com/spree/spree/issues/3824 + describe "#capture" do + it "returns success with the right response code" do + response = subject.capture(123, '12345', {}) + expect(response.message).to include("success") + end + + it "returns failure with the wrong response code" do + response = subject.capture(123, 'wrong', {}) + expect(response.message).to include("failure") + end + end +end diff --git a/core/spec/models/spree/payment_method_spec.rb b/core/spec/models/spree/payment_method_spec.rb index 5072380513e..4b21d210697 100644 --- a/core/spec/models/spree/payment_method_spec.rb +++ b/core/spec/models/spree/payment_method_spec.rb @@ -180,8 +180,8 @@ end describe '#auto_capture?' do - class TestGateway < Spree::Gateway - def provider_class + class TestGateway < Spree::PaymentMethod::CreditCard + def gateway_class Provider end end @@ -309,4 +309,49 @@ def provider_class end end end + + describe 'ActiveMerchant methods' do + class PaymentGateway + def initialize(options) + end + + def authorize; 'authorize'; end + + def purchase; 'purchase'; end + + def capture; 'capture'; end + + def void; 'void'; end + + def credit; 'credit'; end + end + + class TestPaymentMethod < Spree::PaymentMethod + def gateway_class + PaymentGateway + end + end + + let(:payment_method) { TestPaymentMethod.new } + + it "passes through authorize" do + expect(payment_method.authorize).to eq 'authorize' + end + + it "passes through purchase" do + expect(payment_method.purchase).to eq 'purchase' + end + + it "passes through capture" do + expect(payment_method.capture).to eq 'capture' + end + + it "passes through void" do + expect(payment_method.void).to eq 'void' + end + + it "passes through credit" do + expect(payment_method.credit).to eq 'credit' + end + end end diff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rb index 8d1bdbb3492..04dfb6307b7 100644 --- a/core/spec/models/spree/payment_spec.rb +++ b/core/spec/models/spree/payment_spec.rb @@ -6,7 +6,7 @@ let(:refund_reason) { create(:refund_reason) } let(:gateway) do - gateway = Spree::Gateway::Bogus.new(active: true, name: 'Bogus gateway') + gateway = Spree::PaymentMethod::BogusCreditCard.new(active: true, name: 'Bogus gateway') allow(gateway).to receive_messages source_required: true gateway end @@ -209,7 +209,7 @@ # Regression test for https://github.com/spree/spree/issues/4598 it "should allow payments with a gateway_customer_profile_id" do payment.source.update!(gateway_customer_profile_id: "customer_1", brand: 'visa') - expect(payment.payment_method.provider_class).to receive(:supports?).with('visa').and_return(false) + expect(payment.payment_method.gateway_class).to receive(:supports?).with('visa').and_return(false) expect(payment).to receive(:started_processing!) payment.process! end @@ -217,7 +217,7 @@ # Another regression test for https://github.com/spree/spree/issues/4598 it "should allow payments with a gateway_payment_profile_id" do payment.source.update!(gateway_payment_profile_id: "customer_1", brand: 'visa') - expect(payment.payment_method.provider_class).to receive(:supports?).with('visa').and_return(false) + expect(payment.payment_method.gateway_class).to receive(:supports?).with('visa').and_return(false) expect(payment).to receive(:started_processing!) payment.process! end diff --git a/core/spec/models/spree/reimbursement_type/original_payment_spec.rb b/core/spec/models/spree/reimbursement_type/original_payment_spec.rb index e0a3d901667..f644363b4a7 100644 --- a/core/spec/models/spree/reimbursement_type/original_payment_spec.rb +++ b/core/spec/models/spree/reimbursement_type/original_payment_spec.rb @@ -54,7 +54,7 @@ module Spree context "multiple payment methods" do let(:simulate) { true } let!(:check_payment) { create(:check_payment, order: reimbursement.order, amount: 5.0, state: "completed") } - let(:payment) { reimbursement.order.payments.detect { |p| p.payment_method.is_a? Spree::Gateway::Bogus } } + let(:payment) { reimbursement.order.payments.detect { |p| p.payment_method.is_a? Spree::PaymentMethod::BogusCreditCard } } let(:refund_amount) { 10.0 } let(:refund_payment_methods) { subject.map { |refund| refund.payment.payment_method } } diff --git a/core/spec/support/test_gateway.rb b/core/spec/support/test_gateway.rb index f6cf9ea0a00..75c9524c846 100644 --- a/core/spec/support/test_gateway.rb +++ b/core/spec/support/test_gateway.rb @@ -1,2 +1,2 @@ -class Spree::Gateway::Test < Spree::Gateway +class Spree::Gateway::Test < Spree::PaymentMethod::CreditCard end diff --git a/sample/db/samples/payment_methods.rb b/sample/db/samples/payment_methods.rb index 41434aedbeb..8c5ba88cd69 100644 --- a/sample/db/samples/payment_methods.rb +++ b/sample/db/samples/payment_methods.rb @@ -1,4 +1,4 @@ -Spree::Gateway::Bogus.create!( +Spree::PaymentMethod::BogusCreditCard.create!( { name: "Credit Card", description: "Bogus payment gateway",