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.

Fix: PayPal webhook registration failing for a webhook already subscribed

Łukasz Adamczyk52213387d50c
diff --git a/spree/providers/paypal/app/services/spree_paypal_checkout/register_webhook.rb b/spree/providers/paypal/app/services/spree_paypal_checkout/register_webhook.rb
index 10ed425075..188c5714f5 100644
--- a/spree/providers/paypal/app/services/spree_paypal_checkout/register_webhook.rb
+++ b/spree/providers/paypal/app/services/spree_paypal_checkout/register_webhook.rb
@@ -2,7 +2,8 @@ module SpreePaypalCheckout
# Finds or creates the PayPal webhook for a payment method's URL and stores
# its id on the method. A webhook found for the URL has its events replaced
# with the gem's: stores upgraded from 5.x created one by hand, subscribed to
- # other events.
+ # other events. One already subscribed to them is kept as it is, since
+ # PayPal refuses a change that changes nothing.
class RegisterWebhook
# @param payment_method [SpreePaypalCheckout::Gateway]
# @return [Hash, nil] the webhook, or nil when the method has no URL
@@ -14,10 +15,12 @@ module SpreePaypalCheckout
client = payment_method.notifications_client
existing = client.list_webhooks.find { |webhook| webhook['url'] == url }
- webhook = if existing
- client.replace_webhook_events(existing['id'], events)
- else
+ webhook = if existing.nil?
client.create_webhook(url: url, event_types: events)
+ elsif Array(existing['event_types']).map { |event| event['name'] }.sort == events.sort
+ existing
+ else
+ client.replace_webhook_events(existing['id'], events)
end
payment_method.update!(preferred_webhook_id: webhook['id']) if payment_method.preferred_webhook_id != webhook['id']
diff --git a/spree/providers/paypal/spec/services/spree_paypal_checkout/register_webhook_spec.rb b/spree/providers/paypal/spec/services/spree_paypal_checkout/register_webhook_spec.rb
index ba88736962..4118a1433e 100644
--- a/spree/providers/paypal/spec/services/spree_paypal_checkout/register_webhook_spec.rb
+++ b/spree/providers/paypal/spec/services/spree_paypal_checkout/register_webhook_spec.rb
@@ -25,8 +25,12 @@ RSpec.describe SpreePaypalCheckout::RegisterWebhook do
end
context 'when PayPal has a webhook for the URL' do
+ let(:subscribed) { ['CHECKOUT.ORDER.APPROVED'] }
+
before do
- allow(client).to receive(:list_webhooks).and_return([{ 'id' => 'WH-EXISTING', 'url' => gateway.webhook_url }])
+ allow(client).to receive(:list_webhooks).and_return(
+ [{ 'id' => 'WH-EXISTING', 'url' => gateway.webhook_url, 'event_types' => subscribed.map { |name| { 'name' => name } } }]
+ )
allow(client).to receive(:replace_webhook_events).and_return({ 'id' => 'WH-EXISTING' })
allow(client).to receive(:create_webhook)
end
@@ -38,6 +42,17 @@ RSpec.describe SpreePaypalCheckout::RegisterWebhook do
expect(client).not_to have_received(:create_webhook)
expect(gateway.reload.preferred_webhook_id).to eq('WH-EXISTING')
end
+
+ context "that is already subscribed to the gem's events" do
+ let(:subscribed) { events.reverse }
+
+ it 'keeps it without asking PayPal for a change and stores its id' do
+ expect(register['id']).to eq('WH-EXISTING')
+ expect(client).not_to have_received(:replace_webhook_events)
+ expect(client).not_to have_received(:create_webhook)
+ expect(gateway.reload.preferred_webhook_id).to eq('WH-EXISTING')
+ end
+ end
end
it 'does nothing without a webhook URL' do