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
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.rbindex 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 Spreehas_many :store_credits, class_name: 'Spree::StoreCredit', foreign_key: :user_id, dependent: :destroyhas_many :wishlists, class_name: 'Spree::Wishlist', foreign_key: :user_id, dependent: :destroyhas_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: truebelongs_to :bill_address, class_name: 'Spree::Address', optional: truediff --git a/core/app/models/spree/credit_card.rb b/core/app/models/spree/credit_card.rbindex f90ebd2ac9..a4c15f9d32 100644--- a/core/app/models/spree/credit_card.rb+++ b/core/app/models/spree/credit_card.rb@@ -12,8 +12,9 @@ module Spreeacts_as_paranoidbelongs_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: :sourcebefore_save :set_last_digitsdiff --git a/core/app/models/spree/gateway_customer.rb b/core/app/models/spree/gateway_customer.rbnew file mode 100644index 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+enddiff --git a/core/app/models/spree/payment_method.rb b/core/app/models/spree/payment_method.rbindex 4447a4780f..26af68d238 100644--- a/core/app/models/spree/payment_method.rb+++ b/core/app/models/spree/payment_method.rb@@ -26,6 +26,8 @@ module Spreehas_many :credit_cards, class_name: 'Spree::CreditCard'end+ has_many :gateway_customers, class_name: 'Spree::GatewayCustomer'+def self.providersRails.application.config.spree.payment_methodsenddiff --git a/core/db/migrate/20250311105934_create_spree_gateway_customers.rb b/core/db/migrate/20250311105934_create_spree_gateway_customers.rbnew file mode 100644index 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+enddiff --git a/core/lib/spree/testing_support/factories/gateway_customer_factory.rb b/core/lib/spree/testing_support/factories/gateway_customer_factory.rbnew file mode 100644index 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