Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Payment Method Store Credit fixes (#12242)
Damian Legawiec61749236f204
diff --git a/admin/app/views/spree/admin/payment_methods/_form.html.erb b/admin/app/views/spree/admin/payment_methods/_form.html.erbindex 060aa34a3f..bbdb514231 100644--- a/admin/app/views/spree/admin/payment_methods/_form.html.erb+++ b/admin/app/views/spree/admin/payment_methods/_form.html.erb@@ -40,7 +40,12 @@<% if can?(:manage, Spree::Store) && available_stores.count > 1 %><div class="form-group"><%= label_tag :payment_method_stores, Spree.t(:stores) %>- <%= collection_select(:payment_method, :store_ids, available_stores, :id, :unique_name, {}, { multiple: true, class: 'select2' }) %>+ <%= f.collection_check_boxes :store_ids, available_stores, :id, :name do |b| %>+ <div class="custom-control custom-checkbox mb-1">+ <%= b.check_box(class: 'custom-control-input') %>+ <%= b.label(class: 'custom-control-label') %>+ </div>+ <% end %></div><% end %>diff --git a/core/app/models/spree/gateway.rb b/core/app/models/spree/gateway.rbindex b2fe8a959f..c7bf5643dc 100644--- a/core/app/models/spree/gateway.rb+++ b/core/app/models/spree/gateway.rb@@ -4,8 +4,6 @@ module Spreedelegate :authorize, :purchase, :capture, :void, :credit, to: :provider- after_initialize :set_name, if: :new_record?-validates :type, presence: true, inclusion: { in: :valid_providers_list }def payment_source_class@@ -79,10 +77,6 @@ module Spreeprivate- def set_name- self.name ||= self.class.name.demodulize.titleize.gsub(/Gateway/, '').strip- end-def valid_providers_listSpree::PaymentMethod.providers.map(&:to_s)enddiff --git a/core/app/models/spree/payment_method.rb b/core/app/models/spree/payment_method.rbindex 5dcc887faa..f8ed27f748 100644--- a/core/app/models/spree/payment_method.rb+++ b/core/app/models/spree/payment_method.rb@@ -15,6 +15,8 @@ module Spreescope :available_on_front_end, -> { active.where(display_on: [:front_end, :both]) }scope :available_on_back_end, -> { active.where(display_on: [:back_end, :both]) }+ after_initialize :set_name, if: :new_record?+validates :name, presence: trueauto_strip_attributes :name@@ -112,5 +114,9 @@ module Spreedef public_preference_keys[]end++ def set_name+ self.name ||= self.class.name.demodulize.titleize.gsub(/Gateway/, '').strip+ endendenddiff --git a/core/app/models/spree/payment_method/store_credit.rb b/core/app/models/spree/payment_method/store_credit.rbindex 69ba536c30..8e1a09c3ba 100644--- a/core/app/models/spree/payment_method/store_credit.rb+++ b/core/app/models/spree/payment_method/store_credit.rb@@ -16,14 +16,6 @@ module Spreepayment.pending?end- def editable?- amount_used.zero? && amount_authorized.zero?- end-- def can_be_deleted?- amount_used.zero? && amount_authorized.zero?- end-def authorize(amount_in_cents, store_credit, gateway_options = {})if store_credit.nil?ActiveMerchant::Billing::Response.new(false, Spree.t('store_credit_payment_method.unable_to_find'), {}, {})diff --git a/core/app/models/spree/promotion_handler/promotion_duplicator.rb b/core/app/models/spree/promotion_handler/promotion_duplicator.rbindex e88c34bb96..f9652bb191 100644--- a/core/app/models/spree/promotion_handler/promotion_duplicator.rb+++ b/core/app/models/spree/promotion_handler/promotion_duplicator.rb@@ -2,7 +2,7 @@ module Spreemodule PromotionHandlerclass PromotionDuplicatordef initialize(promotion, random_string: nil)- @promotion = promotion+ @promotion = promotion.reload@random_string = random_string || generate_random_string(4)end@@ -19,7 +19,7 @@ module Spreecopy_actionsend- @new_promotion+ @new_promotion.reloadendprivate@@ -27,7 +27,8 @@ module Spreedef copy_rules@promotion.promotion_rules.each do |rule|new_rule = rule.dup- @new_promotion.promotion_rules << new_rule+ new_rule.promotion = @new_promotion+ new_rule.save!new_rule.users = rule.users if rule.try(:users)new_rule.taxons = rule.taxons if rule.try(:taxons)@@ -43,9 +44,9 @@ module Spreedef copy_actions@promotion.promotion_actions.each do |action|new_action = action.dup+ new_action.promotion = @new_promotionnew_action.calculator = action.calculator.dup if action.try(:calculator)-- @new_promotion.promotion_actions << new_action+ new_action.save!next unless action.try(:promotion_action_line_items)diff --git a/core/app/models/spree/store_credit.rb b/core/app/models/spree/store_credit.rbindex a1f272ba06..ed4b27be46 100644--- a/core/app/models/spree/store_credit.rb+++ b/core/app/models/spree/store_credit.rb@@ -170,6 +170,14 @@ module Spreepayment.completed? && payment.credit_allowed > 0end+ def editable?+ amount_used.zero? && amount_authorized.zero?+ end++ def can_be_deleted?+ amount_used.zero? && amount_authorized.zero?+ end+def generate_authorization_code"#{id}-SC-#{Time.now.utc.strftime('%Y%m%d%H%M%S%6N')}"enddiff --git a/core/lib/spree/testing_support/common_rake.rb b/core/lib/spree/testing_support/common_rake.rbindex 9875cdad9a..879ebf6f09 100644--- a/core/lib/spree/testing_support/common_rake.rb+++ b/core/lib/spree/testing_support/common_rake.rb@@ -38,10 +38,10 @@ namespace :common do]puts 'Setting up dummy database...'- system('bin/rails db:environment:set RAILS_ENV=test')- system('bundle exec rake db:drop db:create')+ system('bin/rails db:environment:set RAILS_ENV=test > /dev/null 2>&1')+ system('bundle exec rake db:drop db:create > /dev/null 2>&1')Spree::DummyModelGenerator.start- system('bundle exec rake db:migrate')+ system('bundle exec rake db:migrate > /dev/null 2>&1')beginrequire "generators/#{ENV['LIB_NAME']}/install/install_generator"@@ -52,13 +52,12 @@ namespace :common doendunless ['spree/api', 'spree/core', 'spree/sample'].include?(ENV['LIB_NAME'])- puts 'Precompiling assets...'- system('bundle exec rake assets:precompile')+ system('bundle exec rake assets:precompile > /dev/null 2>&1')endendtask :seed do |_t|puts 'Seeding ...'- system('bundle exec rake db:seed RAILS_ENV=test')+ system('bundle exec rake db:seed RAILS_ENV=test > /dev/null 2>&1')endenddiff --git a/core/lib/spree/testing_support/factories/promotion_factory.rb b/core/lib/spree/testing_support/factories/promotion_factory.rbindex 6a9c509782..bf312f3bcc 100644--- a/core/lib/spree/testing_support/factories/promotion_factory.rb+++ b/core/lib/spree/testing_support/factories/promotion_factory.rb@@ -5,14 +5,13 @@ FactoryBot.define doendafter(:create) do |promotion, evaluator|- rule = Spree::Promotion::Rules::ItemTotal.create!(+ Spree::Promotion::Rules::ItemTotal.create!(+ promotion: promotion,preferred_operator_min: 'gte',preferred_operator_max: 'lte',preferred_amount_min: evaluator.item_total_threshold_amount,preferred_amount_max: evaluator.item_total_threshold_amount + 100)- promotion.rules << rule- promotion.save!endend@@ -61,9 +60,7 @@ FactoryBot.define doafter(:create) do |promotion, evaluator|calculator = Spree::Calculator::FlatRate.newcalculator.preferred_amount = evaluator.weighted_order_adjustment_amount- action = Spree::Promotion::Actions::CreateAdjustment.create!(calculator: calculator)- promotion.actions << action- promotion.save!+ Spree::Promotion::Actions::CreateAdjustment.create!(calculator: calculator, promotion: promotion)endend@@ -74,9 +71,7 @@ FactoryBot.define doname { 'Free Shipping Promotion' }after(:create) do |promotion|- action = Spree::Promotion::Actions::FreeShipping.new- action.promotion = promotion- action.save+ Spree::Promotion::Actions::FreeShipping.create!(promotion: promotion)endfactory :free_shipping_promotion_with_item_total_rule, traits: [:with_item_total_rule]diff --git a/core/spec/models/spree/adjustable/adjuster/promotion_spec.rb b/core/spec/models/spree/adjustable/adjuster/promotion_spec.rbindex b9e7c24512..f68ff9c2d7 100644--- a/core/spec/models/spree/adjustable/adjuster/promotion_spec.rb+++ b/core/spec/models/spree/adjustable/adjuster/promotion_spec.rb@@ -12,7 +12,8 @@ describe Spree::Adjustable::Adjuster::Promotion, type: :model docontext 'best promotion is always applied' dolet(:calculator) { Spree::Calculator::FlatRate.new(preferred_amount: 10) }- let(:source) { Spree::Promotion::Actions::CreateItemAdjustments.create calculator: calculator }+ let(:promotion) { create(:promotion) }+ let(:source) { Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator: calculator, promotion: promotion) }def create_adjustment(label, amount)create(:adjustment,diff --git a/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb b/core/spec/models/spree/promotion/actions/create_adjustment_spec.rbindex 1784f757fa..e45022f413 100644--- a/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb+++ b/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb@@ -3,7 +3,7 @@ require 'spec_helper'describe Spree::Promotion::Actions::CreateAdjustment, type: :model dolet(:order) { create(:order_with_line_items, line_items_count: 1) }let(:promotion) { create(:promotion) }- let(:action) { Spree::Promotion::Actions::CreateAdjustment.new }+ let(:action) { Spree::Promotion::Actions::CreateAdjustment.new(promotion: promotion) }let(:payload) { { order: order } }it_behaves_like 'an adjustment source'diff --git a/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb b/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rbindex f3ca1283c8..6f3181d6a3 100644--- a/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb+++ b/core/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb@@ -128,8 +128,9 @@ module Spreeendcontext '#destroy' do- let!(:action) { CreateItemAdjustments.create! }- let(:other_action) { CreateItemAdjustments.create! }+ let(:other_promotion) { create(:promotion) }+ let!(:action) { CreateItemAdjustments.create!(promotion: promotion) }+ let!(:other_action) { CreateItemAdjustments.create!(promotion: other_promotion) }before { promotion.promotion_actions = [other_action] }diff --git a/core/spec/models/spree/promotion/actions/create_line_items_spec.rb b/core/spec/models/spree/promotion/actions/create_line_items_spec.rbindex b6aa3e2325..326bfc4042 100644--- a/core/spec/models/spree/promotion/actions/create_line_items_spec.rb+++ b/core/spec/models/spree/promotion/actions/create_line_items_spec.rb@@ -2,7 +2,7 @@ require 'spec_helper'describe Spree::Promotion::Actions::CreateLineItems, type: :model dolet(:order) { create(:order) }- let(:action) { Spree::Promotion::Actions::CreateLineItems.create }+ let(:action) { Spree::Promotion::Actions::CreateLineItems.create!(promotion: promotion) }let(:promotion) { create(:promotion) }let(:shirt) { create(:variant) }let(:mug) { create(:variant) }diff --git a/core/spec/models/spree/promotion/actions/free_shipping_spec.rb b/core/spec/models/spree/promotion/actions/free_shipping_spec.rbindex 8b8227d778..48c1baf4ca 100644--- a/core/spec/models/spree/promotion/actions/free_shipping_spec.rb+++ b/core/spec/models/spree/promotion/actions/free_shipping_spec.rb@@ -3,7 +3,7 @@ require 'spec_helper'describe Spree::Promotion::Actions::FreeShipping, type: :model dolet(:order) { create(:completed_order_with_totals) }let(:promotion) { create(:promotion) }- let(:action) { Spree::Promotion::Actions::FreeShipping.create }+ let(:action) { Spree::Promotion::Actions::FreeShipping.create!(promotion: promotion) }let(:payload) { { order: order } }it_behaves_like 'an adjustment source'diff --git a/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rb b/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rbindex 3516efd698..12854d05de 100644--- a/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rb+++ b/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rb@@ -27,7 +27,7 @@ describe Spree::PromotionHandler::PromotionDuplicator doenddescribe '#duplicate' do- let(:new_promotion) { subject.duplicate }+ let!(:new_promotion) { subject.duplicate }context 'model fields' dolet(:excluded_fields) { ['code', 'name', 'path', 'id', 'created_at', 'updated_at', 'deleted_at'] }@@ -54,7 +54,7 @@ describe Spree::PromotionHandler::PromotionDuplicator doit "promotion rule's fields (except promotion_id) are the same" doold_rule = promotion.promotion_rules.first- new_rule = new_promotion.promotion_rules.first+ new_rule = new_promotion.reload.promotion_rules.firstold_rule.attributes.each_key do |key|expect(old_rule.send(key)).to eq new_rule.send(key) unless excluded_fields.include?(key)diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rbindex 2c986b2be8..9dcd58de88 100644--- a/core/spec/models/spree/promotion_spec.rb+++ b/core/spec/models/spree/promotion_spec.rb@@ -234,8 +234,8 @@ describe Spree::Promotion, type: :model doenddescribe '#activate' do- let(:action1) { Spree::Promotion::Actions::CreateAdjustment.create! }- let(:action2) { Spree::Promotion::Actions::CreateAdjustment.create! }+ let(:action1) { Spree::Promotion::Actions::CreateAdjustment.create!(promotion: promotion) }+ let(:action2) { Spree::Promotion::Actions::CreateAdjustment.create!(promotion: promotion) }let(:user) { create(:user) }let(:order) { create(:order, user: user) }let(:payload) { { order: order, user: user } }@@ -244,7 +244,6 @@ describe Spree::Promotion, type: :model doallow(action1).to receive_messages perform: trueallow(action2).to receive_messages perform: true- promotion.promotion_actions = [action1, action2]promotion.created_at = 2.days.agoend@@ -546,8 +545,8 @@ describe Spree::Promotion, type: :model doendcontext "with 'all' match policy" do- let(:promo1) { Spree::PromotionRule.create! }- let(:promo2) { Spree::PromotionRule.create! }+ let(:promo1) { Spree::PromotionRule.create!(promotion: promotion) }+ let(:promo2) { Spree::PromotionRule.create!(promotion: promotion) }before { promotion.match_policy = 'all' }@@ -596,7 +595,7 @@ describe Spree::Promotion, type: :model doit 'has eligible rules if any of the rules are eligible' doallow_any_instance_of(Spree::PromotionRule).to receive_messages(applicable?: true)- true_rule = Spree::PromotionRule.create(promotion: promotion)+ true_rule = Spree::PromotionRule.create!(promotion: promotion)allow(true_rule).to receive_messages(eligible?: true)allow(promotion).to receive_messages(rules: [true_rule])allow(promotion).to receive_message_chain(:rules, :for).and_return([true_rule])@@ -604,7 +603,7 @@ describe Spree::Promotion, type: :model doendcontext 'when none of the rules are eligible' do- let(:promo) { Spree::PromotionRule.create! }+ let(:promo) { Spree::PromotionRule.create!(promotion: promotion) }let(:errors) { double ActiveModel::Errors, empty?: false }before dodiff --git a/core/spec/support/concerns/adjustment_source.rb b/core/spec/support/concerns/adjustment_source.rbindex 6dfb6bd31d..5549af68ef 100644--- a/core/spec/support/concerns/adjustment_source.rb+++ b/core/spec/support/concerns/adjustment_source.rb@@ -1,5 +1,11 @@shared_examples_for 'an adjustment source' do- subject(:source) { described_class.create }+ subject(:source) do+ if defined?(promotion) && described_class.reflect_on_association(:promotion)+ described_class.create!(promotion: promotion)+ else+ described_class.create+ end+ endbefore doallow(Spree::Adjustable::AdjustmentsUpdater).to receive(:update)diff --git a/sample/db/samples/promotions.rb b/sample/db/samples/promotions.rbindex ecc0fe4233..26a70262ee 100644--- a/sample/db/samples/promotions.rb+++ b/sample/db/samples/promotions.rb@@ -12,15 +12,16 @@ promotion = Spree::Promotion.where(usage_limit: nil,path: nil,match_policy: 'any',- description: ''-).first_or_create do |promo|+ description: '',+ code: 'FREESHIP'+).first_or_create! do |promo|promo.stores = Spree::Store.allendSpree::PromotionRule.where(- promotion_id: promotion.id,+ promotion: promotion,type: 'Spree::Promotion::Rules::OptionValue',preferences: { match_policy: 'any', eligible_values: { product.id.to_s => eligible_values } }).first_or_create!-Spree::Promotion::Actions::FreeShipping.where(promotion_id: promotion.id).first_or_create!+Spree::Promotion::Actions::FreeShipping.where(promotion: promotion).first_or_create!