Skip to content

feat: add Stripe payment method (SPT charge intent)#97

Open
brendanjryan wants to merge 8 commits intomainfrom
feat/stripe-payment-method
Open

feat: add Stripe payment method (SPT charge intent)#97
brendanjryan wants to merge 8 commits intomainfrom
feat/stripe-payment-method

Conversation

@brendanjryan
Copy link
Collaborator

@brendanjryan brendanjryan commented Mar 23, 2026

Summary

Adds Stripe payment method support to pympp, mirroring the mppx TypeScript implementation.

Key design decisions

  • SPT flow: Uses Stripe's Shared Payment Token (private preview), matching mppx exactly
  • Duck-typed client: Supports both client.v1.payment_intents (stripe-python v8+) and client.payment_intents (legacy) via _resolve_payment_intents()
  • Two verification paths: Stripe SDK client or raw httpx POST to api.stripe.com
  • Idempotency key: mppx_{challenge_id}_{spt} — matches TypeScript SDK for cross-SDK consistency
  • Analytics metadata: Injects mpp_version, mpp_is_mpp, mpp_intent, etc. on every PaymentIntent
  • transform_request hook: Injects networkId and paymentMethodTypes into challenge methodDetails
  • Error classes: Uses VerificationFailedError (RFC 9457 Problem Details) instead of plain VerificationError
  • Shared DEFAULT_TIMEOUT: Extracted to mpp._defaults — used by both Stripe and Tempo methods

Usage

# Server
from mpp.server import Mpp
from mpp.methods.stripe import stripe, ChargeIntent

server = Mpp.create(
    method=stripe(
        network_id="bn_...",
        payment_method_types=["card"],
        currency="usd",
        decimals=2,
        intents={"charge": ChargeIntent(secret_key="sk_...")},
    ),
)

# Client
from mpp.methods.stripe import stripe

method = stripe(
    create_token=my_spt_proxy,
    payment_method="pm_card_visa",
    intents={},
)

Files

File Purpose
src/mpp/_defaults.py Shared DEFAULT_TIMEOUT constant
src/mpp/methods/stripe/__init__.py Package exports
src/mpp/methods/stripe/_defaults.py Stripe API base URL
src/mpp/methods/stripe/client.py Client-side credential creation (SPT flow)
src/mpp/methods/stripe/intents.py Server-side verification (PaymentIntent)
src/mpp/methods/stripe/schemas.py Pydantic schemas
tests/test_stripe.py 44 tests covering all paths
examples/stripe/ Working example (server + headless client)

mppx parity guards

  • Rejects metadata.externalId in challenge (reserved for credential payload)
  • Requires networkId in challenge.methodDetails
  • Surfaces Stripe error details (message/code) in raw HTTP path exceptions

Test coverage (44 tests)

  • Schema validation (payload, request, method details)
  • Client credential creation (happy path, external ID, missing fields)
  • Server verification (SDK client, modern v1 client, expiry, action required, failed status)
  • Raw HTTP path (success, Stripe error parsing, non-JSON errors, metadata form encoding)
  • Analytics metadata and idempotency key format
  • Async context manager lifecycle (owned vs injected client)
  • Factory defaults and parameter validation

Example (verified working)

examples/stripe/ contains a pay-per-fortune server + headless client using pm_card_visa. Tested end-to-end against Stripe test mode:

Client → GET /api/fortune → 402
Client → POST /api/create-spt → spt_... created
Client → GET /api/fortune + credential → pi_... succeeded → 🥠 fortune

brendanjryan and others added 8 commits March 23, 2026 13:02
Adds Stripe payment method support to pympp, mirroring the TypeScript
mppx implementation:

- StripeMethod client with create_token callback for SPT creation
- ChargeIntent server with Stripe SDK client or raw secret_key paths
- Pydantic schemas for ChargeRequest and StripeCredentialPayload
- transform_request hook for injecting networkId/paymentMethodTypes
- MPP analytics metadata on every PaymentIntent (mpp_version, etc.)
- Idempotency key format matching mppx: mppx_{challengeId}_{spt}
- 29 tests covering schemas, client, server, and factory
- Fix Stripe SDK client shape: support both client.v1.payment_intents
  (stripe-python v8+) and client.payment_intents (legacy/custom)
- Add metadata.externalId rejection in create_credential (mppx parity)
- Require networkId in challenge.methodDetails (mppx parity)
- Use VerificationFailedError instead of VerificationError for proper
  RFC 9457 Problem Details support
- Surface Stripe error details in raw HTTP path instead of generic msg
- Remove unused secret_key param from stripe() factory
- Fix docstring examples: clients don't need secret keys
- Convert OnChallengeParameters to frozen dataclass for consistency
- Remove fragile hasattr(__await__) sync/async detection
- Pass request body as first positional arg (Stripe SDK convention)
- Add tests: raw HTTP path, modern v1 client, async lifecycle,
  networkId guard, metadata.externalId rejection, factory param removal
  (44 tests, up from 29)
The StripeClient protocol was too strict for pyright's invariance
checking on mutable attributes. Since _resolve_payment_intents()
handles duck-type dispatch at runtime, the type annotation can
safely be Any.
Consolidates the 3 duplicate DEFAULT_TIMEOUT = 30.0 constants from
tempo/intents.py, tempo/_rpc.py, and stripe/intents.py into a single
shared mpp._defaults module.
Pay-per-fortune example demonstrating the full SPT flow:
- FastAPI server with /api/create-spt proxy and /api/fortune gated endpoint
- Headless CLI client using pm_card_visa test card
- Mirrors the mppx examples/stripe/ structure
seller_details[network_id] is only for Stripe Business Network accounts.
Skip it for standard accounts so the SPT endpoint works universally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant