Gateways¶
BaseGateway¶
paygraph.gateways.base.BaseGateway
¶
Bases: ABC
Abstract base class for all payment gateways.
Subclass this to implement a custom gateway. You must implement
execute(). Override execute_async() for native async support
and revoke() for card-style gateways that support cancellation.
Source code in src/paygraph/gateways/base.py
execute(amount_cents, vendor, memo, **kwargs)
abstractmethod
¶
Execute a spend for the given amount.
Subclasses should declare gateway-specific parameters as explicit
keyword-only arguments rather than consuming **kwargs. This
ensures callers get immediate feedback on typos.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount_cents
|
int
|
Spend amount in cents. |
required |
vendor
|
str
|
Name of the vendor. |
required |
memo
|
str
|
Justification or memo for the spend. |
required |
Returns:
| Type | Description |
|---|---|
SpendResult
|
A |
Source code in src/paygraph/gateways/base.py
execute_async(amount_cents, vendor, memo, **kwargs)
async
¶
Execute a spend asynchronously.
Default implementation runs execute() in a thread pool.
Override for native async support (e.g. x402 gateways).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount_cents
|
int
|
Spend amount in cents. |
required |
vendor
|
str
|
Name of the vendor. |
required |
memo
|
str
|
Justification or memo for the spend. |
required |
Returns:
| Type | Description |
|---|---|
SpendResult
|
A |
Source code in src/paygraph/gateways/base.py
revoke(gateway_ref)
¶
Revoke (cancel) a previously issued spend.
Optional — card gateways override this, x402 gateways typically don't.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gateway_ref
|
str
|
The |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successfully revoked, False if not found. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If this gateway does not support revocation. |
Source code in src/paygraph/gateways/base.py
MockGateway¶
paygraph.gateways.mock.MockGateway
¶
Bases: BaseGateway
Mock card gateway for development and testing.
Generates fake card numbers. When auto_approve is False (default),
prompts for human approval in the terminal before issuing a card.
Source code in src/paygraph/gateways/mock.py
__init__(auto_approve=False)
¶
Initialize the mock gateway.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auto_approve
|
bool
|
If True, skip the terminal approval prompt and approve all requests automatically. |
False
|
Source code in src/paygraph/gateways/mock.py
execute(amount_cents, vendor, memo)
¶
Create a mock virtual card, optionally prompting for approval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount_cents
|
int
|
Spend limit in cents. |
required |
vendor
|
str
|
Name of the vendor. |
required |
memo
|
str
|
Justification for the spend. |
required |
Returns:
| Type | Description |
|---|---|
CardResult
|
A |
Raises:
| Type | Description |
|---|---|
SpendDeniedError
|
If the human denies the approval prompt. |
Source code in src/paygraph/gateways/mock.py
revoke(gateway_ref)
¶
Remove a mock card from the internal store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gateway_ref
|
str
|
The |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the card existed and was removed, False otherwise. |
Source code in src/paygraph/gateways/mock.py
StripeCardGateway¶
paygraph.gateways.stripe.StripeCardGateway
¶
Bases: BaseGateway
Stripe Issuing gateway that creates real virtual cards.
Automatically detects test vs live mode from the API key prefix
(sk_test_ or sk_live_). Creates or reuses a Stripe cardholder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str
|
Stripe secret key (must start with |
required |
cardholder_id
|
str | None
|
Existing Stripe cardholder ID to use. If None, one is created or reused automatically. |
None
|
currency
|
str
|
ISO currency code (default |
'usd'
|
billing_address
|
dict[str, str] | None
|
Cardholder billing address dict. Uses a San Francisco default if not provided. |
None
|
single_use
|
bool
|
If True (default), a new card is created per transaction. If False, reuses the same card and updates the spending limit. |
True
|
allowed_mccs
|
list[str] | None
|
Stripe MCC allowlist applied at the card level. |
None
|
blocked_mccs
|
list[str] | None
|
Stripe MCC blocklist applied at the card level. |
None
|
Source code in src/paygraph/gateways/stripe.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | |
execute(amount_cents, vendor, memo)
¶
Create a Stripe Issuing virtual card with the given spend limit.
Calls the Stripe /v1/issuing/cards API to mint a new card
(or update the existing card's limit if single_use is False).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount_cents
|
int
|
Spend limit in cents. |
required |
vendor
|
str
|
Name of the vendor (stored in card metadata). |
required |
memo
|
str
|
Justification (stored in card metadata, truncated to 500 chars). |
required |
Returns:
| Type | Description |
|---|---|
CardResult
|
A |
Raises:
| Type | Description |
|---|---|
GatewayError
|
If the Stripe API call fails. |
Source code in src/paygraph/gateways/stripe.py
revoke(gateway_ref)
¶
Cancel a Stripe Issuing card.
Sets the card status to "canceled" via the Stripe API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gateway_ref
|
str
|
The Stripe card ID ( |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the card was canceled, False if not found (404). |
Raises:
| Type | Description |
|---|---|
GatewayError
|
If the Stripe API call fails (non-404). |
Source code in src/paygraph/gateways/stripe.py
X402Receipt¶
paygraph.gateways.x402.X402Receipt = X402Result
module-attribute
¶
X402Gateway¶
paygraph.gateways.x402.X402Gateway
¶
Bases: BaseGateway
Gateway for x402 HTTP 402 payments using on-chain USDC.
Supports EVM chains (Base, Ethereum, etc.) and Solana. At least one private key must be provided. The x402 SDK is imported lazily — it only needs to be installed when this gateway is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
evm_private_key
|
str | None
|
Hex-encoded EVM private key (e.g. |
None
|
svm_private_key
|
str | None
|
Base58-encoded Solana private key. |
None
|
Source code in src/paygraph/gateways/x402.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
execute_async(amount_cents, vendor, memo, *, url='', method='GET', headers=None, body=None)
async
¶
Make a paid HTTP request via the x402 protocol (async).
Use this coroutine directly from async contexts such as LangGraph
agents, FastAPI handlers, or Jupyter notebooks where an event loop
is already running. Calling the synchronous :meth:execute
from those contexts will raise RuntimeError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount_cents
|
int
|
Payment amount in cents. |
required |
vendor
|
str
|
Name of the vendor or service. |
required |
memo
|
str
|
Justification or memo for the payment. |
required |
url
|
str
|
The x402-enabled endpoint URL. |
''
|
method
|
str
|
HTTP method (default |
'GET'
|
headers
|
dict | None
|
Optional additional HTTP headers. |
None
|
body
|
str | None
|
Optional request body string. |
None
|
Returns:
| Type | Description |
|---|---|
X402Result
|
An |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the x402 payment fails (still 402 after retry). |
Source code in src/paygraph/gateways/x402.py
execute(amount_cents, vendor, memo, *, url='', method='GET', headers=None, body=None)
¶
Make a paid HTTP request via the x402 protocol (sync).
Synchronous wrapper around :meth:execute_async. Safe to call
from any context — including scripts, CLI, and environments with a
running event loop (LangGraph agents, FastAPI handlers, Jupyter
notebooks). When a loop is already running in the current thread, the
call is automatically dispatched to a worker thread with its own fresh
event loop. For fully-async callers that prefer to avoid the thread
overhead, await gateway.execute_async(...) is also available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount_cents
|
int
|
Payment amount in cents. |
required |
vendor
|
str
|
Name of the vendor or service. |
required |
memo
|
str
|
Justification or memo for the payment. |
required |
url
|
str
|
The x402-enabled endpoint URL. |
''
|
method
|
str
|
HTTP method (default |
'GET'
|
headers
|
dict | None
|
Optional additional HTTP headers. |
None
|
body
|
str | None
|
Optional request body string. |
None
|
Returns:
| Type | Description |
|---|---|
X402Result
|
An |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the x402 payment fails (still 402 after retry). |
Source code in src/paygraph/gateways/x402.py
MockX402Gateway¶
paygraph.gateways.mock_x402.MockX402Gateway
¶
Bases: BaseGateway
Mock x402 gateway for testing without blockchain access.
Simulates the x402 payment flow by generating fake transaction hashes and returning configurable response bodies. Optionally prompts for human approval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auto_approve
|
bool
|
If True, skip the terminal approval prompt. |
False
|
response_body
|
str
|
Canned response body to return. |
'{}'
|
status_code
|
int
|
HTTP status code of the mock response. |
200
|
content_type
|
str
|
Content-Type of the mock response. |
'application/json'
|
Source code in src/paygraph/gateways/mock_x402.py
execute_async(amount_cents, vendor, memo, *, url='', method='GET', headers=None, body=None)
async
¶
Async variant — delegates to :meth:execute.
Source code in src/paygraph/gateways/mock_x402.py
execute(amount_cents, vendor, memo, *, url='', method='GET', headers=None, body=None)
¶
Simulate an x402 payment, optionally prompting for approval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount_cents
|
int
|
Payment amount in cents. |
required |
vendor
|
str
|
Name of the vendor. |
required |
memo
|
str
|
Justification for the payment. |
required |
url
|
str
|
The x402-enabled endpoint URL. |
''
|
method
|
str
|
HTTP method (ignored in mock). |
'GET'
|
headers
|
dict | None
|
Optional HTTP headers (ignored in mock). |
None
|
body
|
str | None
|
Optional request body (ignored in mock). |
None
|
Returns:
| Type | Description |
|---|---|
X402Result
|
An |
X402Result
|
configured response body. |
Raises:
| Type | Description |
|---|---|
SpendDeniedError
|
If the human denies the approval prompt. |