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
Improve AddStoreCredit service error reporting
Damian Legawiec53d65a394ec4
diff --git a/core/app/services/spree/checkout/add_store_credit.rb b/core/app/services/spree/checkout/add_store_credit.rbindex f1a8844d82..57ff4f2649 100644--- a/core/app/services/spree/checkout/add_store_credit.rb+++ b/core/app/services/spree/checkout/add_store_credit.rb@@ -9,9 +9,12 @@ module Spreeremaining_total = amount ? [amount, @order.outstanding_balance].min : @order.outstanding_balance+ return failure(nil, Spree.t(:error_user_does_not_have_any_store_credits)) unless @order.user&.store_credits&.any?+ApplicationRecord.transaction do@order.payments.store_credits.where(state: :checkout).map(&:invalidate!)- apply_store_credits(remaining_total) if @order.user&.store_credits&.any?++ apply_store_credits(remaining_total)end@order.reload.payments.store_credits.valid.any? ? success(@order) : failure(@order)diff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex adb6f14946..bdd0a62907 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -986,6 +986,7 @@ en:one: 1 error prohibited this record from being savedother: ! '%{count} errors prohibited this record from being saved'error_user_destroy_with_orders: User associated with orders cannot be destroyed+ error_user_does_not_have_any_store_credits: 'User does not have any Store Credits available'event: Eventevents:spree:diff --git a/core/spec/services/spree/checkout/add_store_credit_spec.rb b/core/spec/services/spree/checkout/add_store_credit_spec.rbindex ab99f95801..7150b3c7e1 100644--- a/core/spec/services/spree/checkout/add_store_credit_spec.rb+++ b/core/spec/services/spree/checkout/add_store_credit_spec.rb@@ -24,6 +24,11 @@ describe Spree::Checkout::AddStoreCredit, type: :service doit 'does not create a store credit payment' doexpect(order.payments.count).to eq 0end++ it 'returns error' do+ expect(subject.success?).to eq(false)+ expect(subject.error.to_s).to eq('User does not have any Store Credits available')+ endendcontext 'there is enough store credit to pay for the entire order' do