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.

Pick the most recently linked webhook key when migrating

Damian Legawiecd1bd0a06c1e0
diff --git a/spree/providers/stripe/lib/tasks/migrate_webhook_keys.rake b/spree/providers/stripe/lib/tasks/migrate_webhook_keys.rake
index 952169c0e6..2a326bd2b3 100644
--- a/spree/providers/stripe/lib/tasks/migrate_webhook_keys.rake
+++ b/spree/providers/stripe/lib/tasks/migrate_webhook_keys.rake
@@ -61,13 +61,17 @@ module SpreeStripe
ActiveRecord::Base.connection
end
- # A payment method could be joined to several keys historically; the most
- # recent one is the endpoint Stripe would be signing with today.
+ # A payment method could be joined to several keys historically. The most
+ # recently *linked* one is the endpoint Stripe signs with today — which is
+ # not always the newest key, since an older key could be linked later.
def legacy_key_for(gateway)
- key_ids = join_model.where(payment_method_id: gateway.id).order(:created_at).pluck(:webhook_key_id)
- return if key_ids.empty?
+ join = join_model.
+ where(payment_method_id: gateway.id).
+ order(created_at: :desc, id: :desc).
+ first
+ return if join.nil?
- key_model.where(id: key_ids).order(:created_at).last
+ key_model.find_by(id: join.webhook_key_id)
end
def key_model