Skip to content

Commit fe585f6

Browse files
committed
Update
0 parents  commit fe585f6

9 files changed

+435
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea/
2+
.vscode/
3+
venv/
4+
*.egg-info/

README.md

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# python-simpay-api
2+
3+
## SMS
4+
### Weryfikacja kodu
5+
```python
6+
from payments.sms import SMS
7+
8+
sms = SMS("API_KEY", "API_SECRET", "SERVICE_ID")
9+
10+
response = sms.verify_code(request={"code": "XXXXXXX", "number": "XXXXX"})
11+
```
12+
13+
### Pobieranie listy usług
14+
```python
15+
from payments.sms import SMS
16+
17+
sms = SMS("API_KEY", "API_SECRET", "SERVICE_ID")
18+
19+
response = sms.get_service_list(request={})
20+
```
21+
22+
## SMS XML
23+
```python
24+
from payments.sms_xml import SMS_XML
25+
26+
code = SMS_XML.generateCode() # Generate code
27+
```
28+
29+
## Direct Billing
30+
### Generowanie transakcji
31+
```python
32+
from payments.direct_billing import DirectBilling
33+
34+
db = DirectBilling("API_KEY", "API_SECRET", True, "SERVICE_ID") # True stands for debug mode
35+
36+
response = db.generate_transaction(api_key="XXXXXXXXXXXX",request={"control": "XXXXXX", "amount": 10.00}) # and others variables
37+
```
38+
39+
### Pobieranie danych o transakcji
40+
```python
41+
from payments.direct_billing import DirectBilling
42+
43+
db = DirectBilling("API_KEY", "API_SECRET", True, "SERVICE_ID") # True stands for debug mode
44+
45+
response = db.get_transaction(request={"id": "TRANSACTION_ID"})
46+
```
47+
48+
### Pobieranie listy usług DCB
49+
```python
50+
from payments.direct_billing import DirectBilling
51+
52+
db = DirectBilling("API_KEY", "API_SECRET", True, "SERVICE_ID") # True stands for debug mode
53+
54+
response = db.get_services(request={})
55+
```
56+
57+
### Pobieranie maksymalnych kwot transakcji
58+
```python
59+
from payments.direct_billing import DirectBilling
60+
61+
db = DirectBilling("API_KEY", "API_SECRET", True, "SERVICE_ID") # True stands for debug mode
62+
63+
response = db.get_transaction_limits(request={})
64+
```
65+
66+
### Pobieranie prowizji dla usługi
67+
```python
68+
from payments.direct_billing import DirectBilling
69+
70+
db = DirectBilling("API_KEY", "API_SECRET", True, "SERVICE_ID") # True stands for debug mode
71+
72+
response = db.get_service_commission(request={})
73+
```
74+
75+
### Pobieranie adresów IP serwerów SimPay
76+
```python
77+
from payments.direct_billing import DirectBilling
78+
79+
ips = DirectBilling.get_servers_ip()
80+
```
81+
82+
### Obliczanie podpisu sign
83+
```python
84+
from payments.direct_billing import DirectBilling
85+
86+
db = DirectBilling("API_KEY", "API_SECRET", True, "SERVICE_ID") # True stands for debug mode
87+
88+
sign = db.sign(123, "status", "valuenet", "valuepartner", "control")
89+
```

