diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d3c0e50c..f6bfdcb72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Unreleased ---------- * Fixes typo in webhook generator [#1704](https://github.com/Shopify/shopify_app/pull/1704) +* Fix registration of event_bridge and pub_sub webhooks [#1635](https://github.com/Shopify/shopify_app/pull/1635) 21.6.0 (July 11, 2023) ---------- diff --git a/docs/shopify_app/webhooks.md b/docs/shopify_app/webhooks.md index ae5723bcd..f7e9c3d20 100644 --- a/docs/shopify_app/webhooks.md +++ b/docs/shopify_app/webhooks.md @@ -82,4 +82,38 @@ We have three mandatory GDPR webhooks The `generate shopify_app` command generated three job templates corresponding to all three of these webhooks. To pass our approval process you will need to set these webhooks in your partner dashboard. -You can read more about that [here](https://shopify.dev/apps/webhooks/configuration/mandatory-webhooks). \ No newline at end of file +You can read more about that [here](https://shopify.dev/apps/webhooks/configuration/mandatory-webhooks). + +## EventBridge and PubSub Webhooks + +You can also register webhooks for delivery to Amazon EventBridge or Google Cloud Pub/Sub. In this case the `path` argument to needs to be of a specific form. + +For EventBridge, the `path` must be the ARN of the partner event source. + +```rb +ShopifyApp.configure do |config| + config.webhooks = [ + { + delivery_method: :event_bridge, + topic: 'carts/update', + path: 'arn:aws:events....' + } + ] +end +``` + +For Pub/Sub, the `path` must be of the form `pubsub://[PROJECT-ID]:[PUB-SUB-TOPIC-ID]`. For example, if you created a topic with id `red` in the project `blue`, then the value of path would be `pubsub://blue:red`. + +```rb +ShopifyApp.configure do |config| + config.webhooks = [ + { + delivery_method: :pub_sub, + topic: 'carts/update', + path: 'pubsub://project-id:pub-sub-topic-id' + } + ] +end +``` + +When registering for an EventBridge or PubSub Webhook you'll need to implement a handler that will fetch webhooks from the queue and process them yourself. diff --git a/lib/shopify_app/managers/webhooks_manager.rb b/lib/shopify_app/managers/webhooks_manager.rb index 09acc85fc..aa738e356 100644 --- a/lib/shopify_app/managers/webhooks_manager.rb +++ b/lib/shopify_app/managers/webhooks_manager.rb @@ -43,12 +43,13 @@ def add_registrations ShopifyApp::Logger.debug("Adding registrations to webhooks") ShopifyApp.configuration.webhooks.each do |attributes| webhook_path = path(attributes) + delivery_method = attributes[:delivery_method] || :http ShopifyAPI::Webhooks::Registry.add_registration( topic: attributes[:topic], - delivery_method: attributes[:delivery_method] || :http, + delivery_method: delivery_method, path: webhook_path, - handler: webhook_job_klass(webhook_path), + handler: delivery_method == :http ? webhook_job_klass(webhook_path) : nil, fields: attributes[:fields], ) end