> ## Documentation Index
> Fetch the complete documentation index at: https://getfloo.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Take payments with Stripe on floo

> Sell from an app on floo with Stripe Checkout, a signed webhook, and orders recorded in managed Postgres.

Stripe Checkout hosts the card form, so card numbers never reach your app or floo. Your app creates a Checkout Session and redirects the buyer to Stripe. Stripe sends `checkout.session.completed` to a webhook route on your app, and the route records the order in Postgres.

The examples use Next.js with the App Router and the `stripe` and `pg` packages. You need a [Stripe account](https://dashboard.stripe.com/register) and a Price for the product you sell.

## Set up your agent

```bash theme={null}
curl -fsSL https://getfloo.com/install.sh | bash
floo auth login
```

For Claude Code, also run `floo skills install --path .claude/skills/floo`. Cursor, Codex, and other agents: [Agent setup](https://getfloo.com/docs/guides/agent-setup.md).

## Agent prompt

Paste into your agent. Replace `storefront` with your app name.

```text theme={null}
Add Stripe Checkout to this app and deploy it to floo. App name: storefront.
1. Install the stripe and pg packages.
2. Use the floo.app.toml, routes and migration from https://getfloo.com/docs/deploy/stripe-payments.md.
3. Run `floo deploy --preflight --json` and fix every error. Commit everything with git.
4. Run `floo deploy --json`. It stops on STRIPE_SECRET_KEY, STRIPE_WEBHOOK_SECRET and STRIPE_PRICE_ID.
5. Have me run `floo env set STRIPE_SECRET_KEY --stdin` with my Stripe test-mode secret key, and `floo env set STRIPE_PRICE_ID=price_123`, using my Price ID in place of price_123. Never ask me to paste a secret to you.
6. Tell me to add a webhook endpoint in the Stripe Dashboard (test mode) at the dev URL plus /api/stripe/webhook, listening for checkout.session.completed and checkout.session.async_payment_succeeded. Have me run `floo env set STRIPE_WEBHOOK_SECRET --stdin` with its signing secret.
7. Run `floo deploy --rebuild --json` and report the dev URL.
```

## floo.app.toml

```toml floo.app.toml theme={null}
[app]
name = "storefront"
access_mode = "public"

[services.web]
type = "web"
path = "."
port = 3000
ingress = "public"
migrate_command = "node migrate.mjs"

[services.web.env]
managed = ["postgres"]
required = ["STRIPE_SECRET_KEY", "STRIPE_WEBHOOK_SECRET", "STRIPE_PRICE_ID"]

[managed.default]
type = "postgres"
```

* `access_mode = "public"` on every environment, dev included. Stripe sends webhooks without a floo sign-in, so an environment behind [App sign-in](https://getfloo.com/docs/guides/app-auth.md) never receives them. The webhook route checks Stripe's signature instead. To keep dev private, see [Test webhooks with a private dev](#test-webhooks-with-a-private-dev).
* `required` stops a deploy that is missing a Stripe variable and names it.
* Dev and prod each get their own Postgres, so test-mode orders never land in prod's table.

## Create the orders table

`migrate_command` runs once per deploy, before traffic moves, in your runtime image, so the Dockerfile must copy `migrate.mjs` into it. A row is a paid order; the `checkout_session_id` primary key records each one once.

```js migrate.mjs theme={null}
import pg from "pg";

const client = new pg.Client({ connectionString: process.env.DATABASE_URL });
await client.connect();
await client.query(`
  CREATE TABLE IF NOT EXISTS orders (
    checkout_session_id text PRIMARY KEY,
    customer_email text,
    amount_total bigint NOT NULL,
    currency text NOT NULL,
    created_at timestamptz NOT NULL DEFAULT now()
  )
`);
await client.end();
```

## Start a checkout

```ts app/api/checkout/route.ts theme={null}
import Stripe from "stripe";

export async function POST() {
  const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
  const session = await stripe.checkout.sessions.create({
    mode: "payment",
    line_items: [{ price: process.env.STRIPE_PRICE_ID!, quantity: 1 }],
    success_url: `${process.env.FLOO_APP_URL}/thanks`,
    cancel_url: `${process.env.FLOO_APP_URL}/`,
  });
  return Response.redirect(session.url!, 303);
}
```

A form with `method="post"` and `action="/api/checkout"` sends the buyer to Stripe. Both routes create the Stripe client inside the handler: `next build` loads route modules without your runtime secrets, and a client created at module load throws there for a missing key. floo sets `FLOO_APP_URL` to each environment's URL, including a [custom domain](https://getfloo.com/docs/guides/custom-domains.md) once it is active.

## Receive the webhook

Stripe signs the exact bytes it sends. Read the body with `request.text()` and verify it before parsing; a parsed and re-serialized body fails the check. floo passes the body to your app unchanged.

```ts app/api/stripe/webhook/route.ts theme={null}
import Stripe from "stripe";
import pg from "pg";

const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });

export async function POST(request: Request) {
  const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
  const body = await request.text();
  let event: Stripe.Event;
  try {
    event = stripe.webhooks.constructEvent(
      body,
      request.headers.get("stripe-signature")!,
      process.env.STRIPE_WEBHOOK_SECRET!,
    );
  } catch {
    return new Response("invalid signature", { status: 400 });
  }

  if (
    event.type === "checkout.session.completed" ||
    event.type === "checkout.session.async_payment_succeeded"
  ) {
    const session = event.data.object;
    if (session.payment_status !== "unpaid") {
      await pool.query(
        `INSERT INTO orders (checkout_session_id, customer_email, amount_total, currency)
         VALUES ($1, $2, $3, $4)
         ON CONFLICT (checkout_session_id) DO NOTHING`,
        [session.id, session.customer_details?.email, session.amount_total, session.currency],
      );
    }
  }
  return new Response(null, { status: 200 });
}
```

`checkout.session.completed` means the buyer finished Checkout, not that the money arrived. With a delayed payment method, such as a bank debit, the session completes `unpaid` and `checkout.session.async_payment_succeeded` follows once the payment clears, so the route records an order only when `payment_status` is not `unpaid`.

Stripe retries a delivery that fails or times out, so the same event can arrive more than once; `ON CONFLICT DO NOTHING` records each session once. Answer quickly and move slow work, such as emails or fulfillment calls, to a [cron job](https://getfloo.com/docs/guides/cron-jobs.md) that reads new rows.

A webhook to a service that has scaled to zero waits for a [cold start](https://getfloo.com/docs/guides/cold-starts.md). To keep prod ready, set `min_instances = 1` for prod only:

```toml theme={null}
[environments.prod.resources]
min_instances = 1
```

A warm instance bills while it idles; see [Scaling and availability](https://getfloo.com/docs/guides/scaling.md#warm-http).

## Go live

Prod uses Stripe live mode. Set the live keys on floo's prod environment, with your live Price ID in place of `price_123`:

```bash theme={null}
floo env set STRIPE_SECRET_KEY --stdin --env prod
floo env set STRIPE_PRICE_ID=price_123 --env prod
```

In the Stripe Dashboard, in live mode, add a webhook endpoint at the prod URL plus `/api/stripe/webhook` for `checkout.session.completed` and `checkout.session.async_payment_succeeded`, then set its signing secret:

```bash theme={null}
floo env set STRIPE_WEBHOOK_SECRET --stdin --env prod
```

Release to prod as in [Deploy a customer-facing app](https://getfloo.com/docs/deploy/customer-app.md#deploy-to-dev-then-release-to-prod): `gh release create` for your GitHub repository, or `floo promote --app storefront` for an app floo hosts. A test-mode key never reaches prod, because dev and prod hold separate values.

## Test webhooks with a private dev

To keep dev behind App sign-in, set `[environments.dev] access_mode = "accounts"` and test webhooks on your machine with the [Stripe CLI](https://docs.stripe.com/stripe-cli):

```bash theme={null}
stripe listen --forward-to localhost:3000/api/stripe/webhook
```

`stripe listen` prints a signing secret for local deliveries. Run the app locally with that secret as `STRIPE_WEBHOOK_SECRET`. `floo run --service web -- npm run dev` supplies dev's `DATABASE_URL` but withholds secrets, so set `STRIPE_SECRET_KEY` and `STRIPE_WEBHOOK_SECRET` in your local shell.

## Troubleshooting

| Symptom | Fix |
| - | - |
| Stripe shows `400 invalid signature` | `STRIPE_WEBHOOK_SECRET` is not the signing secret of this endpoint, or the route parsed the body before verifying it. Each endpoint, test and live, has its own secret. |
| Stripe shows a redirect or `401` from the webhook | The environment is behind App sign-in. Set its `access_mode` to `public`, or test locally with `stripe listen`. |
| Deploy stops with `MISSING_REQUIRED_ENV_VAR` | Set the named variable with `floo env set`, then `floo deploy --rebuild`. |
| A test-mode order appears in prod | Prod's `STRIPE_SECRET_KEY` is a test key. Set the live key with `--env prod`. |

## Next steps

> [card] [Deploy a customer-facing app](https://getfloo.com/docs/deploy/customer-app.md)
>
> Custom domain and releases for the app around the checkout.

> [card] [Cron jobs](https://getfloo.com/docs/guides/cron-jobs.md)
>
> Run fulfillment and emails off the webhook path.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.