Skip to content
LogoLogo

Mppx.compose

Present multiple payment options

Combines multiple method handlers into a single route handler that presents all methods to the client via multiple WWW-Authenticate headers.

Usage

Present both stablecoin and Stripe card payment options for a single endpoint. The client picks whichever method it supports.

import { , ,  } from 'mppx/server'
 
const  = '0x20c0000000000000000000000000000000000000'
const  = '0x20C000000000000000000000b9537d11c60E8b50'
const  = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
 
const  = .({  })
const  = .({
  : 2,
  : 'acct_1234',
  : ['card'],
  : 'sk_live_...',
})
 
const  = .({ : [, ] })
 
export async function (: Request) {
  const  = await .(
    [, { : '1', :  }],
    [, { : '1', :  }],
    [, { : '1', : 'usd' }],
  )()
 
  if (. === 402) return .
  return .(.({ : '...' }))
}

Behavior

  • No Credential present: Calls all handlers and merges their 402 Challenges into a single response with multiple WWW-Authenticate headers.
  • Accept-Payment present: Ranks and filters the merged Challenges by the client's supported method/intent entries. Entries with q=0 are excluded. If the header is invalid or filters out every Challenge, all Challenges are returned.
  • Credential present: Dispatches to the handler matching the Credential's method and intent.

Return type

type ReturnType = (input: Request) => Promise<
  | { status: 402; challenge: Response }
  | { status: 200; withReceipt: <T>(response: T) => T }
>

Parameters

...entries

  • Type: readonly [Method.Server | string, Options][]

Each entry is a tuple of a method reference (or string key like "tempo/charge") and the request options for that method. Requires at least one entry.