open-index/open-npm huggingface dataset:
npm package: @stripe402/client-axios
Description: Axios interceptor that automatically handles stripe402 402 responses. When a paid endpoint returns 402, the interceptor calls your callback to get payment details, then retries the request transparently.
Author: Lance Whatley
Latest version: 0.0.1

# @stripe402/client-axios

Axios interceptor that automatically handles stripe402 402 responses. When a paid endpoint returns 402, the interceptor calls your callback to get payment details, then retries the request transparently.

## Install

```bash
npm install @stripe402/client-axios axios
```

## Usage

```ts
import axios from 'axios'
import { createStripe402Axios } from '@stripe402/client-axios'

const client = createStripe402Axios(axios.create(), {
  onPaymentRequired: async (requirements) => {
    // requirements includes: amount, minTopUp, currency, publishableKey, description
    const pm = await stripe.createPaymentMethod(/* ... */)
    return { paymentMethodId: pm.id, topUpAmount: requirements.minTopUp }
  },
})

// Automatically handles 402 → payment → retry
const response = await client.get('https://api.example.com/api/weather')
```

After the first successful payment, the client stores the returned `clientId` and uses it for subsequent requests — no re-payment until credits run out.

Return `null` from `onPaymentRequired` to decline payment and let the 402 error propagate.
npm package: @stripe402/client-fetch
Description: Fetch wrapper that automatically handles stripe402 402 responses. Same behavior as `@stripe402/client-axios`, but wraps the native `fetch` API with zero dependencies beyond `@stripe402/core`.
Author: Lance Whatley
Latest version: 0.0.1

# @stripe402/client-fetch

Fetch wrapper that automatically handles stripe402 402 responses. Same behavior as `@stripe402/client-axios`, but wraps the native `fetch` API with zero dependencies beyond `@stripe402/core`.

## Install

```bash
npm install @stripe402/client-fetch
```

## Usage

```ts
import { createStripe402Fetch } from '@stripe402/client-fetch'

const fetchWithPayment = createStripe402Fetch({
  onPaymentRequired: async (requirements) => {
    return { paymentMethodId: 'pm_...', topUpAmount: requirements.minTopUp }
  },
})

const response = await fetchWithPayment('https://api.example.com/api/weather')
const data = await response.json()
```

After the first successful payment, the wrapper stores the returned `clientId` and uses it for subsequent requests — no re-payment until credits run out.

Return `null` from `onPaymentRequired` to decline payment and return the original 402 response.
npm package: @stripe402/core
Description: Protocol types, constants, header encoding/decoding, HMAC identity derivation, and error types for the stripe402 protocol. Zero external dependencies (Node.js crypto only).
Author: Lance Whatley
Latest version: 0.0.1

# @stripe402/core

Protocol types, constants, header encoding/decoding, HMAC identity derivation, and error types for the stripe402 protocol. Zero external dependencies (Node.js crypto only).

## Install

```bash
npm install @stripe402/core
```

## Usage

### Header encoding/decoding

```ts
import { encodeHeader, decodeHeader } from '@stripe402/core'

const encoded = encodeHeader({ amount: 500, currency: 'usd' })
const decoded = decodeHeader<{ amount: number; currency: string }>(encoded)
```

### Client identity derivation

```ts
import { deriveClientId } from '@stripe402/core'

// Deterministic: same card fingerprint + server secret = same client ID
const clientId = deriveClientId(cardFingerprint, serverSecret)
```

### Unit conversion

```ts
import { unitsToCents, unitsToDollars, UNITS_PER_CENT } from '@stripe402/core'

unitsToCents(500)    // 5 (cents)
unitsToDollars(500)  // "0.05"
```

### Types

Key types exported: `PaymentRequiredResponse`, `PaymentPayload`, `PaymentResponse`, `RouteConfig`, `Stripe402ServerConfig`, `Stripe402ClientConfig`, `Stripe402Store`, `ClientRecord`, `TransactionRecord`.
npm package: @stripe402/express
Description: Express middleware that protects routes with HTTP 402 payment gates. Handles the full lifecycle: 402 challenge, Stripe payment processing, credit management, and resource delivery.
Author: Lance Whatley
Latest version: 0.0.1

# @stripe402/express

Express middleware that protects routes with HTTP 402 payment gates. Handles the full lifecycle: 402 challenge, Stripe payment processing, credit management, and resource delivery.

## Install

```bash
npm install @stripe402/express @stripe402/server
```

## Usage

```ts
import express from 'express'
import Redis from 'ioredis'
import { stripe402Middleware } from '@stripe402/express'
import { RedisStore } from '@stripe402/server'

const app = express()

app.use(stripe402Middleware({
  stripeSecretKey: process.env.STRIPE_SECRET_KEY!,
  stripePublishableKey: process.env.STRIPE_PUBLISHABLE_KEY!,
  serverSecret: process.env.SERVER_SECRET!,
  store: new RedisStore(new Redis()),
  routes: {
    'GET /api/weather': {
      amount: 500,       // 5 cents (500 units)
      minTopUp: 50_000,  // $5.00 minimum charge
      description: 'Weather data',
    },
  },
}))

// This handler only runs after payment is verified
app.get('/api/weather', (req, res) => {
  res.json({ temperature: 72, conditions: 'Sunny' })
})
```

Routes not listed in the config pass through to the next middleware untouched.
npm package: @stripe402/server
Description: Server-side Stripe integration and persistence stores for the stripe402 protocol. Framework-agnostic — provides the building blocks that framework-specific middleware packages (like `@stripe402/express`) use.
Author: Lance Whatley
Latest version: 0.0.1

# @stripe402/server

Server-side Stripe integration and persistence stores for the stripe402 protocol. Framework-agnostic — provides the building blocks that framework-specific middleware packages (like `@stripe402/express`) use.

## Install

```bash
npm install @stripe402/server
```

## Usage

### StripeService

Handles PaymentIntent creation, card fingerprint retrieval, and customer management.

```ts
import { StripeService } from '@stripe402/server'

const stripe = new StripeService(process.env.STRIPE_SECRET_KEY!)

const intent = await stripe.createAndConfirmPayment({
  amount: 500,       // in Stripe cents
  currency: 'usd',
  paymentMethodId: 'pm_...',
  customerId: 'cus_...',
})

const fingerprint = await stripe.getCardFingerprint('pm_...')
const customer = await stripe.findOrCreateCustomer(clientId, 'pm_...')
```

### RedisStore

Fast credit balance storage using Redis with Lua scripts for atomic deduction.

```ts
import Redis from 'ioredis'
import { RedisStore } from '@stripe402/server'

const store = new RedisStore(new Redis())

await store.addBalance('client123', 50_000)
const remaining = await store.deductBalance('client123', 100) // returns new balance or null
```

### PostgresStore

Durable credit balance storage with transaction audit logging.

```ts
import { Pool } from 'pg'
import { PostgresStore } from '@stripe402/server'

const store = new PostgresStore(new Pool({ connectionString: DATABASE_URL }))
await store.createTables() // run once on startup
```

Both stores implement the `Stripe402Store` interface from `@stripe402/core`.