Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(shopify): release a claimed webhook delivery when its side effect fails so retries re-run it

nirholas023a991d30a5
diff --git a/api/_lib/shopify/store.js b/api/_lib/shopify/store.js
index 1b8efe9fa..9360bdf74 100644
--- a/api/_lib/shopify/store.js
+++ b/api/_lib/shopify/store.js
@@ -44,6 +44,12 @@ export async function claimWebhookDelivery(shop, topic, deliveryId) {
return rows.length > 0;
}
+// Drops a claim so Shopify's retry of a delivery whose side effect failed runs
+// the handler again instead of being acknowledged as a duplicate.
+export async function releaseWebhookDelivery(topic, deliveryId) {
+ await sql`delete from shopify_webhook_deliveries where topic = ${topic} and delivery_id = ${deliveryId}`;
+}
+
export async function createMediaJob({ shop, productId, productTitle, imageUrl, forgeJobId }) {
const rows = await sql`
insert into shopify_media_jobs (shop_domain, product_id, product_title, image_url, forge_job_id, status)
diff --git a/api/_lib/shopify/webhook-verify.js b/api/_lib/shopify/webhook-verify.js
index 90ccccf0a..feb14124d 100644
--- a/api/_lib/shopify/webhook-verify.js
+++ b/api/_lib/shopify/webhook-verify.js
@@ -8,12 +8,12 @@
import { readBody } from '../http.js';
import { verifyWebhookHmac } from './hmac.js';
import { apiSecret } from './config.js';
-import { claimWebhookDelivery } from './store.js';
+import { claimWebhookDelivery, releaseWebhookDelivery } from './store.js';
export class WebhookAuthError extends Error {}
/**
- * @returns {Promise<{ shop: string, body: object, fresh: boolean }>}
+ * @returns {Promise<{ shop: string, body: object, fresh: boolean, topic: string, deliveryId: string }>}
*/
export async function verifyAndClaim(req, topic) {
const raw = await readBody(req, 1_000_000);
@@ -30,5 +30,20 @@ export async function verifyAndClaim(req, topic) {
} catch {
body = {};
}
- return { shop, body, fresh };
+ return { shop, body, fresh, topic, deliveryId };
+}
+
+/**
+ * Runs `effect` only for a freshly claimed delivery. If it throws, the claim is
+ * released before rethrowing, so the 5xx makes Shopify's retry run the effect
+ * again rather than hit the ledger and skip a GDPR erasure or uninstall.
+ */
+export async function applyClaimed(claim, effect) {
+ if (!claim.fresh) return;
+ try {
+ await effect();
+ } catch (err) {
+ if (claim.deliveryId) await releaseWebhookDelivery(claim.topic, claim.deliveryId).catch(() => {});
+ throw err;
+ }
}
diff --git a/api/shopify/webhooks/app-uninstalled.js b/api/shopify/webhooks/app-uninstalled.js
index b13ea6fa8..f4ac8fc16 100644
--- a/api/shopify/webhooks/app-uninstalled.js
+++ b/api/shopify/webhooks/app-uninstalled.js
@@ -6,21 +6,22 @@
// next install starts clean.
import { cors, method, wrap, error, json } from '../../_lib/http.js';
-import { verifyAndClaim, WebhookAuthError } from '../../_lib/shopify/webhook-verify.js';
+import { verifyAndClaim, applyClaimed, WebhookAuthError } from '../../_lib/shopify/webhook-verify.js';
import { markShopUninstalled } from '../../_lib/shopify/store.js';
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'POST,OPTIONS' })) return;
if (!method(req, res, ['POST'])) return;
- let shop, fresh;
+ let shop, claim;
try {
- ({ shop, fresh } = await verifyAndClaim(req, 'app/uninstalled'));
+ claim = await verifyAndClaim(req, 'app/uninstalled');
+ shop = claim.shop;
} catch (err) {
if (err instanceof WebhookAuthError) return error(res, 401, 'invalid_hmac', 'webhook HMAC verification failed');
throw err;
}
- if (fresh) await markShopUninstalled(shop);
+ await applyClaimed(claim, () => markShopUninstalled(shop));
return json(res, 200, { ok: true });
});
diff --git a/api/shopify/webhooks/shop-redact.js b/api/shopify/webhooks/shop-redact.js
index ea2239a47..398aac58a 100644
--- a/api/shopify/webhooks/shop-redact.js
+++ b/api/shopify/webhooks/shop-redact.js
@@ -6,21 +6,22 @@
// billing records, the OAuth session row).
import { cors, method, wrap, error, json } from '../../_lib/http.js';
-import { verifyAndClaim, WebhookAuthError } from '../../_lib/shopify/webhook-verify.js';
+import { verifyAndClaim, applyClaimed, WebhookAuthError } from '../../_lib/shopify/webhook-verify.js';
import { eraseShopData } from '../../_lib/shopify/store.js';
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'POST,OPTIONS' })) return;
if (!method(req, res, ['POST'])) return;
- let shop, fresh;
+ let shop, claim;
try {
- ({ shop, fresh } = await verifyAndClaim(req, 'shop/redact'));
+ claim = await verifyAndClaim(req, 'shop/redact');
+ shop = claim.shop;
} catch (err) {
if (err instanceof WebhookAuthError) return error(res, 401, 'invalid_hmac', 'webhook HMAC verification failed');
throw err;
}
- if (fresh) await eraseShopData(shop);
+ await applyClaimed(claim, () => eraseShopData(shop));
return json(res, 200, { ok: true });
});
diff --git a/tests/shopify-webhooks.test.js b/tests/shopify-webhooks.test.js
index d5a932078..9b869eab9 100644
--- a/tests/shopify-webhooks.test.js
+++ b/tests/shopify-webhooks.test.js
@@ -25,10 +25,10 @@ const DDL = `
);
`;
-let verifyAndClaim, WebhookAuthError;
+let verifyAndClaim, applyClaimed, WebhookAuthError;
beforeAll(async () => {
- ({ verifyAndClaim, WebhookAuthError } = await import('../api/_lib/shopify/webhook-verify.js'));
+ ({ verifyAndClaim, applyClaimed, WebhookAuthError } = await import('../api/_lib/shopify/webhook-verify.js'));
});
beforeEach(async () => {
@@ -71,3 +71,21 @@ describe('verifyAndClaim', () => {
await expect(verifyAndClaim(req, 'APP_UNINSTALLED')).rejects.toBeInstanceOf(WebhookAuthError);
});
});
+
+describe('applyClaimed', () => {
+ it('releases the claim when the effect throws so Shopify\'s retry re-runs it', async () => {
+ const first = await verifyAndClaim(fakeReq({ a: 1 }, { deliveryId: 'retry-1' }), 'shop/redact');
+ await expect(applyClaimed(first, async () => { throw new Error('db down'); })).rejects.toThrow('db down');
+
+ const retry = await verifyAndClaim(fakeReq({ a: 1 }, { deliveryId: 'retry-1' }), 'shop/redact');
+ expect(retry.fresh).toBe(true);
+ let ran = 0;
+ await applyClaimed(retry, async () => { ran++; });
+ expect(ran).toBe(1);
+
+ const dup = await verifyAndClaim(fakeReq({ a: 1 }, { deliveryId: 'retry-1' }), 'shop/redact');
+ expect(dup.fresh).toBe(false);
+ await applyClaimed(dup, async () => { ran++; });
+ expect(ran).toBe(1);
+ });
+});