Skip to content

Commit b88a81b

Browse files
committed
docs(readme): add webhook usage examples
1 parent 2a2392d commit b88a81b

1 file changed

Lines changed: 52 additions & 2 deletions

File tree

README.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ A modern, type-safe Python library for the ATH Móvil payment platform.
2222
- Pydantic data validation
2323
- Automatic retries with exponential backoff
2424
- Comprehensive error handling
25-
- High test coverage
2625

2726
## Installation
2827

@@ -67,7 +66,7 @@ print(f"Payment completed: {payment_result.data.reference_number}")
6766

6867
# Refund the payment (requires a client initialized with private token)
6968
refund_result = client.refund_payment(
70-
reference_number=payment_result.data.reference_number,,
69+
reference_number=payment_result.data.reference_number,
7170
amount="5.00",
7271
)
7372
```
@@ -123,6 +122,57 @@ with ATHMovilClient(public_token="token") as client:
123122
# Client is automatically closed when exiting the context
124123
```
125124

125+
## Webhooks
126+
127+
Subscribe to real-time transaction notifications:
128+
129+
```python
130+
# Subscribe to webhooks (requires private token)
131+
client = ATHMovilClient(
132+
public_token="your_public_token",
133+
private_token="your_private_token",
134+
)
135+
136+
client.subscribe_webhook(
137+
listener_url="https://yoursite.com/webhook",
138+
payment_received_event=True,
139+
refund_sent_event=True,
140+
ecommerce_payment_received_event=True,
141+
ecommerce_payment_cancelled_event=True,
142+
ecommerce_payment_expired_event=True,
143+
)
144+
```
145+
146+
Parse incoming webhook payloads in your endpoint:
147+
148+
```python
149+
from athm.webhooks import parse_webhook, WebhookEventType, WebhookStatus
150+
151+
@app.post("/webhook")
152+
async def handle_webhook(request: Request):
153+
payload = await request.json()
154+
event = parse_webhook(payload)
155+
156+
if event.status == WebhookStatus.COMPLETED:
157+
if event.transaction_type == WebhookEventType.PAYMENT:
158+
# Standard payment completed
159+
print(f"Payment received: {event.reference_number} for ${event.total}")
160+
elif event.transaction_type == WebhookEventType.ECOMMERCE:
161+
# eCommerce payment completed
162+
print(f"Order {event.ecommerce_id} completed: ${event.total}")
163+
elif event.transaction_type == WebhookEventType.REFUND:
164+
# Refund processed
165+
print(f"Refund sent: {event.reference_number}")
166+
167+
elif event.status == WebhookStatus.CANCELLED:
168+
print(f"Transaction cancelled: {event.ecommerce_id}")
169+
170+
elif event.status == WebhookStatus.EXPIRED:
171+
print(f"Transaction expired: {event.ecommerce_id}")
172+
173+
return {"status": "ok"}
174+
```
175+
126176
## Documentation
127177

128178
- [Full Documentation](https://athm-python.readthedocs.io)

0 commit comments

Comments
 (0)