Templates / Webhooks

Stripe webhook to Slack: post every payment to a channel

A Stripe webhook receiver that verifies the signature and posts each completed checkout to Slack. Code, setup and a one-click hosted version.

Get a Slack message the moment someone pays. The function receives Stripe’s webhook, checks the Stripe-Signature header so nobody can fake a payment, and posts a one-line summary to a channel.

Build it with Wisp

Type this into Wisp, or press Build this — it writes the code below, deploys it and gives you a URL:

A Stripe webhook that verifies the signature with STRIPE_WEBHOOK_SECRET and, for checkout.session.completed, posts the amount, currency and customer email to Slack using SLACK_WEBHOOK_URL.

The code

POST /stripe-webhook · API

import { createHmac, timingSafeEqual } from 'node:crypto';
import { config } from 'wisp';

async function slack(text) {
  await fetch(config.require('SLACK_WEBHOOK_URL'), {
    method: 'POST',
    headers: { 'content-type': 'application/json' },
    body: JSON.stringify({ text }),
  });
}

export async function handler(req) {
  // Verify against the exact bytes Stripe sent, not re-serialized JSON.
  const raw = Buffer.from(req.rawBody ?? '', req.isBase64Encoded ? 'base64' : 'utf8').toString('utf8');
  const parts = (req.headers['stripe-signature'] ?? '').split(',').map((p) => p.split('='));
  const t = parts.find(([k]) => k === 't')?.[1];
  const signatures = parts.filter(([k]) => k === 'v1').map(([, v]) => v);
  const expected = createHmac('sha256', config.require('STRIPE_WEBHOOK_SECRET')).update(`${t}.${raw}`).digest('hex');
  const fresh = t && Math.abs(Date.now() / 1000 - Number(t)) < 300; // reject replays older than 5 minutes
  const valid = signatures.some((s) => s.length === expected.length && timingSafeEqual(Buffer.from(s), Buffer.from(expected)));
  if (!fresh || !valid) return { status: 400, body: { error: 'Invalid signature' } };

  const event = JSON.parse(raw);
  if (event.type === 'checkout.session.completed') {
    const s = event.data.object;
    const amount = (s.amount_total / 100).toFixed(2);
    await slack(`:moneybag: ${amount} ${s.currency.toUpperCase()} from ${s.customer_details?.email ?? 'a customer'}`);
  }
  return { status: 200, body: { received: true } };
}

Set it up

Add these in the project’s Variables & secrets (secrets are encrypted and never shown in logs):

Try it

In the Stripe Dashboard add an endpoint with your function’s URL and the checkout.session.completed event, then press Send test event. Or with the Stripe CLI: stripe trigger checkout.session.completed.

How it works

Variations

Questions

Why does signature verification fail? Almost always because the body was parsed and re-serialized before verifying. Use the raw body, exactly as received, and the signing secret of this endpoint (test and live mode have different secrets).

Do I need the Stripe SDK? No. Verification is a few lines of HMAC, shown above; the SDK’s constructEvent does the same thing.

Build it with Wisp — free trial

Related