Skip to content

Commit fb2f581

Browse files
committed
added get_payment_status & set_webhook
1 parent a004322 commit fb2f581

File tree

7 files changed

+438
-111
lines changed

7 files changed

+438
-111
lines changed

README.md

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ The library provides a
2626
class to handle payments. You can use handle them manually or use the
2727
`as_tools()` method to create tools for autonomous agents.
2828

29+
### Making Payments
30+
31+
Obtain information about your account and perform payments:
32+
2933
``` python
3034
from fewsats.core import *
3135
```
@@ -49,13 +53,14 @@ fs.payment_methods().json(), fs.balance().json(), fs.me().json()
4953
'exp_month': 12,
5054
'exp_year': 2034,
5155
'is_default': True}],
52-
[{'id': 1, 'balance': 4468, 'currency': 'usd'}],
56+
[{'id': 1, 'balance': 4464, 'currency': 'usd'}],
5357
{'name': 'Pol',
5458
'last_name': 'Alvarez Vecino',
5559
'email': 'pol@fewsats.com',
5660
'billing_info': None,
5761
'id': 1,
58-
'created_at': '2024-08-20T16:13:01.255Z'})
62+
'created_at': '2024-08-20T16:13:01.255Z',
63+
'webhook_url': 'https://example.com/webhook'})
5964

