← 返回列表
全部 Skill›商业 Business›stripe-integration

stripe-integration

作者 @davila7 · 语言 en
Build or review a Stripe integration with current Stripe guidance: Checkout Sessions, Payment Element, Setup Intents, subscriptions and the Customer Portal, webhook-driven fulfillment, idempotency, restricted API keys and Stripe Tax. Use when: stripe, payments, checkout, subscription, billing, webhook, refund, payment intent.
商业 Business支付需海外环境访问网络敏感路径待翻译
最近源活动
—
检测 SKILL.md 语言
en
星标 ★
34,175
分支 Fork
0
安装选项
提示
指令
使用 AI 助手安装
将下面的提示复制到你正在使用的 AI 助手中。
下载完整 Skill 目录(含全部配套文件)。 下载压缩包 ↓
SKILL.md 原文预览

Stripe Integration

Guidance for building payment code that does not lose orders, double-charge or

leak keys. It follows Stripe's own recommendations as of October 2026. When a

detail matters (a parameter, an event name, an API version), check

docs.stripe.com or the Stripe MCP server's

search_stripe_documentation tool before relying on memory.

Versions

  • Latest API version: 2026-09-30.endive. Pin it explicitly on the client so

an account default change never alters response shapes under you.

  • Latest server SDKs: Node.js 23, Python 16, Ruby 20, PHP 22, Java 34, Go 87,

.NET 53. Create a client instance; the global key pattern

(stripe.api_key = …, Stripe.setApiKey) is deprecated in every SDK.

  • To move an existing integration to a newer version, read

Upgrading your API version first.

// lib/stripe.ts
import Stripe from 'stripe';

export const stripe = new Stripe(process.env.STRIPE_API_KEY!, {
  apiVersion: '2026-09-30.endive',
});

Choose the API

| Building | Use |

|---|---|

| One-time payment, simple product | Payment Links (no code) or Checkout Sessions |

| One-time payment in your web app | Checkout Sessions (hosted or embedded) |

| Custom payment form | Checkout Sessions with ui_mode: 'custom' + Payment Element |

| Save a card for later | Setup Intents |

| Charge off-session (no customer present) | Payment Intents with a saved payment method |

| Subscriptions, seats, trials | Billing APIs + Checkout mode: 'subscription' + Customer Portal |

| New usage-based billing | Metronome (Stripe's usage platform), not raw Billing Meters |

| Marketplace or platform | Connect, Accounts v2 (/v2/core/accounts), Stripe-hosted onboarding |

Never use the Charges API, Sources API, Tokens API, Card Element or the

plan object. They are deprecated or legacy; use the rows above and Prices.

Rules

Fulfill from webhooks, never from the success page

A customer can pay and close the tab before the success page loads. Grant

access, send email and write orders only in the webhook handler.

  • Handle checkout.session.completed and

checkout.session.async_payment_succeeded. Fulfill only when

session.payment_status !== 'unpaid'. Delayed methods (bank debits) send

completed while still unpaid.

  • Handle checkout.session.async_payment_failed for those failures.
  • Subscriptions need customer.subscription.created|updated|deleted,

invoice.paid and invoice.payment_failed. Renewals, dunning and

cancellations only reach you this way.

  • Webhooks are required before launch, not a follow-up.

Verify every webhook signature on the raw body

Signature verification needs the exact bytes Stripe sent. Body-parsing

middleware breaks it.

// Next.js App Router: app/api/stripe/webhook/route.ts
import Stripe from 'stripe';
import { stripe } from '@/lib/stripe';

export async function POST(req: Request) {
  const body = await req.text(); // raw body, do not use req.json()
  const signature = req.headers.get('stripe-signature');
  if (!signature) return new Response('missing signature', { status: 400 });

  let event: Stripe.Event;
  try {
    event = stripe.webhooks.constructEvent(
      body,
      signature,
      process.env.STRIPE_WEBHOOK_SECRET!,
    );
  } catch {
    return new Response('invalid signature', { status: 400 });
  }

  switch (event.type) {
    case 'checkout.session.completed':
    case 'checkout.session.async_payment_succeeded': {
      // Re-fetch instead of trusting the payload snapshot.
      const session = await stripe.checkout.sessions.retrieve(event.data.object.id);
      // fulfillOrder must be idempotent: the same event can arrive twice.
      if (session.payment_status !== 'unpaid') await fulfillOrder(session.id);
      break;
    }
    case 'checkout.session.async_payment_failed':
      await markPaymentFailed(event.data.object.id);
      break;
  }
  return new Response(null, { status: 200 });
}
  • On Cloudflare Workers, Deno or Bun use await stripe.webhooks.constructEventAsync(...).
  • In Express, mount express.raw({ type: 'application/json' }) on the webhook

route only, before any global express.json().

  • In live mode Stripe retries for up to three days and can deliver an event more than once

or out of order. Make fulfillOrder idempotent (store processed event or

session IDs with a unique constraint) and decide by the object's current

state, not by event order.

  • Return 2xx quickly; move slow work to a queue.

Use idempotency keys on every create

A network retry without one can create a second charge or subscription.

await stripe.paymentIntents.create(
  { amount: 2000, currency: 'usd', customer: customerId },
  { idempotencyKey: `order-${orderId}-charge` },
);

Derive the key from your own record (order ID, user ID + action), not from a

random value per attempt, or retries get new keys.

Let Stripe pick payment methods

Do not pass payment_method_types (not even ['card']). Omitting it turns on

dynamic payment methods, configured in the Dashboard. To narrow the list use

payment_method_configuration, excluded_payment_method_types, or

allowed_payment_method_types on Payment and Setup Intents. The one exception

is Terminal: payment_method_types: ['card_present'].

Map Stripe objects to your records by ID

Store the Stripe Customer, Subscription an

评论
Ddavila7
davila7
davila7/claude-code-templates
查看 GitHub 仓库 查看作者的其他 Skill
来源信息
作者@davila7
所属大类商业 Business / 支付
国内可用性需海外环境
最近源活动—
风险提示访问网络、敏感路径
提示:风险标签为程序静态扫描结果,仅作提示,不代表安全认证。安装前请阅读 SKILL.md 及其引用的脚本、权限,自行判断是否可信。