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 Mar 22, 2023
1 parent 1f8db98 commit c02c8f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Unreleased

* Support Unified Admin [#1658](https://github.com/Shopify/shopify_app/pull/1658)
* Set `access_scopes` column to string by default [#1636](https://github.com/Shopify/shopify_app/pull/1636)
* Fix registration of event_bridge and pub_sub webhooks [#1635](https://github.com/Shopify/shopify_app/pull/1635)

21.4.1 (Feb 21, 2023)
----------
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 c02c8f9

Please sign in to comment.