solvapay-python 0.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- solvapay_python-0.6.0/.github/workflows/ci.yml +24 -0
- solvapay_python-0.6.0/.github/workflows/publish.yml +15 -0
- solvapay_python-0.6.0/.gitignore +14 -0
- solvapay_python-0.6.0/.python-version +1 -0
- solvapay_python-0.6.0/LICENSE +21 -0
- solvapay_python-0.6.0/PKG-INFO +268 -0
- solvapay_python-0.6.0/README.md +240 -0
- solvapay_python-0.6.0/examples/fastmcp-paywall/.env.example +5 -0
- solvapay_python-0.6.0/examples/fastmcp-paywall/.gitignore +4 -0
- solvapay_python-0.6.0/examples/fastmcp-paywall/README.md +71 -0
- solvapay_python-0.6.0/examples/fastmcp-paywall/claim.py +38 -0
- solvapay_python-0.6.0/examples/fastmcp-paywall/pyproject.toml +14 -0
- solvapay_python-0.6.0/examples/fastmcp-paywall/server.py +107 -0
- solvapay_python-0.6.0/examples/fastmcp-paywall/uv.lock +1537 -0
- solvapay_python-0.6.0/examples/langchain-paywall/.env.example +8 -0
- solvapay_python-0.6.0/examples/langchain-paywall/.gitignore +4 -0
- solvapay_python-0.6.0/examples/langchain-paywall/README.md +46 -0
- solvapay_python-0.6.0/examples/langchain-paywall/agent.py +74 -0
- solvapay_python-0.6.0/examples/langchain-paywall/pyproject.toml +13 -0
- solvapay_python-0.6.0/pyproject.toml +68 -0
- solvapay_python-0.6.0/src/solvapay/__init__.py +57 -0
- solvapay_python-0.6.0/src/solvapay/_async_client.py +355 -0
- solvapay_python-0.6.0/src/solvapay/_config.py +22 -0
- solvapay_python-0.6.0/src/solvapay/_http.py +107 -0
- solvapay_python-0.6.0/src/solvapay/client.py +352 -0
- solvapay_python-0.6.0/src/solvapay/events.py +90 -0
- solvapay_python-0.6.0/src/solvapay/exceptions.py +16 -0
- solvapay_python-0.6.0/src/solvapay/fastapi.py +65 -0
- solvapay_python-0.6.0/src/solvapay/langchain.py +67 -0
- solvapay_python-0.6.0/src/solvapay/models.py +182 -0
- solvapay_python-0.6.0/src/solvapay/paywall.py +130 -0
- solvapay_python-0.6.0/src/solvapay/paywall_state.py +121 -0
- solvapay_python-0.6.0/src/solvapay/webhooks.py +86 -0
- solvapay_python-0.6.0/tests/__init__.py +0 -0
- solvapay_python-0.6.0/tests/conftest.py +17 -0
- solvapay_python-0.6.0/tests/test_admin.py +281 -0
- solvapay_python-0.6.0/tests/test_async_client.py +191 -0
- solvapay_python-0.6.0/tests/test_checkout.py +51 -0
- solvapay_python-0.6.0/tests/test_config.py +37 -0
- solvapay_python-0.6.0/tests/test_customer.py +108 -0
- solvapay_python-0.6.0/tests/test_http.py +21 -0
- solvapay_python-0.6.0/tests/test_invariants.py +184 -0
- solvapay_python-0.6.0/tests/test_langchain.py +76 -0
- solvapay_python-0.6.0/tests/test_lifecycle.py +170 -0
- solvapay_python-0.6.0/tests/test_limits.py +58 -0
- solvapay_python-0.6.0/tests/test_paywall.py +89 -0
- solvapay_python-0.6.0/tests/test_paywall_state.py +159 -0
- solvapay_python-0.6.0/tests/test_webhook_events.py +121 -0
- solvapay_python-0.6.0/tests/test_webhooks.py +92 -0
- solvapay_python-0.6.0/uv.lock +1561 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: ci
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
matrix:
|
|
13
|
+
python-version: ["3.10", "3.11", "3.12"]
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: astral-sh/setup-uv@v3
|
|
17
|
+
with:
|
|
18
|
+
enable-cache: true
|
|
19
|
+
- name: Set Python ${{ matrix.python-version }}
|
|
20
|
+
run: uv python install ${{ matrix.python-version }}
|
|
21
|
+
- run: uv sync --all-extras --dev
|
|
22
|
+
- run: uv run ruff check src tests
|
|
23
|
+
- run: uv run mypy src
|
|
24
|
+
- run: uv run pytest -v
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
name: publish
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
tags: ["v*"]
|
|
5
|
+
jobs:
|
|
6
|
+
publish:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
environment: pypi
|
|
9
|
+
permissions:
|
|
10
|
+
id-token: write
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
- uses: astral-sh/setup-uv@v3
|
|
14
|
+
- run: uv build
|
|
15
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.12
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dhruv Sanan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: solvapay-python
|
|
3
|
+
Version: 0.6.0
|
|
4
|
+
Summary: Community Python SDK for SolvaPay (agent-native payment rails)
|
|
5
|
+
Project-URL: Homepage, https://github.com/dhruv-sanan/solvapay-python
|
|
6
|
+
Project-URL: Issues, https://github.com/dhruv-sanan/solvapay-python/issues
|
|
7
|
+
Project-URL: Official TS SDK, https://github.com/solvapay/solvapay-sdk
|
|
8
|
+
Author: Dhruv Sanan
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: agents,fintech,mcp,payments,solvapay
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Requires-Dist: httpx>=0.27
|
|
22
|
+
Requires-Dist: pydantic>=2.5
|
|
23
|
+
Provides-Extra: fastapi
|
|
24
|
+
Requires-Dist: fastapi>=0.110; extra == 'fastapi'
|
|
25
|
+
Provides-Extra: langchain
|
|
26
|
+
Requires-Dist: langchain-core<0.4,>=0.3; extra == 'langchain'
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# solvapay-python
|
|
30
|
+
|
|
31
|
+
Community Python SDK for [SolvaPay](https://solvapay.com) — payment rails for the agentic economy.
|
|
32
|
+
|
|
33
|
+
> **Status:** v0.5, community-maintained. Pending official adoption.
|
|
34
|
+
> Mirrors the most-used surface of [@solvapay/core](https://github.com/solvapay/solvapay-sdk).
|
|
35
|
+
|
|
36
|
+
Python is the dominant language for agent frameworks (LangChain, FastMCP, CrewAI, AutoGen). SolvaPay's official SDK is TypeScript-only. This SDK brings first-class Python support so agent developers can gate tools behind paywalls without switching ecosystems.
|
|
37
|
+
|
|
38
|
+
> 🎬 **New in v0.5:** Paywall state classifier (`paywall_state` module) and LangChain `monetize_tool` decorator — gate any LangChain tool behind a SolvaPay paywall with one line.
|
|
39
|
+
> **v0.4:** Async client (`AsyncSolvaPay`), lifecycle ops, typed webhook events.
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install git+https://github.com/dhruv-sanan/solvapay-python
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Quickstart
|
|
48
|
+
|
|
49
|
+
**Sync:**
|
|
50
|
+
```python
|
|
51
|
+
from solvapay import SolvaPay
|
|
52
|
+
|
|
53
|
+
sv = SolvaPay() # reads SOLVAPAY_SECRET_KEY from env
|
|
54
|
+
|
|
55
|
+
customer_ref = sv.ensure_customer("user_42", email="alice@example.com")
|
|
56
|
+
limits = sv.check_limits(customer_ref=customer_ref, product_ref="prd_0QKI8NHF")
|
|
57
|
+
if not limits.within_limits:
|
|
58
|
+
print("Upgrade needed:", limits.checkout_url)
|
|
59
|
+
|
|
60
|
+
session = sv.create_checkout_session(
|
|
61
|
+
customer_ref=customer_ref,
|
|
62
|
+
product_ref="prd_0QKI8NHF",
|
|
63
|
+
return_url="https://your-app.com/done",
|
|
64
|
+
)
|
|
65
|
+
print(session.checkout_url)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**Async:**
|
|
69
|
+
```python
|
|
70
|
+
import asyncio
|
|
71
|
+
from solvapay import AsyncSolvaPay
|
|
72
|
+
|
|
73
|
+
async def main() -> None:
|
|
74
|
+
async with AsyncSolvaPay() as sv:
|
|
75
|
+
customer_ref = await sv.ensure_customer("user_42", email="alice@example.com")
|
|
76
|
+
limits = await sv.check_limits(customer_ref=customer_ref, product_ref="prd_0QKI8NHF")
|
|
77
|
+
if not limits.within_limits:
|
|
78
|
+
print("Upgrade needed:", limits.checkout_url)
|
|
79
|
+
return
|
|
80
|
+
session = await sv.create_checkout_session(
|
|
81
|
+
customer_ref=customer_ref,
|
|
82
|
+
product_ref="prd_0QKI8NHF",
|
|
83
|
+
)
|
|
84
|
+
print(session.checkout_url)
|
|
85
|
+
|
|
86
|
+
asyncio.run(main())
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Ecosystem integrations
|
|
90
|
+
|
|
91
|
+
### LangChain
|
|
92
|
+
|
|
93
|
+
Gate any LangChain tool with `monetize_tool`:
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from solvapay.langchain import monetize_tool
|
|
97
|
+
from langchain_core.tools import Tool
|
|
98
|
+
|
|
99
|
+
raw = Tool.from_function(name="search", func=do_search, description="Search the web.")
|
|
100
|
+
paid = monetize_tool(raw, product="prd_0QKI8NHF")
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
When the customer is over-limit the tool returns a structured dict with `checkout_url` — the agent surfaces it to the user instead of raising an exception.
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
pip install solvapay-python[langchain]
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
See [`examples/langchain-paywall/`](examples/langchain-paywall/) for a full agent example.
|
|
110
|
+
|
|
111
|
+
### FastMCP
|
|
112
|
+
|
|
113
|
+
See [`examples/fastmcp-paywall/`](examples/fastmcp-paywall/) for a FastMCP server with two paywalled tools, ready to plug into Claude Desktop.
|
|
114
|
+
|
|
115
|
+
### FastAPI
|
|
116
|
+
|
|
117
|
+
Use `webhook_router` to mount a verified webhook endpoint:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from solvapay.fastapi import webhook_router
|
|
121
|
+
app.include_router(webhook_router(secret=os.environ["SOLVAPAY_WEBHOOK_SECRET"], on_event=handle))
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
pip install solvapay-python[fastapi]
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Paywall state classifier
|
|
129
|
+
|
|
130
|
+
`solvapay.paywall_state` maps a `LimitResponse` to a structured recovery action:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from solvapay.paywall_state import decide
|
|
134
|
+
|
|
135
|
+
limits = sv.check_limits(customer_ref="cus_123", product_ref="prd_xyz")
|
|
136
|
+
if not limits.within_limits:
|
|
137
|
+
d = decide(limits)
|
|
138
|
+
print(d.state) # PaywallState.UPGRADE_REQUIRED
|
|
139
|
+
print(d.message) # "You don't have an active plan..."
|
|
140
|
+
print(d.recovery_tool) # "upgrade"
|
|
141
|
+
print(d.checkout_url) # "https://solvapay.com/c/..."
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Examples
|
|
145
|
+
|
|
146
|
+
| Path | What it shows |
|
|
147
|
+
|---|---|
|
|
148
|
+
| [`examples/fastmcp-paywall/`](examples/fastmcp-paywall/) | FastMCP server with two paywalled tools. Demo for `@paywall.require` + MCP. |
|
|
149
|
+
| [`examples/langchain-paywall/`](examples/langchain-paywall/) | LangChain agent with `monetize_tool`. Shows paywall response in agent trace. |
|
|
150
|
+
|
|
151
|
+
## TS ↔ Python parity
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
// TypeScript (@solvapay/core)
|
|
155
|
+
const sv = createSolvaPay();
|
|
156
|
+
const session = await sv.createCheckoutSession({
|
|
157
|
+
customerRef: "cus_123",
|
|
158
|
+
productRef: "prd_0QKI8NHF",
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
# Python (solvapay-python)
|
|
164
|
+
sv = SolvaPay()
|
|
165
|
+
session = sv.create_checkout_session(
|
|
166
|
+
customer_ref="cus_123",
|
|
167
|
+
product_ref="prd_0QKI8NHF",
|
|
168
|
+
)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Supported methods
|
|
172
|
+
|
|
173
|
+
**Core:**
|
|
174
|
+
|
|
175
|
+
| Python | TypeScript equivalent | Description |
|
|
176
|
+
|---|---|---|
|
|
177
|
+
| `create_checkout_session` | `createCheckoutSession` | Hosted checkout URL |
|
|
178
|
+
| `ensure_customer` | `ensureCustomer` | Idempotent customer upsert |
|
|
179
|
+
| `get_customer` | `getCustomer` | Fetch customer by ref / email |
|
|
180
|
+
| `check_limits` | `checkLimits` | Usage / purchase limit check |
|
|
181
|
+
| `verify_webhook` | `verifyWebhook` | HMAC-SHA256 signature verification |
|
|
182
|
+
|
|
183
|
+
**Lifecycle (new in v0.4):**
|
|
184
|
+
|
|
185
|
+
| Python | Verb + path | Description |
|
|
186
|
+
|---|---|---|
|
|
187
|
+
| `track_usage` | `POST /v1/sdk/usages` | Record metered usage |
|
|
188
|
+
| `update_customer` | `PATCH /v1/sdk/customers/{ref}` | Update customer email / name |
|
|
189
|
+
| `get_customer_balance` | `GET /v1/sdk/customers/{ref}/balance` | Credit balance |
|
|
190
|
+
| `cancel_purchase` | `POST /v1/sdk/purchases/{ref}/cancel` | Cancel a subscription |
|
|
191
|
+
| `reactivate_purchase` | `POST /v1/sdk/purchases/{ref}/reactivate` | Reactivate cancelled purchase |
|
|
192
|
+
|
|
193
|
+
All methods available on both `SolvaPay` (sync) and `AsyncSolvaPay` (async).
|
|
194
|
+
|
|
195
|
+
## Webhook handler (FastAPI)
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
from fastapi import FastAPI, HTTPException, Request
|
|
199
|
+
from solvapay import SolvaPayError
|
|
200
|
+
from solvapay.webhooks import verify_webhook
|
|
201
|
+
import os
|
|
202
|
+
|
|
203
|
+
app = FastAPI()
|
|
204
|
+
|
|
205
|
+
@app.post("/webhooks/solvapay")
|
|
206
|
+
async def handle_webhook(request: Request) -> dict:
|
|
207
|
+
body = (await request.body()).decode()
|
|
208
|
+
sig = request.headers.get("sv-signature", "")
|
|
209
|
+
try:
|
|
210
|
+
event = verify_webhook(
|
|
211
|
+
body=body,
|
|
212
|
+
signature=sig,
|
|
213
|
+
secret=os.environ["SOLVAPAY_WEBHOOK_SECRET"],
|
|
214
|
+
)
|
|
215
|
+
except SolvaPayError as exc:
|
|
216
|
+
raise HTTPException(401, str(exc))
|
|
217
|
+
# Option A: dict (default)
|
|
218
|
+
if event["type"] == "purchase.created":
|
|
219
|
+
... # grant access
|
|
220
|
+
|
|
221
|
+
# Option B: typed discriminated union (new in v0.4)
|
|
222
|
+
from solvapay import WebhookEvent, PurchaseCreated
|
|
223
|
+
from pydantic import TypeAdapter
|
|
224
|
+
typed = TypeAdapter(WebhookEvent).validate_python(event)
|
|
225
|
+
if isinstance(typed, PurchaseCreated):
|
|
226
|
+
... # typed access to typed.data, typed.id, etc.
|
|
227
|
+
|
|
228
|
+
return {"received": True}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
> **Important:** use `await request.body()` (raw bytes), not `await request.json()`.
|
|
232
|
+
> Re-serialising JSON changes whitespace and breaks the HMAC signature.
|
|
233
|
+
|
|
234
|
+
## Environment variables
|
|
235
|
+
|
|
236
|
+
| Variable | Purpose |
|
|
237
|
+
|---|---|
|
|
238
|
+
| `SOLVAPAY_SECRET_KEY` | API secret key (required) |
|
|
239
|
+
| `SOLVAPAY_API_BASE_URL` | Override API base URL (optional) |
|
|
240
|
+
| `SOLVAPAY_WEBHOOK_SECRET` | Webhook signing secret (required for `verify_webhook`) |
|
|
241
|
+
|
|
242
|
+
## Non-features
|
|
243
|
+
|
|
244
|
+
- **No retries** — add your own retry logic or use `tenacity`
|
|
245
|
+
- **No pagination** — not needed for current endpoints
|
|
246
|
+
|
|
247
|
+
## Roadmap
|
|
248
|
+
|
|
249
|
+
- v0.1 — sync client, hosted checkout, customers, limits, webhooks ✅
|
|
250
|
+
- v0.2 — `@paywall.require` decorator, FastAPI webhook router ✅
|
|
251
|
+
- v0.3 — FastMCP paywall demo (`examples/fastmcp-paywall/`) ✅
|
|
252
|
+
- v0.4 — async client (`AsyncSolvaPay`), lifecycle ops, typed webhook events ✅
|
|
253
|
+
- v0.5 — paywall state classifier, LangChain `monetize_tool` decorator ✅
|
|
254
|
+
|
|
255
|
+
## Contributing
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
git clone https://github.com/dhruv-sanan/solvapay-python
|
|
259
|
+
cd solvapay-python
|
|
260
|
+
uv sync
|
|
261
|
+
uv run pytest
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Open a PR — all contributions welcome.
|
|
265
|
+
|
|
266
|
+
## License
|
|
267
|
+
|
|
268
|
+
MIT
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# solvapay-python
|
|
2
|
+
|
|
3
|
+
Community Python SDK for [SolvaPay](https://solvapay.com) — payment rails for the agentic economy.
|
|
4
|
+
|
|
5
|
+
> **Status:** v0.5, community-maintained. Pending official adoption.
|
|
6
|
+
> Mirrors the most-used surface of [@solvapay/core](https://github.com/solvapay/solvapay-sdk).
|
|
7
|
+
|
|
8
|
+
Python is the dominant language for agent frameworks (LangChain, FastMCP, CrewAI, AutoGen). SolvaPay's official SDK is TypeScript-only. This SDK brings first-class Python support so agent developers can gate tools behind paywalls without switching ecosystems.
|
|
9
|
+
|
|
10
|
+
> 🎬 **New in v0.5:** Paywall state classifier (`paywall_state` module) and LangChain `monetize_tool` decorator — gate any LangChain tool behind a SolvaPay paywall with one line.
|
|
11
|
+
> **v0.4:** Async client (`AsyncSolvaPay`), lifecycle ops, typed webhook events.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install git+https://github.com/dhruv-sanan/solvapay-python
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quickstart
|
|
20
|
+
|
|
21
|
+
**Sync:**
|
|
22
|
+
```python
|
|
23
|
+
from solvapay import SolvaPay
|
|
24
|
+
|
|
25
|
+
sv = SolvaPay() # reads SOLVAPAY_SECRET_KEY from env
|
|
26
|
+
|
|
27
|
+
customer_ref = sv.ensure_customer("user_42", email="alice@example.com")
|
|
28
|
+
limits = sv.check_limits(customer_ref=customer_ref, product_ref="prd_0QKI8NHF")
|
|
29
|
+
if not limits.within_limits:
|
|
30
|
+
print("Upgrade needed:", limits.checkout_url)
|
|
31
|
+
|
|
32
|
+
session = sv.create_checkout_session(
|
|
33
|
+
customer_ref=customer_ref,
|
|
34
|
+
product_ref="prd_0QKI8NHF",
|
|
35
|
+
return_url="https://your-app.com/done",
|
|
36
|
+
)
|
|
37
|
+
print(session.checkout_url)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Async:**
|
|
41
|
+
```python
|
|
42
|
+
import asyncio
|
|
43
|
+
from solvapay import AsyncSolvaPay
|
|
44
|
+
|
|
45
|
+
async def main() -> None:
|
|
46
|
+
async with AsyncSolvaPay() as sv:
|
|
47
|
+
customer_ref = await sv.ensure_customer("user_42", email="alice@example.com")
|
|
48
|
+
limits = await sv.check_limits(customer_ref=customer_ref, product_ref="prd_0QKI8NHF")
|
|
49
|
+
if not limits.within_limits:
|
|
50
|
+
print("Upgrade needed:", limits.checkout_url)
|
|
51
|
+
return
|
|
52
|
+
session = await sv.create_checkout_session(
|
|
53
|
+
customer_ref=customer_ref,
|
|
54
|
+
product_ref="prd_0QKI8NHF",
|
|
55
|
+
)
|
|
56
|
+
print(session.checkout_url)
|
|
57
|
+
|
|
58
|
+
asyncio.run(main())
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Ecosystem integrations
|
|
62
|
+
|
|
63
|
+
### LangChain
|
|
64
|
+
|
|
65
|
+
Gate any LangChain tool with `monetize_tool`:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from solvapay.langchain import monetize_tool
|
|
69
|
+
from langchain_core.tools import Tool
|
|
70
|
+
|
|
71
|
+
raw = Tool.from_function(name="search", func=do_search, description="Search the web.")
|
|
72
|
+
paid = monetize_tool(raw, product="prd_0QKI8NHF")
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
When the customer is over-limit the tool returns a structured dict with `checkout_url` — the agent surfaces it to the user instead of raising an exception.
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
pip install solvapay-python[langchain]
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
See [`examples/langchain-paywall/`](examples/langchain-paywall/) for a full agent example.
|
|
82
|
+
|
|
83
|
+
### FastMCP
|
|
84
|
+
|
|
85
|
+
See [`examples/fastmcp-paywall/`](examples/fastmcp-paywall/) for a FastMCP server with two paywalled tools, ready to plug into Claude Desktop.
|
|
86
|
+
|
|
87
|
+
### FastAPI
|
|
88
|
+
|
|
89
|
+
Use `webhook_router` to mount a verified webhook endpoint:
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from solvapay.fastapi import webhook_router
|
|
93
|
+
app.include_router(webhook_router(secret=os.environ["SOLVAPAY_WEBHOOK_SECRET"], on_event=handle))
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
pip install solvapay-python[fastapi]
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Paywall state classifier
|
|
101
|
+
|
|
102
|
+
`solvapay.paywall_state` maps a `LimitResponse` to a structured recovery action:
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from solvapay.paywall_state import decide
|
|
106
|
+
|
|
107
|
+
limits = sv.check_limits(customer_ref="cus_123", product_ref="prd_xyz")
|
|
108
|
+
if not limits.within_limits:
|
|
109
|
+
d = decide(limits)
|
|
110
|
+
print(d.state) # PaywallState.UPGRADE_REQUIRED
|
|
111
|
+
print(d.message) # "You don't have an active plan..."
|
|
112
|
+
print(d.recovery_tool) # "upgrade"
|
|
113
|
+
print(d.checkout_url) # "https://solvapay.com/c/..."
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Examples
|
|
117
|
+
|
|
118
|
+
| Path | What it shows |
|
|
119
|
+
|---|---|
|
|
120
|
+
| [`examples/fastmcp-paywall/`](examples/fastmcp-paywall/) | FastMCP server with two paywalled tools. Demo for `@paywall.require` + MCP. |
|
|
121
|
+
| [`examples/langchain-paywall/`](examples/langchain-paywall/) | LangChain agent with `monetize_tool`. Shows paywall response in agent trace. |
|
|
122
|
+
|
|
123
|
+
## TS ↔ Python parity
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
// TypeScript (@solvapay/core)
|
|
127
|
+
const sv = createSolvaPay();
|
|
128
|
+
const session = await sv.createCheckoutSession({
|
|
129
|
+
customerRef: "cus_123",
|
|
130
|
+
productRef: "prd_0QKI8NHF",
|
|
131
|
+
});
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
# Python (solvapay-python)
|
|
136
|
+
sv = SolvaPay()
|
|
137
|
+
session = sv.create_checkout_session(
|
|
138
|
+
customer_ref="cus_123",
|
|
139
|
+
product_ref="prd_0QKI8NHF",
|
|
140
|
+
)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Supported methods
|
|
144
|
+
|
|
145
|
+
**Core:**
|
|
146
|
+
|
|
147
|
+
| Python | TypeScript equivalent | Description |
|
|
148
|
+
|---|---|---|
|
|
149
|
+
| `create_checkout_session` | `createCheckoutSession` | Hosted checkout URL |
|
|
150
|
+
| `ensure_customer` | `ensureCustomer` | Idempotent customer upsert |
|
|
151
|
+
| `get_customer` | `getCustomer` | Fetch customer by ref / email |
|
|
152
|
+
| `check_limits` | `checkLimits` | Usage / purchase limit check |
|
|
153
|
+
| `verify_webhook` | `verifyWebhook` | HMAC-SHA256 signature verification |
|
|
154
|
+
|
|
155
|
+
**Lifecycle (new in v0.4):**
|
|
156
|
+
|
|
157
|
+
| Python | Verb + path | Description |
|
|
158
|
+
|---|---|---|
|
|
159
|
+
| `track_usage` | `POST /v1/sdk/usages` | Record metered usage |
|
|
160
|
+
| `update_customer` | `PATCH /v1/sdk/customers/{ref}` | Update customer email / name |
|
|
161
|
+
| `get_customer_balance` | `GET /v1/sdk/customers/{ref}/balance` | Credit balance |
|
|
162
|
+
| `cancel_purchase` | `POST /v1/sdk/purchases/{ref}/cancel` | Cancel a subscription |
|
|
163
|
+
| `reactivate_purchase` | `POST /v1/sdk/purchases/{ref}/reactivate` | Reactivate cancelled purchase |
|
|
164
|
+
|
|
165
|
+
All methods available on both `SolvaPay` (sync) and `AsyncSolvaPay` (async).
|
|
166
|
+
|
|
167
|
+
## Webhook handler (FastAPI)
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
from fastapi import FastAPI, HTTPException, Request
|
|
171
|
+
from solvapay import SolvaPayError
|
|
172
|
+
from solvapay.webhooks import verify_webhook
|
|
173
|
+
import os
|
|
174
|
+
|
|
175
|
+
app = FastAPI()
|
|
176
|
+
|
|
177
|
+
@app.post("/webhooks/solvapay")
|
|
178
|
+
async def handle_webhook(request: Request) -> dict:
|
|
179
|
+
body = (await request.body()).decode()
|
|
180
|
+
sig = request.headers.get("sv-signature", "")
|
|
181
|
+
try:
|
|
182
|
+
event = verify_webhook(
|
|
183
|
+
body=body,
|
|
184
|
+
signature=sig,
|
|
185
|
+
secret=os.environ["SOLVAPAY_WEBHOOK_SECRET"],
|
|
186
|
+
)
|
|
187
|
+
except SolvaPayError as exc:
|
|
188
|
+
raise HTTPException(401, str(exc))
|
|
189
|
+
# Option A: dict (default)
|
|
190
|
+
if event["type"] == "purchase.created":
|
|
191
|
+
... # grant access
|
|
192
|
+
|
|
193
|
+
# Option B: typed discriminated union (new in v0.4)
|
|
194
|
+
from solvapay import WebhookEvent, PurchaseCreated
|
|
195
|
+
from pydantic import TypeAdapter
|
|
196
|
+
typed = TypeAdapter(WebhookEvent).validate_python(event)
|
|
197
|
+
if isinstance(typed, PurchaseCreated):
|
|
198
|
+
... # typed access to typed.data, typed.id, etc.
|
|
199
|
+
|
|
200
|
+
return {"received": True}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
> **Important:** use `await request.body()` (raw bytes), not `await request.json()`.
|
|
204
|
+
> Re-serialising JSON changes whitespace and breaks the HMAC signature.
|
|
205
|
+
|
|
206
|
+
## Environment variables
|
|
207
|
+
|
|
208
|
+
| Variable | Purpose |
|
|
209
|
+
|---|---|
|
|
210
|
+
| `SOLVAPAY_SECRET_KEY` | API secret key (required) |
|
|
211
|
+
| `SOLVAPAY_API_BASE_URL` | Override API base URL (optional) |
|
|
212
|
+
| `SOLVAPAY_WEBHOOK_SECRET` | Webhook signing secret (required for `verify_webhook`) |
|
|
213
|
+
|
|
214
|
+
## Non-features
|
|
215
|
+
|
|
216
|
+
- **No retries** — add your own retry logic or use `tenacity`
|
|
217
|
+
- **No pagination** — not needed for current endpoints
|
|
218
|
+
|
|
219
|
+
## Roadmap
|
|
220
|
+
|
|
221
|
+
- v0.1 — sync client, hosted checkout, customers, limits, webhooks ✅
|
|
222
|
+
- v0.2 — `@paywall.require` decorator, FastAPI webhook router ✅
|
|
223
|
+
- v0.3 — FastMCP paywall demo (`examples/fastmcp-paywall/`) ✅
|
|
224
|
+
- v0.4 — async client (`AsyncSolvaPay`), lifecycle ops, typed webhook events ✅
|
|
225
|
+
- v0.5 — paywall state classifier, LangChain `monetize_tool` decorator ✅
|
|
226
|
+
|
|
227
|
+
## Contributing
|
|
228
|
+
|
|
229
|
+
```bash
|
|
230
|
+
git clone https://github.com/dhruv-sanan/solvapay-python
|
|
231
|
+
cd solvapay-python
|
|
232
|
+
uv sync
|
|
233
|
+
uv run pytest
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Open a PR — all contributions welcome.
|
|
237
|
+
|
|
238
|
+
## License
|
|
239
|
+
|
|
240
|
+
MIT
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# FastMCP Paywall Demo
|
|
2
|
+
|
|
3
|
+
A Model Context Protocol (MCP) server with two tools gated behind a SolvaPay paywall, built with [FastMCP](https://github.com/jlowin/fastmcp) and the community [solvapay-python](https://github.com/dhruv-sanan/solvapay-python) SDK.
|
|
4
|
+
|
|
5
|
+
**What this proves:** any Python MCP server can monetize tools per-call by stacking `@paywall.require` over `@mcp.tool()`. No backend changes needed.
|
|
6
|
+
|
|
7
|
+
## Tools
|
|
8
|
+
|
|
9
|
+
| Name | Price | What it does |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| `summarize_url(url)` | $0.02 / call | Fetches a URL and returns a 280-char preview. |
|
|
12
|
+
| `analyze_text(text)` | $0.05 / call | Returns word/char/sentence counts + avg word length. |
|
|
13
|
+
|
|
14
|
+
When the customer's limit is reached, the tool returns:
|
|
15
|
+
```json
|
|
16
|
+
{"paywall_required": true, "checkout_url": "https://solvapay.com/c/sess_..."}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The LLM can render the checkout link directly to the user.
|
|
20
|
+
|
|
21
|
+
## Run in 60 seconds
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
git clone https://github.com/dhruv-sanan/solvapay-python
|
|
25
|
+
cd solvapay-python/examples/fastmcp-paywall
|
|
26
|
+
cp .env.example .env
|
|
27
|
+
# Fill in SOLVAPAY_SECRET_KEY, SOLVAPAY_PRODUCT_REF, SOLVAPAY_DEMO_CUSTOMER_REF
|
|
28
|
+
|
|
29
|
+
uv sync
|
|
30
|
+
uv run python claim.py # smoke test (no Claude Desktop needed)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Expected: two tool responses (or a paywall dict if your demo customer is over their limit).
|
|
34
|
+
|
|
35
|
+
## Plug into Claude Desktop
|
|
36
|
+
|
|
37
|
+
Add to `~/Library/Application Support/Claude/claude_desktop_config.json`:
|
|
38
|
+
```json
|
|
39
|
+
{
|
|
40
|
+
"mcpServers": {
|
|
41
|
+
"solvapay-paywall-demo": {
|
|
42
|
+
"command": "uv",
|
|
43
|
+
"args": ["--directory", "/abs/path/to/examples/fastmcp-paywall", "run", "python", "server.py"],
|
|
44
|
+
"env": {
|
|
45
|
+
"SOLVAPAY_SECRET_KEY": "sk_sandbox_...",
|
|
46
|
+
"SOLVAPAY_PRODUCT_REF": "prd_..."
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Restart Claude Desktop. Ask Claude: *"summarize https://example.com"* — Claude should call the tool, hit the paywall, and surface the checkout URL.
|
|
54
|
+
|
|
55
|
+
## How the gating works
|
|
56
|
+
|
|
57
|
+
`server.py` decorates each tool with `@paywall.require(product=PRODUCT_REF, client=sv)` from the SolvaPay Python SDK. The decorator runs `check_limits` against SolvaPay before each call. On `withinLimits=False`, it raises `PaywallRequired` carrying the hosted-checkout URL.
|
|
58
|
+
|
|
59
|
+
A small `_gated` helper converts the exception into a structured dict so the LLM can render the URL gracefully instead of seeing a stack trace.
|
|
60
|
+
|
|
61
|
+
## Customizing per-tool pricing
|
|
62
|
+
|
|
63
|
+
Pricing is defined in your SolvaPay dashboard (per-product or per-meter). To split the price between the two tools:
|
|
64
|
+
- Create separate products (one per tool) and pass distinct `product=...` to each decorator.
|
|
65
|
+
- OR: keep one product and use SolvaPay metering with different `meter_name` values per tool.
|
|
66
|
+
|
|
67
|
+
> **Production note:** `customer_ref` defaults to an env-var for demo convenience. In production, extract it from the MCP session context (e.g., `mcp.context.session.user_id`) to gate per real user.
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
MIT — same as the main SDK.
|