Skip to content

Commit

Permalink
Fix registration of event_bridge and pub_sub webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillplatonov committed Jan 6, 2023
1 parent 97b6d21 commit 9040bf4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased
----------

* Fix registration of event_bridge and pub_sub webhooks [#1635](https://github.com/Shopify/shopify_app/pull/1635)

21.4.0 (Jan 5, 2023)
----------
* Updated shopify_api to 12.4.0 [#1633](https://github.com/Shopify/shopify_app/pull/1633)
Expand Down
36 changes: 35 additions & 1 deletion docs/shopify_app/webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
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.
5 changes: 3 additions & 2 deletions lib/shopify_app/managers/webhooks_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,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
Expand Down

0 comments on commit 9040bf4

Please sign in to comment.