# /setup-payments — Stripe Payment Integration Wizard

## Usage
```
/setup-payments              # Full setup wizard (detect framework → install SDK → validate key → create products → generate code)
/setup-payments checkout     # Generate only checkout session endpoint
/setup-payments webhook      # Generate only webhook handler
/setup-payments portal       # Generate only customer portal redirect
/setup-payments status       # Check integration completeness
```

## Input
- `$ARGUMENTS` — optional: one of the modes above (defaults to full setup)

## Process

Parse `$ARGUMENTS` to determine mode. If empty or "setup", run the full wizard.

---

### Mode: Full Setup (default)

Runs the complete Stripe integration wizard:

1. **Detect Framework**: Reads `package.json` to identify Next.js (App/Pages Router), Express, Fastify, Remix, or generic Node.js. Checks for TypeScript and `src/` directory.

2. **Install Stripe SDK**: If `stripe` is not in `node_modules`, runs `npm install stripe`.

3. **Validate API Key**: Reads `STRIPE_SECRET_KEY` from `.env` or environment. Calls `stripe.accounts.retrieve()` to validate.
   - If missing: prints instructions to add it and stops.
   - If invalid: prints error and stops.

4. **Create Products**: Searches for existing products to avoid duplicates. If none found, creates a default "Pro Plan" ($29/mo).

5. **Generate Code**: Creates framework-appropriate files:
   - Checkout session endpoint
   - Webhook handler (with `constructEvent` signature verification)
   - Customer portal redirect
   - Checkout button component (Next.js only)
   - **Never overwrites existing files** — skips with a warning.

6. **Update .env**: Appends `STRIPE_SECRET_KEY` and `STRIPE_WEBHOOK_SECRET` if not present. Ensures `.env` is in `.gitignore`.

7. **Save State**: Writes integration status to `data/payments.json`.

---

### Mode: `checkout`

Generates only the checkout session creation endpoint for the detected framework.

---

### Mode: `webhook`

Generates only the webhook handler with Stripe signature verification.

---

### Mode: `portal`

Generates only the customer portal redirect endpoint.

---

### Mode: `status`

Audits the current integration:
- Is Stripe SDK installed?
- Is the API key valid?
- Do products exist in Stripe?
- Are checkout/webhook/portal endpoints generated?
- Is the webhook secret configured?

Reports completeness as a percentage.

---

## State File

`data/payments.json` tracks:
```json
{
  "last_updated": "2025-01-15T...",
  "integration_status": {
    "sdk_installed": true,
    "api_key_valid": true,
    "products_created": true,
    "checkout_endpoint": true,
    "webhook_endpoint": true,
    "portal_endpoint": false
  },
  "framework": { "framework": "nextjs", "typescript": true, "appRouter": true, "srcDir": true },
  "products": [
    { "name": "Pro Plan", "stripe_product_id": "prod_...", "stripe_price_id": "price_...", "amount_cents": 2900, "currency": "usd", "interval": "month" }
  ],
  "generated_files": ["src/app/api/checkout/route.ts", "src/app/api/webhook/route.ts"]
}
```

## Error Handling

- Missing `STRIPE_SECRET_KEY` → return instructions to add it to `.env`
- Invalid key → return `success: false` with "check your key" message
- Products already exist → skip creation, use existing IDs
- Target file exists → skip with warning (don't overwrite user code)
- Framework not detected → fall back to generic Node.js templates
