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
Expand Spree::PaymentSource to work with gateway customer profiles (#12816)
Damian Legawieca031a2fe5c0b
diff --git a/core/app/models/concerns/spree/payment_source_concern.rb b/core/app/models/concerns/spree/payment_source_concern.rbnew file mode 100644index 0000000000..c67147d1af--- /dev/null+++ b/core/app/models/concerns/spree/payment_source_concern.rb@@ -0,0 +1,39 @@+module Spree+ module PaymentSourceConcern+ extend ActiveSupport::Concern++ # Available actions for the payment source.+ # @return [Array<String>]+ def actions+ %w{capture void credit}+ end++ # Indicates whether its possible to capture the payment+ # @param payment [Spree::Payment]+ # @return [Boolean]+ def can_capture?(payment)+ payment.pending? || payment.checkout?+ end++ # Indicates whether its possible to void the payment.+ # @param payment [Spree::Payment]+ # @return [Boolean]+ def can_void?(payment)+ !payment.failed? && !payment.void?+ end++ # Indicates whether its possible to credit the payment. Note that most gateways require that the+ # payment be settled first which generally happens within 12-24 hours of the transaction.+ # @param payment [Spree::Payment]+ # @return [Boolean]+ def can_credit?(payment)+ payment.completed? && payment.credit_allowed > 0+ end++ # Returns true if the payment source has a payment profile.+ # @return [Boolean]+ def has_payment_profile?+ gateway_customer_profile_id.present? || gateway_payment_profile_id.present?+ end+ end+enddiff --git a/core/app/models/spree/credit_card.rb b/core/app/models/spree/credit_card.rbindex 107cc48af5..f58250eb86 100644--- a/core/app/models/spree/credit_card.rb+++ b/core/app/models/spree/credit_card.rb@@ -2,6 +2,8 @@ module Spreeclass CreditCard < Spree.base_classinclude ActiveMerchant::Billing::CreditCardMethodsinclude Spree::Metadata+ include Spree::PaymentSourceConcern+if defined?(Spree::Webhooks::HasWebhooks)include Spree::Webhooks::HasWebhooksend@@ -135,30 +137,6 @@ module Spreebrand.present? ? brand.upcase : Spree.t(:no_cc_type)end- def actions- %w{capture void credit}- end-- # Indicates whether its possible to capture the payment- def can_capture?(payment)- payment.pending? || payment.checkout?- end-- # Indicates whether its possible to void the payment.- def can_void?(payment)- !payment.failed? && !payment.void?- end-- # Indicates whether its possible to credit the payment. Note that most gateways require that the- # payment be settled first which generally happens within 12-24 hours of the transaction.- def can_credit?(payment)- payment.completed? && payment.credit_allowed > 0- end-- def has_payment_profile?- gateway_customer_profile_id.present? || gateway_payment_profile_id.present?- end-# ActiveMerchant needs first_name/last_name because we pass it a Spree::CreditCard and it calls those methods on it.# Looking at the ActiveMerchant source code we should probably be calling #to_active_merchant before passing# the object to ActiveMerchant but this should do for now.diff --git a/core/app/models/spree/gateway/custom_payment_source_method.rb b/core/app/models/spree/gateway/custom_payment_source_method.rbnew file mode 100644index 0000000000..53d4bab8cf--- /dev/null+++ b/core/app/models/spree/gateway/custom_payment_source_method.rb@@ -0,0 +1,33 @@+module Spree+ class Gateway::CustomPaymentSourceMethod < Gateway+ def provider_class+ self.class+ end++ def payment_source_class+ Spree::PaymentSource+ end++ def payment_profiles_supported?+ true+ end++ def show_in_admin?+ false+ end++ def create_profile(payment)+ return if payment.source.gateway_customer.present?++ user = payment.source.user || payment.order.user+ return if user.blank?++ find_or_create_customer(user)+ end++ # simulate a 3rd party payment gateway api to fetch/or create a customer+ def find_or_create_customer(user)+ gateway_customers.find_or_create_by!(user: user, profile_id: "CUSTOMER-#{user.id}")+ end+ end+enddiff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rbindex be5f8be6c5..db5db0373d 100644--- a/core/app/models/spree/payment.rb+++ b/core/app/models/spree/payment.rb@@ -302,7 +302,8 @@ module Spree# Payment profile cannot be created without sourcereturn unless source# Imported payments shouldn't create a payment profile.- return if source.imported+ # Imported is only available on Spree::CreditCard, non-credit card payments should not have this attribute.+ return if source.respond_to?(:imported) && source.importedpayment_method.create_profile(self)rescue ActiveMerchant::ConnectionError => ediff --git a/core/app/models/spree/payment_source.rb b/core/app/models/spree/payment_source.rbindex 57cb81b9bb..30ff156805 100644--- a/core/app/models/spree/payment_source.rb+++ b/core/app/models/spree/payment_source.rb@@ -1,10 +1,37 @@+# This model is used to store payment sources for non-credit card payments, eg wallet, account, etc.module Spreeclass PaymentSource < Spree.base_classinclude Spree::Metadata+ include Spree::PaymentSourceConcern+ #+ # Associations+ #belongs_to :payment_method, class_name: 'Spree::PaymentMethod'belongs_to :user, class_name: Spree.user_class.to_s, optional: true+ #+ # Validations+ #validates_uniqueness_of :gateway_payment_profile_id, scope: :type++ #+ # Delegations+ #+ delegate :profile_id, to: :gateway_customer, prefix: true, allow_nil: true++ # Returns true if the payment source has a payment profile.+ # @return [Boolean]+ def has_payment_profile?+ gateway_customer&.profile_id&.present? || gateway_payment_profile_id.present?+ end++ # Returns the gateway customer for the user.+ # @return [Spree::GatewayCustomer]+ def gateway_customer+ return if user.blank?++ payment_method.gateway_customers.find_by(user: user)+ endendenddiff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rbindex 9c0781e227..c288fdf1e2 100644--- a/core/lib/spree/core/engine.rb+++ b/core/lib/spree/core/engine.rb@@ -98,6 +98,7 @@ module SpreeRails.application.config.spree.payment_methods = [Spree::Gateway::Bogus,Spree::Gateway::BogusSimple,+ Spree::Gateway::CustomPaymentSourceMethod,Spree::PaymentMethod::Check,Spree::PaymentMethod::StoreCredit]diff --git a/core/lib/spree/testing_support/factories/payment_factory.rb b/core/lib/spree/testing_support/factories/payment_factory.rbindex baed9bcdac..e324f0a0dd 100644--- a/core/lib/spree/testing_support/factories/payment_factory.rb+++ b/core/lib/spree/testing_support/factories/payment_factory.rb@@ -14,6 +14,11 @@ FactoryBot.define docreate(:refund, amount: 5, payment: payment)endend++ factory :custom_payment, class: Spree::Payment do+ payment_method { create(:custom_payment_method, stores: [order.store]) }+ source { create(:payment_source, user: order.user, payment_method: payment_method) }+ endendfactory :check_payment, class: Spree::Payment dodiff --git a/core/lib/spree/testing_support/factories/payment_method_factory.rb b/core/lib/spree/testing_support/factories/payment_method_factory.rbindex 69464b67da..1cb986083d 100644--- a/core/lib/spree/testing_support/factories/payment_method_factory.rb+++ b/core/lib/spree/testing_support/factories/payment_method_factory.rb@@ -37,4 +37,9 @@ FactoryBot.define doactive { true }auto_capture { true }end++ factory :custom_payment_method, parent: :payment_method, class: Spree::Gateway::CustomPaymentSourceMethod do+ type { 'Spree::Gateway::CustomPaymentSourceMethod' }+ name { 'Custom' }+ endenddiff --git a/core/lib/spree/testing_support/factories/payment_source_factory.rb b/core/lib/spree/testing_support/factories/payment_source_factory.rbnew file mode 100644index 0000000000..fe1c07cf06--- /dev/null+++ b/core/lib/spree/testing_support/factories/payment_source_factory.rb@@ -0,0 +1,5 @@+FactoryBot.define do+ factory :payment_source, class: Spree::PaymentSource do+ association(:payment_method, factory: :custom_payment_method)+ end+enddiff --git a/core/spec/models/spree/payment_source_spec.rb b/core/spec/models/spree/payment_source_spec.rbnew file mode 100644index 0000000000..4580debde2--- /dev/null+++ b/core/spec/models/spree/payment_source_spec.rb@@ -0,0 +1,26 @@+require 'spec_helper'++RSpec.describe Spree::PaymentSource, type: :model do+ let(:store) { @default_store }+ let(:payment_method) { create(:custom_payment_method, stores: [store]) }+ let(:payment_source) { create(:payment_source, user: user, payment_method: payment_method) }+ let(:user) { create(:user) }++ describe '#gateway_customer' do+ let!(:gateway_customer) { create(:gateway_customer, payment_method: payment_method, user: user) }++ context 'when user is present' do+ it 'returns the gateway customer for the user' do+ expect(payment_source.gateway_customer).to eq(gateway_customer)+ end+ end++ context 'when user is not present' do+ let(:payment_source) { create(:payment_source, user: nil, payment_method: payment_method) }++ it 'returns nil' do+ expect(payment_source.gateway_customer).to be_nil+ end+ end+ end+enddiff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rbindex 6324ad2ab8..43543524eb 100644--- a/core/spec/models/spree/payment_spec.rb+++ b/core/spec/models/spree/payment_spec.rb@@ -221,6 +221,31 @@ describe Spree::Payment, type: :model doendendend++ describe '#create_payment_profile' do+ context 'when payment method supports profiles' do+ context 'when source is a credit card' do+ let(:source) { create(:credit_card) }+ let(:payment) { build(:payment, source: source) }++ it 'creates a payment profile' do+ expect { payment.save! }.to change { source.gateway_customer_profile_id }.from(nil).to(/BGS-/)+ end+ end++ context 'when source is not a credit card' do+ let(:user) { create(:user) }+ let(:order) { create(:order, user: user, total: 100) }+ let(:gateway) { create(:custom_payment_method) }+ let(:source) { create(:payment_source, user: user, payment_method: gateway) }+ let(:payment) { build(:custom_payment, source: source, amount: 100, payment_method: gateway) }++ it 'creates a payment profile' do+ expect { payment.save! }.to change { source.gateway_customer_profile_id }.from(nil).to("CUSTOMER-#{user.id}")+ end+ end+ end+ endend# Regression test for https://github.com/spree/spree/pull/2224