Integrations / Payments / Stripe

๐Ÿ’ณ

Stripe

Connect your Stripe webhook to LogIt and track every charge, subscription, refund, and failed payment in real time.


Setup

In your Stripe dashboard, go to Developers โ†’ Webhooks โ†’ Add endpoint.

Point it at https://yourdomain.com/api/webhooks/stripe and select these events:

  • checkout.session.completed
  • customer.subscription.created
  • customer.subscription.deleted
  • invoice.payment_failed
  • charge.refunded

Install

bash
npm install logit stripe

Handler (Next.js App Router)

ts
// app/api/webhooks/stripe/route.ts
import Stripe from "stripe";
import { init } from "logit";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
const logit = init({ token: process.env.LOGIT_API_KEY! });

export async function POST(req: Request) {
  const body = await req.text();
  const sig = req.headers.get("stripe-signature")!;

  const event = stripe.webhooks.constructEvent(
    body,
    sig,
    process.env.STRIPE_WEBHOOK_SECRET!
  );

  switch (event.type) {
    case "checkout.session.completed": {
      const session = event.data.object;
      await logit.now("payments", {
        event: "Payment received",
        description: `${session.customer_email} โ€” $${((session.amount_total ?? 0) / 100).toFixed(2)}`,
        icon: "๐Ÿ’ฐ",
        notify: true,
        tags: { currency: session.currency ?? "usd" },
        metadata: {
          sessionId: session.id,
          email: session.customer_email,
          amount: session.amount_total,
        },
      });
      break;
    }
    case "customer.subscription.deleted": {
      const sub = event.data.object;
      await logit.now("churn", {
        event: "Subscription cancelled",
        description: `Customer ${sub.customer}`,
        icon: "๐Ÿ˜ข",
        notify: true,
        metadata: { subscriptionId: sub.id, customerId: sub.customer },
      });
      break;
    }
    case "invoice.payment_failed": {
      const invoice = event.data.object;
      await logit.now("errors", {
        event: "Payment failed",
        description: `${invoice.customer_email} โ€” ${invoice.last_finalization_error?.message ?? "unknown"}`,
        icon: "โŒ",
        notify: true,
        metadata: { invoiceId: invoice.id, email: invoice.customer_email },
      });
      break;
    }
  }

  return new Response("ok");
}

Environment variables

bash
LOGIT_API_KEY=lk_live_xxxxxxxxxxxxxxxx
STRIPE_SECRET_KEY=sk_live_xxxxxxxxxxxxxxxx
STRIPE_WEBHOOK_SECRET=whsec_xxxxxxxxxxxxxxxx

Ready to connect Stripe?

7-day free trial. No credit card required.

Start free