-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for webhook registration and handling
Add basic support for registration and handling of Shopify webhook notifications, as a starter example. Key pieces: - `registerWebhook` function in *lib/shopify.js* registers a webhook via the admin API; creates a new registration or updates an existing one as appropriate - add endpoint */api/shopify/webhook/TOPIC/* that receives, verifies, and prints to console the content of webhook notifications - register the `app/uninstalled` webhook after initial auth, like the original example Shopify project Also includes an endpoint */api/shopify/register-webhooks* you can hit manually from within the Shopify Admin [1] to register webhooks, as a convenience to add or update webhooks since the after-auth hook is rarely triggered (unlike in the original Shopify example) [1]: https://MYSTORE.myshopify.com/admin/apps/MYAPP/api/shopify/register-webhooks
- Loading branch information
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { authenticateShopifyAPI } from "@bluebeela/nextjs-shopify-auth"; | ||
import { registerWebhook } from "../../../lib/shopify"; | ||
|
||
export default authenticateShopifyAPI(async function handler(req, res) { | ||
const { shopOrigin, shopifyToken } = req; | ||
const callbackUrlPathPrefix = "/api/shopify/webhook"; | ||
|
||
await registerWebhook( | ||
shopOrigin, | ||
shopifyToken, | ||
"app/uninstalled", | ||
callbackUrlPathPrefix | ||
); | ||
|
||
await registerWebhook( | ||
shopOrigin, | ||
shopifyToken, | ||
"carts/create", | ||
callbackUrlPathPrefix | ||
); | ||
|
||
await registerWebhook( | ||
shopOrigin, | ||
shopifyToken, | ||
"carts/update", | ||
callbackUrlPathPrefix | ||
); | ||
|
||
await registerWebhook( | ||
shopOrigin, | ||
shopifyToken, | ||
"checkouts/create", | ||
callbackUrlPathPrefix | ||
); | ||
|
||
await registerWebhook( | ||
shopOrigin, | ||
shopifyToken, | ||
"checkouts/update", | ||
callbackUrlPathPrefix | ||
); | ||
|
||
await registerWebhook( | ||
shopOrigin, | ||
shopifyToken, | ||
"orders/paid", | ||
callbackUrlPathPrefix | ||
); | ||
|
||
res.status(200).end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import crypto from "crypto"; | ||
|
||
const isVerifiedWebhookRequest = (req) => { | ||
const expectedHash = req.headers["x-shopify-hmac-sha256"]; | ||
const actualHash = crypto | ||
.createHmac("sha256", process.env.SHOPIFY_API_SECRET) | ||
.update(JSON.stringify(req.body)) | ||
.digest("base64"); | ||
|
||
return expectedHash === actualHash; | ||
}; | ||
|
||
export default async function webhooks(req, res) { | ||
const { query } = req; | ||
const topic = query.topic.join("/"); | ||
|
||
if (!isVerifiedWebhookRequest(req)) { | ||
console.log(`Ignoring UNVERIFIED Shopify webhook ${topic}`); | ||
res.status(400).end(); | ||
return; | ||
} | ||
|
||
console.log(`Received Shopify webhook ${topic}`, req.body); | ||
res.status(200).end(); | ||
} |