Gateways¶
BaseGateway¶
paygraph.gateways.base.BaseGateway
¶
Bases: ABC
Abstract base class for card payment gateways.
Subclass this to implement a custom card gateway. You must implement
execute_spend() and revoke().
Source code in src/paygraph/gateways/base.py
execute_spend(amount_cents, vendor, memo)
abstractmethod
¶
Create a virtual card for the given spend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount_cents
|
int
|
Spend limit in cents. |
required |
vendor
|
str
|
Name of the vendor. |
required |
memo
|
str
|
Justification or memo for the spend. |
required |
Returns:
| Type | Description |
|---|---|
VirtualCard
|
A |
Source code in src/paygraph/gateways/base.py
revoke(gateway_ref)
abstractmethod
¶
Revoke (cancel) a previously issued virtual card.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gateway_ref
|
str
|
The |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the card was successfully revoked, False if not found. |
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_spend(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 |
|---|---|
VirtualCard
|
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
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 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 | |
execute_spend(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 |
|---|---|
VirtualCard
|
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
dataclass
¶
Result of a successful x402 payment.
Attributes:
| Name | Type | Description |
|---|---|---|
url |
str
|
The x402-enabled endpoint URL that was called. |
amount_cents |
int
|
Amount paid in cents. |
network |
str
|
Blockchain network identifier (e.g. |
transaction_hash |
str
|
On-chain transaction hash. |
payer |
str
|
Wallet address of the payer. |
gateway_ref |
str
|
Unique reference (usually the transaction hash). |
gateway_type |
str
|
Always |
status_code |
int
|
HTTP status code of the response. |
response_body |
str
|
Body of the HTTP response from the paid endpoint. |
content_type |
str
|
Content-Type header of the response. |
Source code in src/paygraph/gateways/x402.py
X402Gateway¶
paygraph.gateways.x402.X402Gateway
¶
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
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 | |
execute_x402(url, amount_cents, vendor, memo, method='GET', headers=None, body=None)
¶
Make a paid HTTP request via the x402 protocol.
Synchronous wrapper around the async x402 client. Sends the request, handles the 402 payment challenge, and returns the final response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The x402-enabled endpoint URL. |
required |
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 |
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 |
|---|---|
X402Receipt
|
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
¶
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_x402(url, amount_cents, vendor, memo, method='GET', headers=None, body=None)
¶
Simulate an x402 payment, optionally prompting for approval.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The endpoint URL. |
required |
amount_cents
|
int
|
Payment amount in cents. |
required |
vendor
|
str
|
Name of the vendor. |
required |
memo
|
str
|
Justification for the payment. |
required |
method
|
str
|
HTTP method (ignored in mock). |
'GET'
|
headers
|
dict | None
|
Optional headers (ignored in mock). |
None
|
body
|
str | None
|
Optional body (ignored in mock). |
None
|
Returns:
| Type | Description |
|---|---|
X402Receipt
|
An |
X402Receipt
|
configured response body. |
Raises:
| Type | Description |
|---|---|
SpendDeniedError
|
If the human denies the approval prompt. |