Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.rb
index 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 Spree
set_type :payment_method
attributes :type, :name, :description
+
+ attribute :preferences do |object|
+ object.public_preferences.as_json
+ end
end
end
end
diff --git a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
index 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 do
expect(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)
end
end
diff --git a/core/app/models/spree/gateway/bogus.rb b/core/app/models/spree/gateway/bogus.rb
index 8ec3c810b5..70b7589e96 100644
--- a/core/app/models/spree/gateway/bogus.rb
+++ b/core/app/models/spree/gateway/bogus.rb
@@ -9,14 +9,12 @@ module Spree
attr_accessor :test
+ preference :dummy_key, :string, default: 'PUBLICKEY123'
+
def provider_class
self.class
end
- def preferences
- {}
- end
-
def create_profile(payment)
return if payment.source.has_payment_profile?
@@ -88,5 +86,9 @@ module Spree
end
random
end
+
+ def public_preference_keys
+ [:dummy_key]
+ end
end
end
diff --git a/core/app/models/spree/payment_method.rb b/core/app/models/spree/payment_method.rb
index 974e036ddf..4ab85eaefa 100644
--- a/core/app/models/spree/payment_method.rb
+++ b/core/app/models/spree/payment_method.rb
@@ -87,5 +87,17 @@ module Spree
store_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
+ []
+ end
end
end
diff --git a/core/spec/models/spree/gateway_spec.rb b/core/spec/models/spree/gateway_spec.rb
index 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 do
end
class TestGateway < Spree::Gateway
+ preference :publishable_preference1, :string
+ preference :publishable_preference2, :string
+ preference :private_preference, :string
+
def provider_class
Provider
end
+
+ private
+
+ def public_preference_keys
+ [:publishable_preference1, :publishable_preference2]
+ end
end
it 'passes through all arguments on a method_missing call' do
@@ -55,4 +65,16 @@ describe Spree::Gateway, type: :model do
rate = Spree::Gateway::FROM_DOLLAR_TO_CENT_RATE
expect(gateway.exchange_multiplier).to eq rate
end
+
+ 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'
+ })
+ end
end