Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/provider-neutral-dedup-log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@dashcommerce/core": patch
---

The log line for a skipped duplicate webhook now reads "Webhook deduped" and names the provider, instead of "Stripe webhook deduped" for every provider. Its fields are now `providerId`, `eventId` (was `stripeEventId`) and `type`.
10 changes: 8 additions & 2 deletions packages/core/src/routes/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ async function recordStripeEvent(
ctx: PluginContext,
stripeEventId: string,
type: string,
providerId: string,
): Promise<string | null> {
try {
const id = randomId();
Expand All @@ -101,7 +102,7 @@ async function recordStripeEvent(
return id;
} catch (err) {
if (isUniqueViolation(err)) {
ctx.log.info("Stripe webhook deduped", { stripeEventId, type });
ctx.log.info("Webhook deduped", { providerId, eventId: stripeEventId, type });
return null;
}
throw err;
Expand Down Expand Up @@ -879,7 +880,12 @@ export const webhookRoutes = {
// the same resource. `providerEventId` is Stripe `event.id`.
let dedupRowId: string | null = null;
if (providerEventId) {
dedupRowId = await recordStripeEvent(ctx, providerEventId, eventTypeForDedup);
dedupRowId = await recordStripeEvent(
ctx,
providerEventId,
eventTypeForDedup,
provider.id,
);
if (!dedupRowId) {
return new Response(JSON.stringify({ received: true, duplicate: true }), {
status: 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,22 @@ describe("webhook retry after a failed confirmation", () => {

it("still skips an exact repeat once the event was processed", async () => {
registerPaymentProvider(scriptedProvider([paid]), { override: true });
const { ctx, getCol } = makeRouteContext(mockSettings);
const { ctx, getCol, logs } = makeRouteContext(mockSettings);
const draftId = await startCheckout(ctx, "sess_repeat");
const payload = chargeSucceeded(draftId);

expect((await deliverWebhook(ctx, payload, headers)).status).toBe(200);
const repeat = await deliverWebhook(ctx, payload, headers);
expect(((await repeat.json()) as { duplicate?: boolean }).duplicate).toBe(true);
expect(getCol("orders").size).toBe(1);
// The log names the provider that sent the event, not Stripe.
expect(logs).toContainEqual(
expect.objectContaining({
level: "info",
msg: "Webhook deduped",
meta: expect.objectContaining({ providerId: "mock", type: "charge.succeeded" }),
}),
);
});

it("keeps the record for failures other than confirmation, as before", async () => {
Expand Down
Loading