payments/direct_billing.py

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import decimal
2+
import hashlib
3+
import requests
4+
5+
6+
class DirectBilling:
7+
def __init__(self, api_key, secret, debug_mode=False, service_id=None):
8+
self.api_key = api_key
9+
self.secret = secret
10+
self.debug_mode = debug_mode
11+
self.service_id = service_id
12+
13+
@staticmethod
14+
def format_number(num):
15+
try:
16+
dec = decimal.Decimal(num)
17+
except[]:
18+
return "BAD_FORMAT"
19+
tup = dec.as_tuple()
20+
delta = len(tup.digits) + tup.exponent
21+
digits = ''.join(str(d) for d in tup.digits)
22+
if delta <= 0:
23+
zeros = abs(tup.exponent) - len(tup.digits)
24+
val = '0.' + ('0' * zeros) + digits
25+
else:
26+
val = digits[:delta] + ('0' * tup.exponent) + '.' + digits[delta:]
27+
val = val.rstrip('0')
28+
if val[-1] == '.':
29+
val = val[:-1]
30+
if tup.sign:
31+
return '-' + val
32+
return val
33+
34+
# https://docs.simpay.pl/#generowanie-transakcji
35+
def generate_transaction(self, api_key, request):
36+
if request.get("serviceId") is None:
37+
request["serviceId"] = self.service_id
38+
39+
amount = ""
40+
41+
if request.get("amount") is not None:
42+
amount = request["amount"]
43+
44+
if request.get("amount_gross") is not None:
45+
amount = request["amount_gross"]
46+
47+
if request.get("amount_required") is not None:
48+
amount = request["amount_required"]
49+
50+
amount = self.format_number(amount)
51+
52+
request["sign"] = hashlib.new('sha256',
53+
bytes(
54+
str(request["serviceId"]) +
55+
str(amount) +
56+
str(request.get("control")) +
57+
str(api_key),
58+
encoding="utf8")
59+
).hexdigest()
60+
61+
r = requests.post(DirectBilling.API_URL, data=request)
62+
63+
return r.json()
64+
65+
# https://docs.simpay.pl/#pobieranie-danych-o-transakcji
66+
def get_transaction(self, request):
67+
if request.get("key") is None:
68+
request["key"] = self.api_key
69+
70+
if request.get("secret") is None:
71+
request["secret"] = self.secret
72+
73+
print(request)
74+
75+
r = requests.post(DirectBilling.TRANSACTION_STATUS_URL, json={"params": request})
76+
77+
return r.json()
78+
79+
# https://docs.simpay.pl/#pobieranie-listy-uslug-dcb
80+
def get_services(self, request):
81+
if request.get("key") is None:
82+
request["key"] = self.api_key
83+
84+
if request.get("secret") is None:
85+
request["secret"] = self.secret
86+
87+
r = requests.post(DirectBilling.SERVICES_LIST_URL, json={"params": request})
88+
89+
return r.json()
90+
91+
# https://docs.simpay.pl/#pobieranie-maksymalnych-kwot-transakcji
92+
def get_transaction_limits(self, request):
93+
if request.get("key") is None:
94+
request["key"] = self.api_key
95+
96+
if request.get("secret") is None:
97+
request["secret"] = self.secret
98+
99+
if request.get("service_id") is None:
100+
request["service_id"] = self.service_id
101+
102+
r = requests.post(DirectBilling.TRANSACTION_LIMITS_URL, json={"params": request})
103+
104+
return r.json()
105+
106+
# https://docs.simpay.pl/#pobieranie-prowizji-dla-uslugi
107+
def get_service_commission(self, request):
108+
if request.get("key") is None:
109+
request["key"] = self.api_key
110+
111+
if request.get("secret") is None:
112+
request["secret"] = self.secret
113+
114+
if request.get("service_id") is None:
115+
request["service_id"] = self.service_id
116+
117+
r = requests.post(DirectBilling.SERVICE_COMMISSION_URL, json={"params": request})
118+
119+
return r.json()
120+
121+
# https://docs.simpay.pl/#lista-ip-serwerow-simpay
122+
@staticmethod
123+
def get_servers_ip():
124+
r = requests.get(DirectBilling.GET_IP_URL)
125+
126+
return r.json()
127+
128+
# https://docs.simpay.pl/#odbieranie-transakcji
129+
def sign(self, id, status, valuenet, valuepartner, control):
130+
return hashlib.new('sha256',
131+
bytes(
132+
str(id) +
133+
str(status) +
134+
str(valuenet) +
135+
str(valuepartner) +
136+
str(control) +
137+
str(self.api_key),
138+
encoding="utf8")
139+
).hexdigest()
140+
141+
142+
DirectBilling.API_URL = "https://simpay.pl/db/api"
143+
DirectBilling.TRANSACTION_STATUS_URL = "https://simpay.pl/api/db_status"
144+
DirectBilling.SERVICES_LIST_URL = "https://simpay.pl/api/get_services_db"
145+
DirectBilling.TRANSACTION_LIMITS_URL = "https://simpay.pl/api/db_hosts"
146+
DirectBilling.SERVICE_COMMISSION_URL = "https://simpay.pl/api/db_hosts_commission"
147+
DirectBilling.GET_IP_URL = "https://simpay.pl/api/get_ip"

