-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathpaystack.py
More file actions
256 lines (219 loc) · 10.7 KB
/
paystack.py
File metadata and controls
256 lines (219 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
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
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
"""
EasySwitch - Paystack Integrator
"""
import hmac
import hashlib
import json
from typing import ClassVar, List, Dict, Optional, Any
from datetime import datetime
from easyswitch.adapters.base import AdaptersRegistry, BaseAdapter
from easyswitch.types import (Currency, PaymentResponse, WebhookEvent,
TransactionDetail,TransactionStatusResponse,
CustomerInfo, TransactionStatus)
from easyswitch.exceptions import PaymentError,UnsupportedOperationError
@AdaptersRegistry.register()
class PaystackAdapter(BaseAdapter):
"""Paystack Adapter for EasySwitch SDK."""
SANDBOX_URL: str = "https://api.paystack.co"
PRODUCTION_URL: str = "https://api.paystack.co"
SUPPORTED_CURRENCIES: ClassVar[List[Currency]] = [
Currency.NGN,
Currency.GHS,
Currency.USD,
]
MIN_AMOUNT: ClassVar[Dict[Currency, float]] = {
Currency.NGN: 50.0, # 50.00 NGN (main units)
Currency.GHS: 0.10, # 0.10 GHS
Currency.USD: 2.0, # 2.00 USD
}
MAX_AMOUNT: ClassVar[Dict[Currency, float]] = {
Currency.NGN: 10_000_000,
Currency.GHS: 10_000_000,
Currency.USD: 10_000_000,
}
def validate_credentials(self) -> bool:
"""Validate the credentials for Paystack."""
return bool(self.config.api_key)
def get_credentials(self):
"""Return API credentials."""
return {"api_key": self.config.api_key}
def get_headers(self, authorization=True, **kwargs) -> Dict[str, str]:
"""Return headers for Paystack requests."""
headers = {"Content-Type": "application/json"}
if authorization:
headers["Authorization"] = f"Bearer {self.config.api_key}"
return headers
def get_normalize_status(self, status: str) -> TransactionStatus:
"""Normalize Paystack transaction status."""
mapping = {
"success": TransactionStatus.SUCCESSFUL,
"failed": TransactionStatus.FAILED,
"abandoned": TransactionStatus.CANCELLED,
"pending": TransactionStatus.PENDING,
"refund": TransactionStatus.REFUNDED,
}
return mapping.get(status.lower(), TransactionStatus.UNKNOWN)
# validate_webhook expects raw_body: bytes
def validate_webhook(self, raw_body: bytes, headers: Dict[str, str]) -> bool:
"""Validate the authenticity of a Paystack webhook."""
signature = headers.get("x-paystack-signature")
secret_key = getattr(self.config, "api_key", None)
if not signature or not secret_key:
return False
computed_sig = hmac.new(secret_key.encode("utf-8"), msg=raw_body, digestmod=hashlib.sha512).hexdigest()
return hmac.compare_digest(computed_sig, signature)
def parse_webhook(self, payload: Dict[str, Any], headers: Dict[str, str]) -> WebhookEvent:
"""Parse and validate a Paystack webhook."""
# Convert payload to bytes for validation
raw_body = json.dumps(payload, separators=(",", ":"), sort_keys=True).encode("utf-8")
if not self.validate_webhook(raw_body, headers):
raise PaymentError("Invalid webhook signature", raw_response=payload)
data = payload.get("data", {})
event_type = payload.get("event", "unknown_event")
transaction_id = data.get("reference")
status = self.get_normalize_status(data.get("status"))
amount = (data.get("amount") or 0) / 100
currency = data.get("currency", "NGN")
metadata = data.get("metadata", {}) or {}
context = {
"customer_email": data.get("customer", {}).get("email"),
"authorization": data.get("authorization", {}),
}
return WebhookEvent(
event_type=event_type,
provider=self.provider_name(),
transaction_id=transaction_id,
status=status,
amount=amount,
currency=currency,
created_at=datetime.fromtimestamp(data.get("createdAt") / 1000) if data.get("createdAt") else None,
raw_data=payload,
metadata=metadata,
context=context,
)
def format_transaction(self, transaction: TransactionDetail) -> Dict[str, Any]:
"""Convert standardized TransactionDetail into Paystack-specific payload."""
self.validate_transaction(transaction)
return {
"amount": int(transaction.amount * 100), # Paystack expects kobo
"email": transaction.customer.email,
"reference": transaction.reference,
"callback_url": transaction.callback_url or self.config.callback_url,
"metadata": transaction.metadata or {},
}
async def send_payment(self, transaction: TransactionDetail) -> PaymentResponse:
"""Send a payment initialization request to Paystack."""
payload = self.format_transaction(transaction)
async with self.get_client() as client:
response = await client.post(
"/transaction/initialize",
json_data=payload,
headers=self.get_headers()
)
data = response.json() if hasattr(response, "json") else response.data
if response.status in range(200, 300) and data.get("status"):
init_data = data.get("data", {})
return PaymentResponse(
transaction_id=transaction.transaction_id,
reference=init_data.get("reference"),
provider=self.provider_name(),
status="pending",
amount=transaction.amount,
currency=transaction.currency,
payment_link=init_data.get("authorization_url"),
transaction_token=init_data.get("access_code"),
metadata=init_data,
raw_response=data,
)
raise PaymentError(
message=f"Payment request failed with status {response.status}",
status_code=response.status,
raw_response=data,
)
async def check_status(self, reference: str) -> TransactionStatusResponse:
"""Check the status of a Paystack transaction by reference."""
async with self.get_client() as client:
response = await client.get(
f"/transaction/verify/{reference}",
headers=self.get_headers()
)
data = response.json() if hasattr(response, "json") else response.data
if not data.get("status"):
raise PaymentError(
message="Failed to verify Paystack transaction",
raw_response=data
)
tx = data.get("data", {})
return TransactionStatusResponse(
transaction_id=tx.get("id"),
provider=self.provider_name(),
status=self.get_normalize_status(tx.get("status")),
amount=(tx.get("amount") or 0) / 100,
data=tx,
)
async def cancel_transaction(self, transaction_id: str) -> None:
"""Paystack does not support transaction cancellation.
Use refund() for post-payment reversals.
"""
raise UnsupportedOperationError(
self.provider_name(),
)
async def refund(self, transaction_id: str, amount: Optional[float] = None) -> PaymentResponse:
"""Refund a Paystack transaction."""
async with self.get_client() as client:
payload = {"transaction": transaction_id}
if amount:
payload["amount"] = int(amount * 100)
response = await client.post("/refund", json_data=payload, headers=self.get_headers())
data = response.json() if hasattr(response, "json") else response.data
if response.status in range(200, 300) and data.get("status"):
refund_data = data.get("data", {})
return PaymentResponse(
transaction_id=transaction_id,
reference=refund_data.get("transaction", {}).get("reference", f"refund-{transaction_id}"),
provider=self.provider_name(),
status=self.get_normalize_status(refund_data.get("status")),
amount=(refund_data.get("amount") or (amount or 0)) / 100,
currency=refund_data.get("currency", "NGN"),
metadata=refund_data,
raw_response=data,
)
raise PaymentError(
message=f"Refund failed with status {response.status}",
status_code=response.status,
raw_response=data,
)
async def get_transaction_detail(self, transaction_id: str) -> TransactionDetail:
"""Retrieve transaction details from Paystack by transaction ID."""
async with self.get_client() as client:
response = await client.get(f"/transaction/{transaction_id}", headers=self.get_headers())
data = response.json() if hasattr(response, "json") else response.data
if response.status in range(200, 300) and data.get("status"):
tx = data.get("data", {})
customer = CustomerInfo(
email=tx.get("customer", {}).get("email"),
phone_number=tx.get("customer", {}).get("phone"),
first_name=tx.get("customer", {}).get("first_name"),
last_name=tx.get("customer", {}).get("last_name"),
metadata=tx.get("customer", {}).get("metadata", {}),
)
return TransactionDetail(
transaction_id=str(tx.get("id", transaction_id)),
provider=self.provider_name(),
amount=(tx.get("amount") or 0) / 100,
currency=tx.get("currency", "NGN"),
status=self.get_normalize_status(tx.get("status")),
reference=tx.get("reference"),
callback_url=tx.get("callback_url"),
created_at=datetime.fromtimestamp(tx.get("createdAt") / 1000) if tx.get("createdAt") else datetime.now(),
updated_at=datetime.fromtimestamp(tx.get("updatedAt") / 1000) if tx.get("updatedAt") else None,
completed_at=datetime.fromtimestamp(tx.get("paidAt") / 1000) if tx.get("paidAt") else None,
customer=customer,
metadata=tx.get("metadata", {}),
raw_data=tx
)
raise PaymentError(
message=f"Failed to retrieve transaction {transaction_id}",
status_code=response.status,
raw_response=data
)