diff --git a/app/models/customer_account_transaction.rb b/app/models/customer_account_transaction.rb index f425a62d32..d596757bfc 100644 --- a/app/models/customer_account_transaction.rb +++ b/app/models/customer_account_transaction.rb @@ -5,8 +5,6 @@ require "spree/localized_number" class CustomerAccountTransaction < ApplicationRecord extend Spree::LocalizedNumber - DEFAULT_PAYMENT_METHOD_NAME = "api_payment_method.name" - localize_number :amount belongs_to :customer @@ -43,7 +41,9 @@ class CustomerAccountTransaction < ApplicationRecord # Creates the first transaction with a 0 amount def create_initial_transaction - api_payment_method = Spree::PaymentMethod.find_by!(name: DEFAULT_PAYMENT_METHOD_NAME) + api_payment_method = Spree::PaymentMethod.find_by!( + name: Rails.application.config.api_payment_method[:name] + ) CustomerAccountTransaction.create!( customer: customer, amount: 0.00, diff --git a/app/models/spree/payment_method/customer_credit.rb b/app/models/spree/payment_method/customer_credit.rb index 2619adccbe..b532d04a20 100644 --- a/app/models/spree/payment_method/customer_credit.rb +++ b/app/models/spree/payment_method/customer_credit.rb @@ -13,14 +13,10 @@ module Spree customer = Customer.find_by(id: options[:customer_id]) return error_response("customer_not_found") if customer.nil? - return error_response("missing_payment") if options[:payment_id].nil? - - payment_method = Spree::PaymentMethod.find_by(name: "credit_payment_method.name") return error_response("credit_payment_method_missing") if payment_method.nil? available_credit = customer.customer_account_transactions.last&.balance - return error_response("no_credit_available") if available_credit.nil? return error_response("not_enough_credit_available") if calculated_amount > available_credit @@ -57,6 +53,12 @@ module Spree private + def payment_method + Spree::PaymentMethod.find_by( + name: Rails.application.config.credit_payment_method[:name] + ) + end + def error_response(translation_key) message = I18n.t(translation_key, scope: "credit_payment_method.errors") ActiveMerchant::Billing::Response.new(false, message) diff --git a/spec/factories/customer_account_transaction_factory.rb b/spec/factories/customer_account_transaction_factory.rb index 4270fb7276..6bba7d800d 100644 --- a/spec/factories/customer_account_transaction_factory.rb +++ b/spec/factories/customer_account_transaction_factory.rb @@ -5,6 +5,8 @@ FactoryBot.define do customer { build(:customer) } amount { 10.00 } currency { "AUD" } - payment_method { build(:payment_method) } + payment_method { + build(:payment_method, name: Rails.application.config.api_payment_method[:name]) + } end end diff --git a/spec/models/customer_account_transaction_spec.rb b/spec/models/customer_account_transaction_spec.rb index d36395fd39..0b5dc3a827 100644 --- a/spec/models/customer_account_transaction_spec.rb +++ b/spec/models/customer_account_transaction_spec.rb @@ -3,10 +3,6 @@ require 'spec_helper' RSpec.describe CustomerAccountTransaction do - let!(:payment_method) { - create(:payment_method, name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME) - } - describe "validations" do subject { build(:customer_account_transaction) } @@ -60,12 +56,21 @@ RSpec.describe CustomerAccountTransaction do end context "when the default payment method is missing" do - let!(:payment_method) { nil } + around do |example| + # A Customer account transaction is linked to a customer which is linked to an enterprise. + # That means FactoryBot will create an enterprise, so we disable the after create callback + # so that credit payment are not created. + Enterprise.skip_callback(:create, :after, :add_credit_payment_method) + example.run + Enterprise.set_callback(:create, :after, :add_credit_payment_method) + end it "raises an error" do - expect { create(:customer_account_transaction, amount: 12.00) }.to raise_error( - ActiveRecord::RecordNotFound - ) + expect do + create( + :customer_account_transaction, amount: 12.00, payment_method: create(:payment_method) + ) + end.to raise_error(ActiveRecord::RecordNotFound) end end end diff --git a/spec/models/spree/payment_method/customer_credit_spec.rb b/spec/models/spree/payment_method/customer_credit_spec.rb index 680fcfd4bd..b7ce76adab 100644 --- a/spec/models/spree/payment_method/customer_credit_spec.rb +++ b/spec/models/spree/payment_method/customer_credit_spec.rb @@ -8,9 +8,6 @@ RSpec.describe Spree::PaymentMethod::CustomerCredit do describe "#purchase" do let(:response) { subject.purchase(amount, nil, options) } - let!(:payment_method) { - create(:payment_method, name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME) - } let!(:credit_payment_method) { create(:customer_credit_payment_method) } @@ -95,6 +92,15 @@ RSpec.describe Spree::PaymentMethod::CustomerCredit do context "when credit payment method is not configured" do let!(:credit_payment_method) { nil } + around do |example| + # Customer is needed to create a purchase and a customer which is linked to an enterprise. + # That means FactoryBot will create an enterprise, so we disable the after create callback + # so that credit payment methods are not created. + Enterprise.skip_callback(:create, :after, :add_credit_payment_method) + example.run + Enterprise.set_callback(:create, :after, :add_credit_payment_method) + end + it "returns an error" do expect(response.success?).to be(false) expect(response.message).to eq("Credit payment method is missing") diff --git a/spec/queries/customers_with_balance_query_spec.rb b/spec/queries/customers_with_balance_query_spec.rb index f87d878d3b..c9d9238cd3 100644 --- a/spec/queries/customers_with_balance_query_spec.rb +++ b/spec/queries/customers_with_balance_query_spec.rb @@ -212,15 +212,6 @@ RSpec.describe CustomersWithBalanceQuery do end context "with customer payments" do - # TODO should not be needed, need to add this to seed somehow - let!(:payment_method) { - create( - :payment_method, - name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME, - distributors: [customer.enterprise] - ) - } - it 'returns the customer available credit' do create(:customer_account_transaction, customer:, amount: 10.00) create(:customer_account_transaction, customer:, amount: -2.00) diff --git a/spec/requests/admin/customer_account_transaction_spec.rb b/spec/requests/admin/customer_account_transaction_spec.rb index 5a3211f487..651e77efe9 100644 --- a/spec/requests/admin/customer_account_transaction_spec.rb +++ b/spec/requests/admin/customer_account_transaction_spec.rb @@ -7,13 +7,6 @@ RSpec.describe Admin::CustomerAccountTransactionController do let(:enterprise_user) { create(:user, enterprises: [enterprise]) } let(:enterprise) { create(:enterprise) } let(:customer) { create(:customer, enterprise:) } - let!(:payment_method) { - create( - :payment_method, - name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME, - distributors: [enterprise] - ) - } before do login_as enterprise_user diff --git a/spec/requests/api/v1/customer_account_transaction_spec.rb b/spec/requests/api/v1/customer_account_transaction_spec.rb index 3fcba1aa3a..324ec68146 100644 --- a/spec/requests/api/v1/customer_account_transaction_spec.rb +++ b/spec/requests/api/v1/customer_account_transaction_spec.rb @@ -4,12 +4,9 @@ require "swagger_helper" RSpec.describe "CustomerAccountTransactions", swagger_doc: "v1.yaml", feature: :api_v1 do let!(:enterprise) { create(:enterprise) } + # Paymnent method will be created when the enterprise is created let(:payment_method) { - create( - :payment_method, - name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME, - distributors: [enterprise] - ) + Spree::PaymentMethod.find_by(name: Rails.application.config.api_payment_method[:name]) } let(:customer) { create(:customer) } diff --git a/spec/services/customer_account_transactions/data_loader_service_spec.rb b/spec/services/customer_account_transactions/data_loader_service_spec.rb index f98600d880..6b574ed2ae 100644 --- a/spec/services/customer_account_transactions/data_loader_service_spec.rb +++ b/spec/services/customer_account_transactions/data_loader_service_spec.rb @@ -5,13 +5,6 @@ require 'spec_helper' RSpec.describe CustomerAccountTransactions::DataLoaderService do subject { described_class.new(user:, enterprise:) } - let!(:payment_method) { - create( - :payment_method, - name: CustomerAccountTransaction::DEFAULT_PAYMENT_METHOD_NAME, - distributors: [enterprise] - ) - } let(:user) { create(:user) } let(:enterprise) { create(:distributor_enterprise) }