payments/sms.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import requests
2+
3+
4+
class SMS:
5+
def __init__(self, api_key, secret, service_id=None):
6+
self.api_key = api_key
7+
self.secret = secret
8+
self.service_id = service_id
9+
10+
# https://docs.simpay.pl/#weryfikacja-kodu
11+
def verify_code(self, request):
12+
if request.get("key") is None:
13+
request["key"] = self.api_key
14+
15+
if request.get("secret") is None:
16+
request["secret"] = self.secret
17+
18+
if request.get("service_id") is None:
19+
request["service_id"] = self.service_id
20+
21+
r = requests.post(SMS.VERIFY_CODE_URL, json={"params": request})
22+
23+
return r.json()
24+
25+
# https://docs.simpay.pl/#pobieranie-listy-uslug
26+
def get_service_list(self, request):
27+
if request.get("key") is None:
28+
request["key"] = self.api_key
29+
30+
if request.get("secret") is None:
31+
request["secret"] = self.secret
32+
33+
r = requests.post(SMS.SERVICE_LIST_URL, json={"params": request})
34+
35+
return r.json()
36+
37+
38+
SMS.VERIFY_CODE_URL = "https://simpay.pl/api/status"
39+
SMS.SERVICE_LIST_URL = "https://simpay.pl/api/get_services"

payments/sms_xml.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import hashlib
2+
import random
3+
import string
4+
import unicodedata
5+
6+
import requests
7+
8+
9+
class SMS_XML:
10+
def __init__(self, api_key):
11+
self.api_key = api_key
12+
13+
# https://docs.simpay.pl/#odbieranie-informacji-o-sms
14+
def check_parameters(self, request):
15+
for param in SMS_XML.PARAMS:
16+
if request.get(param) is None:
17+
return False
18+
19+
return request.get("sign") is not None and request.get("sign") == self.sign(request)
20+
21+
# https://docs.simpay.pl/#odbieranie-informacji-o-sms
22+
@staticmethod
23+
def generate_code():
24+
key = ''
25+
26+
for i in range(6):
27+
key += random.choice(string.ascii_uppercase + string.digits)
28+
29+
return key
30+
31+
# https://docs.simpay.pl/#lista-ip-serwerow-simpay
32+
@staticmethod
33+
def get_servers_ip():
34+
r = requests.get(SMS_XML.GET_IP_URL)
35+
36+
return r.json()
37+
38+
# https://docs.simpay.pl/#odbieranie-informacji-o-sms
39+
@staticmethod
40+
def generate_xml(code):
41+
header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><sms-response>"
42+
footer = "<sms-text></sms-text></sms-response>"
43+
44+
return header + unicodedata.normalize(code, "NKFD") + footer
45+
46+
def sign(self, request):
47+
return hashlib.new('sha256',
48+
bytes(
49+
str(request.get("sms_id")) +
50+
str(request.get("sms_text")) +
51+
str(request.get("sms_from")) +
52+
str(request.get("send_number")) +
53+
str(request.get("send_time")) +
54+
str(self.api_key),
55+
encoding="utf8")
56+
).hexdigest()
57+
58+
59+
SMS_XML.GET_IP_URL = "https://simpay.pl/api/get_ip"
60+
SMS_XML.CODES = {
61+
"7055": 0.25,
62+
"7136": 0.5,
63+
"7255": 1.0,
64+
"7355": 1.5,
65+
"7455": 2.0,
66+
"7555": 2.5,
67+
"7636": 3.0,
68+
"77464": 3.5,
69+
"78464": 4.0,
70+
"7936": 4.5,
71+
"91055": 5.0,
72+
"91155": 5.5,
73+
"91455": 7.0,
74+
"91664": 8.0,
75+
"91955": 9.5,
76+
"92055": 10.0,
77+
"92555": 12.5
78+
}
79+
SMS_XML.PARAMS = ["send_number", "sms_text", "sms_from", "sms_id", "sign"]

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
setuptools~=49.2.0
2+
requests~=2.24.0

setup.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
3+
from setuptools import setup, find_packages
4+
5+
setup(
6+
name='simpay-api',
7+
version='1.0',
8+
description='Python wrapper for Simpay API',
9+
author='DarkGL <r.wiecek@simpay.pl>',
10+
11+
url='https://github.com/SimPaypl/SimPay-API-Python',
12+
13+
packages=['payments'],
14+
install_required=[
15+
'requests'
16+
]
17+
)

0 commit comments

Comments
 (0)