-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpayment.py
39 lines (35 loc) · 1.37 KB
/
payment.py
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
import json
import http.client
class PaystackTransactionInitializer:
def __init__(self, email, amount, secret_key, ref_id=None, status=None):
self.email = email
self.amount = amount
self.status = status
self.ref_id = ref_id
self.secret_key = secret_key
class Pay(PaystackTransactionInitializer):
def initialize_transaction(self):
url = "/transaction/initialize"
data = {
"email": self.email,
"amount": self.amount
}
headers = {
"Authorization": "Bearer " + self.secret_key,
"Content-Type": "application/json"
}
response = self._http_request("POST", url, headers, json.dumps(data))
# Check for request success
if response.status == 200:
response_data = json.loads(response.read().decode("utf-8")) # Read response data
# Store the generated reference
self.ref_id = response_data['data']['reference']
return response_data
else:
# Handle errors gracefully
print("Error:", response.reason)
return None
def _http_request(self, method, url, headers, body=None):
connection = http.client.HTTPSConnection("api.paystack.co")
connection.request(method, url, body, headers)
return connection.getresponse()