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.

Replace the Stripe decorators with a subscriber

Damian Legawiecd8fcfb9ec84f
diff --git a/spree/providers/stripe/app/jobs/spree_stripe/update_customer_job.rb b/spree/providers/stripe/app/jobs/spree_stripe/update_customer_job.rb
deleted file mode 100644
index 02d1e47fef..0000000000
--- a/spree/providers/stripe/app/jobs/spree_stripe/update_customer_job.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-module SpreeStripe
- class UpdateCustomerJob < BaseJob
- def perform(customer_id)
- customer = Spree.customer_class.find_by(id: customer_id)
- return if customer.blank?
-
- SpreeStripe::UpdateCustomer.new.call(customer: customer)
- end
- end
-end
diff --git a/spree/providers/stripe/app/models/spree_stripe/customer_decorator.rb b/spree/providers/stripe/app/models/spree_stripe/customer_decorator.rb
deleted file mode 100644
index a726d11f28..0000000000
--- a/spree/providers/stripe/app/models/spree_stripe/customer_decorator.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-module SpreeStripe
- module CustomerDecorator
- def self.prepended(base)
- base.after_update :update_stripe_customer, if: :should_update_stripe_customer?
- end
-
- def update_stripe_customer
- SpreeStripe::UpdateCustomerJob.perform_later(id)
- end
-
- private
-
- def should_update_stripe_customer?
- ship_address_previously_changed? || bill_address_previously_changed? ||
- first_name_previously_changed? || last_name_previously_changed? ||
- email_previously_changed?
- end
- end
-end
-
-Spree.customer_class.prepend(SpreeStripe::CustomerDecorator) if Spree.customer_class.present?
diff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb
index 3564647993..47c1de90a9 100644
--- a/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb
+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb
@@ -30,6 +30,11 @@ module SpreeStripe
after_commit :create_webhook_endpoint_async, on: %i[create update]
end
+ # Translates a Stripe event into the normalized shape core's webhook
+ # controller and `Spree::Payments::HandleWebhook` consume. Everything
+ # after this — idempotency, locking, payment creation, order completion —
+ # belongs to core.
+ #
# @param raw_body [String]
# @param headers [Hash]
# @return [Hash, nil] nil for events this gateway does not act on
@@ -46,7 +51,7 @@ module SpreeStripe
)
return nil unless payment_session
- { action: action, payment_session: payment_session, metadata: { stripe_event: event } }
+ { action: action, payment_session: payment_session }
end
def create_webhook_endpoint
diff --git a/spree/providers/stripe/app/models/spree_stripe/store_decorator.rb b/spree/providers/stripe/app/models/spree_stripe/store_decorator.rb
deleted file mode 100644
index 05ecb19815..0000000000
--- a/spree/providers/stripe/app/models/spree_stripe/store_decorator.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-module SpreeStripe
- module StoreDecorator
- # @return [SpreeStripe::Gateway, nil]
- def stripe_gateway
- @stripe_gateway ||= payment_methods.stripe.active.last
- end
- end
-end
-
-Spree::Store.prepend(SpreeStripe::StoreDecorator)
diff --git a/spree/providers/stripe/app/services/spree_stripe/create_payment.rb b/spree/providers/stripe/app/services/spree_stripe/create_payment.rb
index b58fef8cd3..534d381dac 100644
--- a/spree/providers/stripe/app/services/spree_stripe/create_payment.rb
+++ b/spree/providers/stripe/app/services/spree_stripe/create_payment.rb
@@ -4,10 +4,10 @@ module SpreeStripe
class CreatePayment
# @param owner [Spree::Cart, Spree::Order]
# @param payment_session [Spree::PaymentSessions::Stripe]
- # @param gateway [SpreeStripe::Gateway]
- def initialize(owner:, payment_session:, gateway: nil, amount: nil)
+ # @param gateway [SpreeStripe::Gateway] the session's payment method
+ def initialize(owner:, payment_session:, gateway:, amount: nil)
@owner = owner
- @gateway = gateway || owner.store.stripe_gateway
+ @gateway = gateway
@payment_session = payment_session
@amount = amount || owner.total_minus_store_credits
end
diff --git a/spree/providers/stripe/app/subscribers/spree_stripe/customer_updated_subscriber.rb b/spree/providers/stripe/app/subscribers/spree_stripe/customer_updated_subscriber.rb
new file mode 100644
index 0000000000..69eaa5d580
--- /dev/null
+++ b/spree/providers/stripe/app/subscribers/spree_stripe/customer_updated_subscriber.rb
@@ -0,0 +1,18 @@
+module SpreeStripe
+ # Keeps the Stripe customer in sync with the Spree one.
+ #
+ # Lifecycle events carry no changed-attribute information, so this fires on
+ # every customer update rather than only on name, email or address changes.
+ # UpdateCustomer returns immediately for a customer with no Stripe customers,
+ # which is the common case and costs one indexed query.
+ class CustomerUpdatedSubscriber < Spree::Subscriber
+ subscribes_to 'user.updated'
+
+ def handle(event)
+ customer = Spree.customer_class.find_by_prefix_id(event.payload['id'])
+ return if customer.blank?
+
+ SpreeStripe::UpdateCustomer.new.call(customer: customer)
+ end
+ end
+end
diff --git a/spree/providers/stripe/spec/jobs/spree_stripe/update_customer_job_spec.rb b/spree/providers/stripe/spec/jobs/spree_stripe/update_customer_job_spec.rb
deleted file mode 100644
index aa72306485..0000000000
--- a/spree/providers/stripe/spec/jobs/spree_stripe/update_customer_job_spec.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe SpreeStripe::UpdateCustomerJob do
- subject { described_class.new.perform(customer_id) }
-
- let(:customer) { create(:customer) }
- let(:customer_id) { customer.id }
-
- it 'calls the SpreeStripe::UpdateCustomer service' do
- expect(SpreeStripe::UpdateCustomer).to receive_message_chain(:new, :call).with(customer: customer)
- subject
- end
-
- context 'when the customer is not found' do
- let(:customer_id) { 'missing' }
-
- it 'does nothing' do
- expect(SpreeStripe::UpdateCustomer).not_to receive(:new)
- subject
- end
- end
-end
diff --git a/spree/providers/stripe/spec/subscribers/spree_stripe/customer_updated_subscriber_spec.rb b/spree/providers/stripe/spec/subscribers/spree_stripe/customer_updated_subscriber_spec.rb
new file mode 100644
index 0000000000..dbf01fe317
--- /dev/null
+++ b/spree/providers/stripe/spec/subscribers/spree_stripe/customer_updated_subscriber_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+RSpec.describe SpreeStripe::CustomerUpdatedSubscriber do
+ subject(:handle) { described_class.new.handle(event) }
+
+ let(:store) { @default_store }
+ let(:customer) { create(:customer) }
+ let(:event) { instance_double(Spree::Event, payload: { 'id' => customer.prefixed_id }) }
+
+ context 'when the customer has a Stripe customer' do
+ let(:gateway) { create(:stripe_gateway, store: store) }
+ let!(:gateway_customer) do
+ create(:gateway_customer, customer: customer, payment_method: gateway, profile_id: 'cus_123')
+ end
+
+ it 'pushes the change to Stripe' do
+ expect_any_instance_of(SpreeStripe::Gateway).to receive(:update_customer).with(customer: customer)
+
+ handle
+ end
+ end
+
+ context 'when the customer has no Stripe customer' do
+ it 'does not call Stripe' do
+ expect_any_instance_of(SpreeStripe::Gateway).not_to receive(:update_customer)
+
+ handle
+ end
+ end
+
+ context 'when the customer no longer exists' do
+ let(:event) { instance_double(Spree::Event, payload: { 'id' => 'user_nonexistent' }) }
+
+ it 'does nothing' do
+ expect { handle }.not_to raise_error
+ end
+ end
+end