-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_official_credentials.py
More file actions
152 lines (123 loc) Β· 5.56 KB
/
Copy pathtest_official_credentials.py
File metadata and controls
152 lines (123 loc) Β· 5.56 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
#!/usr/bin/env python3
"""
PesaPal API Endpoint Tester
Testing your official Denncathy Enterprises credentials
"""
import requests
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def test_official_credentials():
print("π― Testing Official Denncathy Enterprises PesaPal Credentials")
print("=" * 60)
# Your official credentials from PesaPal
consumer_key = "EUhWl3sqfn0SvLhSxyejGh1Vt/3LAg3s"
consumer_secret = "aa3mvsfopr5kHr/gUIhjw7d8H5c="
print(f"π Consumer Key: {consumer_key}")
print(f"π Consumer Secret: {consumer_secret}")
print()
# Test different endpoint variations
endpoints = [
("Sandbox v3", "https://cybqa.pesapal.com/pesapalv3/api/Auth/RequestToken"),
("Production v3", "https://pay.pesapal.com/v3/api/Auth/RequestToken"),
("Alternative Sandbox", "https://demo.pesapal.com/API/PostPesapalDirectOrderV4"),
("Alternative Production", "https://www.pesapal.com/API/PostPesapalDirectOrderV4"),
("New API Sandbox", "https://cybqa.pesapal.com/pesapalv3/api/Auth/RequestToken"),
("New API Production", "https://pay.pesapal.com/pesapalv3/api/Auth/RequestToken"),
]
for env_name, auth_url in endpoints:
print(f"π§ͺ Testing {env_name}...")
print(f"URL: {auth_url}")
# Standard payload
payload = {
"consumer_key": consumer_key,
"consumer_secret": consumer_secret
}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
try:
response = requests.post(auth_url, json=payload, headers=headers, timeout=15)
print(f"Status Code: {response.status_code}")
# Check if response is JSON
try:
data = response.json()
print(f"JSON Response: {data}")
if response.status_code == 200 and data.get('token'):
print(f"β
{env_name} authentication successful!")
print(f"Token: {data.get('token')[:30]}...")
return env_name, auth_url.replace('/api/Auth/RequestToken', ''), data.get('token')
elif 'error' in data:
error = data.get('error', {})
print(f"β {env_name} error: {error.get('message', error.get('code', 'Unknown'))}")
else:
print(f"β οΈ {env_name} unexpected response")
except ValueError:
# Not JSON response
print(f"Non-JSON Response (first 200 chars): {response.text[:200]}")
if response.status_code == 404:
print(f"β {env_name} endpoint not found")
else:
print(f"β οΈ {env_name} unexpected format")
except requests.exceptions.Timeout:
print(f"β° {env_name} request timed out")
except requests.exceptions.RequestException as e:
print(f"π {env_name} network error: {e}")
except Exception as e:
print(f"β {env_name} unexpected error: {e}")
print("-" * 50)
print("\nβ None of the endpoints worked with your credentials.")
print("\nπ Possible reasons:")
print("1. Credentials need to be activated by PesaPal")
print("2. These are production credentials (not sandbox)")
print("3. API endpoint has changed")
print("4. Account needs additional setup")
print("\nπ Next steps:")
print("1. Contact PesaPal support: developers@pesapal.com")
print("2. Verify account status in PesaPal merchant portal")
print("3. Ask about API v3 endpoint requirements")
return None, None, None
def test_ipn_registration_with_working_endpoint(base_url, token):
"""Test IPN registration if we find a working endpoint"""
print(f"\nπ§ͺ Testing IPN registration with working endpoint...")
# Try different IPN endpoint patterns
ipn_endpoints = [
f"{base_url}/api/URLSetup/RegisterIPN",
f"{base_url}/api/IPN/RegisterIPN",
f"{base_url}/URLSetup/RegisterIPN"
]
for ipn_url in ipn_endpoints:
print(f"π‘ Trying IPN endpoint: {ipn_url}")
payload = {
"url": "https://denncathy.co.ke/payment/ipn",
"ipn_notification_type": "GET"
}
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}'
}
try:
response = requests.post(ipn_url, json=payload, headers=headers, timeout=10)
print(f"IPN Status: {response.status_code}")
print(f"IPN Response: {response.text}")
if response.status_code == 200:
data = response.json()
if data.get('ipn_id'):
print(f"β
IPN registered successfully!")
print(f"IPN ID: {data.get('ipn_id')}")
return data.get('ipn_id')
except Exception as e:
print(f"IPN Error: {e}")
print("-" * 30)
return None
if __name__ == "__main__":
env_name, base_url, token = test_official_credentials()
if token and base_url:
ipn_id = test_ipn_registration_with_working_endpoint(base_url, token)
if ipn_id:
print(f"\nπ SUCCESS!")
print(f"Add this to your .env file:")
print(f"PESAPAL_IPN_ID={ipn_id}")