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.

Merge pull request #12504 from spree/feature/add-gateway-customer

Łukasz Adamczykac64f813e264
diff --git a/core/app/models/concerns/spree/user_methods.rb b/core/app/models/concerns/spree/user_methods.rb
index b42ce9dd58..b23d0c3cd6 100644
--- a/core/app/models/concerns/spree/user_methods.rb
+++ b/core/app/models/concerns/spree/user_methods.rb
@@ -30,6 +30,7 @@ module Spree
has_many :store_credits, class_name: 'Spree::StoreCredit', foreign_key: :user_id, dependent: :destroy
has_many :wishlists, class_name: 'Spree::Wishlist', foreign_key: :user_id, dependent: :destroy
has_many :wished_items, through: :wishlists, source: :wished_items
+ has_many :gateway_customers, class_name: 'Spree::GatewayCustomer'
belongs_to :ship_address, class_name: 'Spree::Address', optional: true
belongs_to :bill_address, class_name: 'Spree::Address', optional: true
diff --git a/core/app/models/spree/credit_card.rb b/core/app/models/spree/credit_card.rb
index f90ebd2ac9..a4c15f9d32 100644
--- a/core/app/models/spree/credit_card.rb
+++ b/core/app/models/spree/credit_card.rb
@@ -12,8 +12,9 @@ module Spree
acts_as_paranoid
belongs_to :payment_method
- belongs_to :user, class_name: "::#{Spree.user_class}", foreign_key: 'user_id',
- optional: true
+ belongs_to :user, class_name: "::#{Spree.user_class}", foreign_key: 'user_id', optional: true
+ belongs_to :gateway_customer, class_name: 'Spree::GatewayCustomer', optional: true
+
has_many :payments, as: :source
before_save :set_last_digits
diff --git a/core/app/models/spree/gateway_customer.rb b/core/app/models/spree/gateway_customer.rb
new file mode 100644
index 0000000000..5c42f23f80
--- /dev/null
+++ b/core/app/models/spree/gateway_customer.rb
@@ -0,0 +1,13 @@
+module Spree
+ class GatewayCustomer < Spree.base_class
+ belongs_to :payment_method, class_name: 'Spree::PaymentMethod'
+ belongs_to :user, class_name: Spree.user_class.to_s
+
+ validates :payment_method, presence: true
+ validates :user, presence: true
+ validates :profile_id, presence: true
+ validates :payment_method_id, uniqueness: { scope: :user_id }
+
+ encrypts :profile_id, deterministic: true if Rails.configuration.active_record.encryption.include?(:primary_key)
+ end
+end
diff --git a/core/app/models/spree/payment_method.rb b/core/app/models/spree/payment_method.rb
index 4447a4780f..26af68d238 100644
--- a/core/app/models/spree/payment_method.rb
+++ b/core/app/models/spree/payment_method.rb
@@ -26,6 +26,8 @@ module Spree
has_many :credit_cards, class_name: 'Spree::CreditCard'
end
+ has_many :gateway_customers, class_name: 'Spree::GatewayCustomer'
+
def self.providers
Rails.application.config.spree.payment_methods
end
diff --git a/core/db/migrate/20250311105934_create_spree_gateway_customers.rb b/core/db/migrate/20250311105934_create_spree_gateway_customers.rb
new file mode 100644
index 0000000000..6ee5b23cdf
--- /dev/null
+++ b/core/db/migrate/20250311105934_create_spree_gateway_customers.rb
@@ -0,0 +1,18 @@
+class CreateSpreeGatewayCustomers < ActiveRecord::Migration[7.2]
+ def change
+ create_table :spree_gateway_customers, if_not_exists: true do |t|
+ t.string :profile_id, null: false
+ t.bigint :payment_method_id, null: false
+ t.bigint :user_id, null: false
+
+ t.timestamps
+
+ t.index ['payment_method_id'], name: 'index_spree_gateway_customers_on_payment_method_id'
+ t.index ['user_id', 'payment_method_id'], name: 'index_spree_gateway_customers_on_user_id_and_payment_method_id', unique: true
+ t.index ['user_id'], name: 'index_spree_gateway_customers_on_user_id'
+ end
+
+ add_column :spree_credit_cards, :gateway_customer_id, if_not_exists: true
+ add_index :spree_credit_cards, :gateway_customer_id, if_not_exists: true
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/gateway_customer_factory.rb b/core/lib/spree/testing_support/factories/gateway_customer_factory.rb
new file mode 100644
index 0000000000..d8415a6cd7
--- /dev/null
+++ b/core/lib/spree/testing_support/factories/gateway_customer_factory.rb
@@ -0,0 +1,7 @@
+FactoryBot.define do
+ factory :gateway_customer, class: Spree::GatewayCustomer do
+ sequence(:profile_id) { |n| "cus_#{n}" }
+ user { |p| p.association(:user) }
+ payment_method { |p| p.association(:payment_method) }
+ end
+end