6065
The `pay` method uses the information returned by a [L402
6166
Protocol](https://github.com/l402-protocol/l402?tab=readme-ov-file#402-response-format)
@@ -92,8 +97,8 @@ l402_offer = {
9297
fs.pay_offer(l402_offer).json()
9398
```
9499

95-
{'id': 118,
96-
'created_at': '2025-02-06T13:57:59.586Z',
100+
{'id': 121,
101+
'created_at': '2025-02-10T11:04:48.083Z',
97102
'status': 'success',
98103
'payment_method': 'lightning'}
99104

@@ -109,6 +114,67 @@ requires you to specify the amount you are expecting to pay in cents.
109114
This is done for accounting purposes and convenience, but the amount
110115
paid will be the sats in the invoice.
111116

117+
### Getting Paid
118+
119+
Fewsats also provides methods for receiving payments. You can create
120+
offers for receiving payments as follows.
121+
122+
``` python
123+
# Create offers for receiving payments
124+
offers_data = [{
125+
"offer_id": "offer_example",
126+
"amount": 1,
127+
"currency": "USD",
128+
"description": "Receive payment for your service",
129+
"title": "1 Credit Package",
130+
"payment_methods": ["lightning", "stripe"]
131+
}]
132+
r = fs.create_offers(offers_data)
133+
offers = r.json()
134+
offers
135+
```
136+
137+
{'offers': [{'offer_id': 'offer_example',
138+
'amount': 1,
139+
'currency': 'USD',
140+
'description': 'Receive payment for your service',
141+
'title': '1 Credit Package',
142+
'payment_methods': ['lightning', 'stripe'],
143+
'type': 'one-off'}],
144+
'payment_context_token': 'a175fd73-cb68-4a22-8685-b236eff2f1a0',
145+
'payment_request_url': 'http://localhost:8000/v0/l402/payment-request',
146+
'version': '0.2.2'}
147+
148+
You can check if an offer has been paid using the payment context token
149+
as follows.
150+
151+
``` python
152+
fs.get_payment_status(payment_context_token=offers["payment_context_token"]).json()
153+
```
154+
155+
{'payment_context_token': 'a175fd73-cb68-4a22-8685-b236eff2f1a0',
156+
'status': 'pending',
157+
'offer_id': None,
158+
'paid_at': None,
159+
'amount': None,
160+
'currency': None}
161+
162+
If you prefer to be notified whenever an offer is paid, you can set up a
163+
webhook as follows, and we will call it whenever a payment is made.
164+
165+
``` python
166+
r = fs.set_webhook(webhook_url="https://example.com/webhook")
167+
r.json()
168+
```
169+
170+
{'name': 'Pol',
171+
'last_name': 'Alvarez Vecino',
172+
'email': 'pol@fewsats.com',
173+
'billing_info': None,
174+
'id': 1,
175+
'created_at': '2024-08-20T16:13:01.255Z',
176+
'webhook_url': 'https://example.com/webhook'}
177+
112178
### AI Agent Integration
113179

114180
We will show how to enable your AI assistant to handle payments using

fewsats/_modidx.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
'fewsats.core.Fewsats.as_tools': ('core.html#fewsats.as_tools', 'fewsats/core.py'),
1313
'fewsats.core.Fewsats.balance': ('core.html#fewsats.balance', 'fewsats/core.py'),
1414
'fewsats.core.Fewsats.create_offers': ('core.html#fewsats.create_offers', 'fewsats/core.py'),
15+
'fewsats.core.Fewsats.get_payment_details': ('core.html#fewsats.get_payment_details', 'fewsats/core.py'),
16+
'fewsats.core.Fewsats.get_payment_status': ('core.html#fewsats.get_payment_status', 'fewsats/core.py'),
1517
'fewsats.core.Fewsats.me': ('core.html#fewsats.me', 'fewsats/core.py'),
1618
'fewsats.core.Fewsats.pay_lightning': ('core.html#fewsats.pay_lightning', 'fewsats/core.py'),
1719
'fewsats.core.Fewsats.pay_offer': ('core.html#fewsats.pay_offer', 'fewsats/core.py'),
1820
'fewsats.core.Fewsats.payment_info': ('core.html#fewsats.payment_info', 'fewsats/core.py'),
1921
'fewsats.core.Fewsats.payment_methods': ('core.html#fewsats.payment_methods', 'fewsats/core.py'),
22+
'fewsats.core.Fewsats.set_webhook': ('core.html#fewsats.set_webhook', 'fewsats/core.py'),
2023
'fewsats.core.Fewsats.wait_for_settlement': ('core.html#fewsats.wait_for_settlement', 'fewsats/core.py')},
2124
'fewsats.l402': { 'fewsats.l402.L.starstarmap': ('claudette.html#l.starstarmap', 'fewsats/l402.py'),
2225
'fewsats.l402.Offer': ('claudette.html#offer', 'fewsats/l402.py'),

fewsats/core.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,32 @@ def create_offers(self:Fewsats,
7878

7979
# %% ../nbs/00_core.ipynb 30
8080
@patch
81+
def get_payment_details(self:Fewsats,
82+
payment_request_url:str,
83+
offer_id:str,
84+
payment_method:str,
85+
payment_context_token:str,
86+
) -> dict:
87+
data = {"offer_id": offer_id, "payment_method": payment_method, "payment_context_token": payment_context_token}
88+
return httpx.post(payment_request_url, json=data)
89+
90+
91+
# %% ../nbs/00_core.ipynb 33
92+
@patch
93+
def get_payment_status(self:Fewsats,
94+
payment_context_token:str,
95+
) -> dict:
96+
return self._request("GET", f"v0/l402/payment-status?payment_context_token={payment_context_token}")
97+
98+
# %% ../nbs/00_core.ipynb 35
99+
@patch
100+
def set_webhook(self:Fewsats,
101+
webhook_url:str,
102+
) -> dict:
103+
return self._request("POST", f"v0/users/webhook/set", json={"webhook_url": webhook_url})
104+
105+
# %% ../nbs/00_core.ipynb 38
106+
@patch
81107
def pay_offer(self:Fewsats,
82108
l402_offer: Dict, # a dictionary containing the response of an L402 endpoint
83109
payment_method:str = '', # preferred payment method (optional)
@@ -93,7 +119,7 @@ def pay_offer(self:Fewsats,
93119
data = {"payment_method": payment_method, **l402_offer} if payment_method else l402_offer
94120
return self._request("POST", "v0/l402/purchases/from-offer", json=data)
95121

96-
# %% ../nbs/00_core.ipynb 32
122+
# %% ../nbs/00_core.ipynb 40
97123
@patch
98124
def pay_lightning(self: Fewsats,
99125
invoice: str, # lightning invoice
@@ -109,14 +135,14 @@ def pay_lightning(self: Fewsats,
109135
}
110136
return self._request("POST", "v0/l402/purchases/lightning", json=data)
111137

112-
# %% ../nbs/00_core.ipynb 37
138+
# %% ../nbs/00_core.ipynb 45
113139
@patch
114140
def payment_info(self:Fewsats,
115141
pid:str): # purchase id
116142
"Retrieve the details of a payment."
117143
return self._request("GET", f"v0/l402/purchases/{pid}")
118144

119-
# %% ../nbs/00_core.ipynb 40
145+
# %% ../nbs/00_core.ipynb 48
120146
@patch
121147
def wait_for_settlement(self:Fewsats,
122148
pid:str, # purchase id
@@ -134,7 +160,7 @@ def wait_for_settlement(self:Fewsats,
134160
wait *= 2
135161
raise TimeoutError(f"Payment {pid} did not settle within {max_wait} seconds. Final status: {status}")
136162

137-
# %% ../nbs/00_core.ipynb 43
163+
# %% ../nbs/00_core.ipynb 51
138164
@patch
139165
def as_tools(self:Fewsats):
140166
"Return list of available tools for AI agents"

0 commit comments

Comments
 (0)