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
Add public preferences to payment methods Storefront API (#11301)
Damian Legawiec646519154996
diff --git a/api/app/serializers/spree/v2/storefront/payment_method_serializer.rb b/api/app/serializers/spree/v2/storefront/payment_method_serializer.rbindex de1abf90fe..7292dd8506 100644--- a/api/app/serializers/spree/v2/storefront/payment_method_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/payment_method_serializer.rb@@ -5,6 +5,10 @@ module Spreeset_type :payment_methodattributes :type, :name, :description++ attribute :preferences do |object|+ object.public_preferences.as_json+ endendendenddiff --git a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rbindex 073c49aa9d..ea40f53afa 100644--- a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb@@ -558,6 +558,7 @@ describe 'API V2 Storefront Checkout Spec', type: :request doexpect(json_response['data'][0]).to have_attribute(:name).with_value(payment_method.name)expect(json_response['data'][0]).to have_attribute(:description).with_value(payment_method.description)expect(json_response['data'][0]).to have_attribute(:type).with_value(payment_method.type)+ expect(json_response['data'][0]).to have_attribute(:preferences).with_value(payment_method.public_preferences.as_json)endenddiff --git a/core/app/models/spree/gateway/bogus.rb b/core/app/models/spree/gateway/bogus.rbindex 8ec3c810b5..70b7589e96 100644--- a/core/app/models/spree/gateway/bogus.rb+++ b/core/app/models/spree/gateway/bogus.rb@@ -9,14 +9,12 @@ module Spreeattr_accessor :test+ preference :dummy_key, :string, default: 'PUBLICKEY123'+def provider_classself.classend- def preferences- {}- end-def create_profile(payment)return if payment.source.has_payment_profile?@@ -88,5 +86,9 @@ module Spreeendrandomend++ def public_preference_keys+ [:dummy_key]+ endendenddiff --git a/core/app/models/spree/payment_method.rb b/core/app/models/spree/payment_method.rbindex 974e036ddf..4ab85eaefa 100644--- a/core/app/models/spree/payment_method.rb+++ b/core/app/models/spree/payment_method.rb@@ -87,5 +87,17 @@ module Spreestore_ids.include?(store.id)end++ def public_preferences+ public_preference_keys.each_with_object({}) do |key, hash|+ hash[key] = preferences[key]+ end+ end++ protected++ def public_preference_keys+ []+ endendenddiff --git a/core/spec/models/spree/gateway_spec.rb b/core/spec/models/spree/gateway_spec.rbindex f121b34e67..712068bfe2 100644--- a/core/spec/models/spree/gateway_spec.rb+++ b/core/spec/models/spree/gateway_spec.rb@@ -8,9 +8,19 @@ describe Spree::Gateway, type: :model doendclass TestGateway < Spree::Gateway+ preference :publishable_preference1, :string+ preference :publishable_preference2, :string+ preference :private_preference, :string+def provider_classProviderend++ private++ def public_preference_keys+ [:publishable_preference1, :publishable_preference2]+ endendit 'passes through all arguments on a method_missing call' do@@ -55,4 +65,16 @@ describe Spree::Gateway, type: :model dorate = Spree::Gateway::FROM_DOLLAR_TO_CENT_RATEexpect(gateway.exchange_multiplier).to eq rateend++ it 'returns public preferences' do+ gateway = TestGateway.new+ gateway.preferences[:publishable_preference1] = 'public1'+ gateway.preferences[:publishable_preference2] = 'public2'+ gateway.preferences[:private_preference] = 'secret'++ expect(gateway.public_preferences).to eq({+ publishable_preference1: 'public1',+ publishable_preference2: 'public2'+ })+ endend