|
| 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" |
0 commit comments