-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
55 lines (49 loc) · 2.28 KB
/
Copy pathmain.py
File metadata and controls
55 lines (49 loc) · 2.28 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
from paystackpyAPI.transaction import Transaction
from os import getenv
api_key = getenv("PAYSTACK_KEY")
transaction = Transaction(api_key)
# Example: Initialize a transaction
email = 'customer@example.com'
amount = 5000 # Replace with your desired amount
optional_params = {'currency': 'NGN', 'callback_url': 'https://example.com/callback'}
try:
initialization_response = transaction.initialize_transaction(email, amount, **optional_params)
print("Initialization Response:", initialization_response)
# Extract the reference from the initialization response
reference_to_verify = initialization_response["response_from_api"]['data']['reference']
try:
auth_code = initialization_response["response_from_api"]['data']['authorization_url']
auth_code = auth_code.split('/')[-1]
auth_transaction = transaction.charge_authorization(email, amount, auth_code, **optional_params)
print("Authorization Response:", auth_transaction)
except Exception as e:
print("Error authorizing transaction:", str(e))
# Example: Verify the transaction using the obtained reference
try:
verification_response = transaction.verify_transaction(reference_to_verify)
print("Verification Response:", verification_response)
except Exception as e:
print("Error verifying transaction:", str(e))
try:
transactions = transaction.list_transactions()
print("Transactions:", transactions)
except Exception as e:
print("Error listing transactions:", str(e))
try:
transaction_id = verification_response["response_from_api"]['data']['id']
fetch_transaction = transaction.fetch_transaction(transaction_id)
print("Transaction:", fetch_transaction)
except Exception as e:
print("Error fetching transaction:", str(e))
try:
total_transactions = transaction.get_transaction_totals()
print("Total Transactions:", total_transactions)
except Exception as e:
print("Error getting total transactions:", str(e))
try:
export = transaction.export_transactions()
print("export result :", export)
except Exception as e:
print("Error Exporting transactions:", str(e))
except Exception as e:
print("Error initializing transaction:", str(e))