界面已本地化;技能介绍与说明文档按所选语言由 AI 自动翻译,代码、命令、路径和专业术语保留原文,以原作者文档为准。
探索技能
开发与工程 · SKILL.md

stripe-payments

stripe-payments

【开发与工程】Stripe subscription lifecycle and webhook handling.

独立 GitHub 快照 · 自动分类 · 以原作者文档为准

技能说明

GitHub

name: stripe-payments description: Stripe subscription lifecycle and webhook handling.

Stripe Payments & Subscriptions

Manage Stripe subscription lifecycles securely via robust webhook handling and Idempotency.

Best Practices

  1. Always Verify Webhook Signatures: Prevent replay and spoofing attacks by verifying the Stripe-Signature header.
  2. Idempotency Keys: Always pass idempotency keys for mutations (e.g., creating customers, subscriptions) to handle network retries safely.
  3. Async Webhook Processing: Acknowledge the webhook with a 200 OK immediately, then process the event asynchronously.

Code Snippet: Webhook Handler

import Stripe from 'stripe';
import { buffer } from 'micro';

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, { apiVersion: '2023-10-16' });

export const config = { api: { bodyParser: false } };

export default async function webhookHandler(req, res) {
  const buf = await buffer(req);
  const sig = req.headers['stripe-signature'];

  let event;
  try {
    event = stripe.webhooks.constructEvent(buf, sig, process.env.STRIPE_WEBHOOK_SECRET);
  } catch (err) {
    return res.status(400).send(`Webhook Error: ${err.message}`);
  }

  // Acknowledge receipt
  res.status(200).json({ received: true });

  // Process asynchronously
  switch (event.type) {
    case 'customer.subscription.created':
    case 'customer.subscription.updated':
    case 'customer.subscription.deleted':
      await handleSubscriptionChange(event.data.object);
      break;
    case 'invoice.payment_succeeded':
      await handlePaymentSucceeded(event.data.object);
      break;
    default:
      console.log(`Unhandled event type ${event.type}`);
  }
}

Subscription Lifecycle Flow

%%{init: {"theme": "default", "flowchart": {"useMaxWidth": true}}}%%
flowchart TD
    A[Checkout] -->|Create Subscription| B(Stripe API)
    B -->|Webhook: subscription.created| C[Update DB Status: Active]
    B -->|Billing Cycle End| D{Payment Success?}
    D -- Yes --> E[Webhook: payment_succeeded]
    E --> F[Extend Access]
    D -- No --> G[Webhook: payment_failed]
    G --> H[Update DB Status: Past Due]
我的收藏