Payments stripe elements checkout

From CraftOS-dev/craftbot-agent-bundles · View interactive version

Payments stripe elements checkout is an AI agent skill from the GitHub repository CraftOS-dev/craftbot-agent-bundles. Source: https://docs.stripe.com/payments/checkout · https://docs.stripe.com/payments/payment-element

How to use this skill

Skill file

<!--

Source: https://docs.stripe.com/payments/checkout · https://docs.stripe.com/payments/payment-element

Authored: June 2026 for the frontend-engineer agent bundle.

-->

Payments — Stripe Elements + Checkout + Connect

Stripe is the standard for web payments in 2026. Checkout is the hosted

flow (simplest, PCI-DSS off your plate). Elements + Payment Intents give

you inline custom UX. Connect is for marketplaces. Webhooks must be

signature-verified. Always use Stripe test mode for development.

When to use

"Payment Intents", "webhook", "Connect"

Setup

pnpm add stripe @stripe/stripe-js @stripe/react-stripe-js

# Stripe CLI for webhook forwarding in dev
brew install stripe/stripe-cli/stripe        # macOS
# or scoop install stripe                    # Windows
# or pnpm dlx stripe-cli@latest              # one-off
stripe login                                 # opens browser

Auth / API key requirements:

production. Get from https://dashboard.stripe.com/apikeys.

Free for test mode; live mode requires verified business account + processing

fees (2.9% + $0.30 typical).

Common recipes

Recipe 1 — Stripe Checkout (hosted, simplest)

// app/api/checkout/route.ts
import Stripe from "stripe";
import { NextResponse } from "next/server";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export async function POST(req: Request) {
  const { priceId } = await req.json();

  const session = await stripe.checkout.sessions.create({
    mode: "subscription",                    // or "payment" for one-time
    line_items: [{ price: priceId, quantity: 1 }],
    success_url: `${process.env.APP_URL}/success?session_id={CHECKOUT_SESSION_ID}`,
    cancel_url: `${process.env.APP_URL}/cancel`,
    automatic_tax: { enabled: true },
    customer_email: req.headers.get("x-user-email") ?? undefined,
    allow_promotion_codes: true,
  });

  return NextResponse.json({ url: session.url });
}
"use client";
async function subscribe(priceId: string) {
  const { url } = await fetch("/api/checkout", {
    method: "POST",
    body: JSON.stringify({ priceId }),
  }).then((r) => r.json());
  window.location.assign(url);
}

<button onClick={() => subscribe("price_1Pxxx")}>Subscribe — $29/mo</button>

Recipe 2 — Inline Payment Element (custom UX)

// app/checkout/page.tsx
"use client";
import { useEffect, useState } from "react";
import { Elements, PaymentElement, useStripe, useElements } from "@stripe/react-stripe-js";
import { loadStripe } from "@stripe/stripe-js";

const stripePromise = loadStripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY!);

export default function CheckoutPage() {
  const [clientSecret, setClientSecret] = useState<string>();

  useEffect(() => {
    fetch("/api/payment-intent", { method: "POST" })
      .then((r) => r.json())
      .then((d) => setClientSecret(d.clientSecret));
  }, []);

  if (!clientSecret) return <p>Loading...</p>;

  return (
    <Elements stripe={stripePromise} options={{ clientSecret, appearance: { theme: "stripe" } }}>
      <CheckoutForm />
    </Elements>
  );
}

function CheckoutForm() {
  const stripe = useStripe();
  const elements = useElements();
  const [submitting, setSubmitting] = useState(false);
  const [error, setError] = useState<string>();

  async function onSubmit(e: React.FormEvent) {
    e.preventDefault();
    if (!stripe || !elements) return;
    setSubmitting(true);

    const { error } = await stripe.confirmPayment({
      elements,
      confirmParams: { return_url: `${window.location.origin}/order-complete` },
    });

    if (error) setError(error.message);
    setSubmitting(false);
  }

  return (
    <form onSubmit={onSubmit}>
      <PaymentElement />
      <button type="submit" disabled={!stripe || submitting}>
        {submitting ? "Processing..." : "Pay"}
      </button>
      {error && <p role="alert">{error}</p>}
    </form>
  );
}
// app/api/payment-intent/route.ts
export async function POST() {
  const intent = await stripe.paymentIntents.create({
    amount: 2999,
    currency: "usd",
    automatic_payment_methods: { enabled: true },
  });
  return NextResponse.json({ clientSecret: intent.client_secret });
}

Recipe 3 — Webhook signature verification

// app/api/webhook/route.ts
import Stripe from "stripe";
import { NextResponse } from "next/server";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export async function POST(req: Request) {
  const sig = req.headers.get("stripe-signature");
  if (!sig) return new Response("No signature", { status: 400 });

  let event: Stripe.Event;
  try {
    const body = await req.text();
    event = stripe.webhooks.constructEvent(body, sig, process.env.STRIPE_WEBHOOK_SECRET!);
  } catch (err) {
    return new Response(`Webhook error: ${(err as Error).message}`, { status: 400 });
  }

  switch (event.type) {
    case "checkout.session.completed": {
      const session = event.data.object;
      await db.subscription.upsert({
        where: { userId: session.client_reference_id! },
        create: { userId: session.client_reference_id!, status: "active", stripeId: session.id },
        update: { status: "active" },
      });
      break;
    }
    case "customer.subscription.deleted":
    case "customer.subscription.updated": {
      const sub = event.data.object;
      await db.subscription.update({
        where: { stripeId: sub.id },
        data: { status: sub.status },
      });
      break;
    }
    case "invoice.payment_failed":
      // notify user
      break;
  }

  return NextResponse.json({ received: true });
}

Important: in App Router, you cannot use req.json() for the webhook —

the raw body must be passed to constructEvent. Use req.text().

Recipe 4 — Local webhook forwarding

stripe listen --forward-to http://localhost:3000/api/webhook
# Prints a webhook signing secret — set STRIPE_WEBHOOK_SECRET to that value in .env.local

Trigger a test event:

stripe trigger checkout.session.completed
stripe trigger invoice.payment_succeeded

Recipe 5 — Customer Portal (self-service billing)

// app/api/portal/route.ts
export async function POST(req: Request) {
  const userId = await getUserId(req);
  const user = await db.user.findUnique({ where: { id: userId }, select: { stripeCustomerId: true } });
  if (!user?.stripeCustomerId) return new Response("No customer", { status: 404 });

  const session = await stripe.billingPortal.sessions.create({
    customer: user.stripeCustomerId,
    return_url: `${process.env.APP_URL}/settings/billing`,
  });
  return NextResponse.json({ url: session.url });
}

Customer Portal handles plan changes, cancellation, invoice download, payment

method updates — for free, no UI to build.

Recipe 6 — Create Customer + attach to Checkout

const customer = await stripe.customers.create({
  email: user.email,
  metadata: { userId: user.id },
});
await db.user.update({ where: { id: user.id }, data: { stripeCustomerId: customer.id } });

// Later, in checkout:
const session = await stripe.checkout.sessions.create({
  customer: customer.id,
  client_reference_id: user.id,
  // ...
});

Recipe 7 — Stripe Connect (marketplace)

const account = await stripe.accounts.create({
  type: "express",                           // or "standard" / "custom"
  email: seller.email,
  business_type: "individual",
});

const accountLink = await stripe.accountLinks.create({
  account: account.id,
  refresh_url: `${process.env.APP_URL}/connect/refresh`,
  return_url: `${process.env.APP_URL}/connect/complete`,
  type: "account_onboarding",
});

// Redirect user to accountLink.url

When checking out for a Connected account product:

const session = await stripe.checkout.sessions.create({
  payment_intent_data: {
    application_fee_amount: Math.round(total * 0.05),   // 5% platform fee
    transfer_data: { destination: sellerStripeAccountId },
  },
  // ...
});

Recipe 8 — Subscription with trial

const session = await stripe.checkout.sessions.create({
  mode: "subscription",
  line_items: [{ price: "price_1Pxxx", quantity: 1 }],
  subscription_data: {
    trial_period_days: 14,
    trial_settings: { end_behavior: { missing_payment_method: "pause" } },
  },
  payment_method_collection: "if_required",
  // ...
});

Recipe 9 — Stripe Tax (automatic VAT/GST)

const session = await stripe.checkout.sessions.create({
  automatic_tax: { enabled: true },          // Stripe calculates tax based on customer location
  customer_update: { address: "auto" },      // Update saved address from Checkout
  tax_id_collection: { enabled: true },      // Collect B2B VAT ID
  // ...
});

Requires Stripe Tax enabled in dashboard.

Examples

Example 1: $29/mo SaaS with Checkout + webhook

# 1. Create product + price in Stripe dashboard
# 2. Implement Checkout route (Recipe 1)
# 3. Implement webhook + verification (Recipe 3)
# 4. Forward locally: stripe listen --forward-to localhost:3000/api/webhook
# 5. Trigger flow: stripe trigger checkout.session.completed

Example 2: Inline custom checkout with Payment Element

  1. Create PaymentIntent server-side (Recipe 2)
  2. Render <Elements> + <PaymentElement> client-side
  3. Call stripe.confirmPayment on submit
  4. Handle webhook for payment_intent.succeeded to fulfill order

Edge cases / gotchas

Session server-side with prices fetched from your DB or Stripe.

Use the event id as a dedup key.

parsed already and signature verification fails.

charge real cards. Use freely.

Custom card forms (rolling your own) put you in PCI scope.

true }` handles it.

payments until they complete it.

required"` skips the card prompt. Risky for fraud — pair with email

verification.

refund endpoint or use the Stripe dashboard.

<Elements>.

Sources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.