api.py
9.29 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import base64
import hashlib
import hmac
import json
import sys
from datetime import datetime, date
import requests
from pyramid.i18n import TranslationStringFactory
from pyramid_rpc.jsonrpc import JsonRpcError
from opensipkd.tools import get_settings, get_random_number
LIMIT = 1000
CODE_OK = 0
MSG_OK = 'Data Submitted'
import logging
log = logging.getLogger(__name__)
_ = TranslationStringFactory('json_error')
class JsonRpcOtherError(JsonRpcError):
code = -32604
message = _('Other Error')
# error terkait database
class JsonRpcNotImplementedError(JsonRpcError):
code = -40000
message = _("Method Not Implemented")
class JsonRpcDbUrlNotFoundError(JsonRpcError):
code = -40001
message = _("DB URL Connection Not Found")
class JsonRpcDbConnectionError(JsonRpcError):
code = -40002
message = _("DB Connection Not Found")
class JsonRpcDataNotFoundError(JsonRpcError):
code = -40003
message = _("Error Data Not Found")
class JsonRpcDataFoundError(JsonRpcError):
code = -40004
message = _("Error Data Already Found")
class JsonRpcInvalidLoginError(JsonRpcError):
code = -41001
message = _("Invalid RPC User/Password")
class JsonRpcInvalidNikError(JsonRpcError):
code = -41002
message = _('Invalid NIK')
class JsonRpcNikFoundError(JsonRpcError):
code = -41003
message = _('NIK already found')
class JsonRpcMobileFoundError(JsonRpcError):
code = -41004
message = _('Mobile number already found')
class JsonRpcInvalidMobileError(JsonRpcError):
code = -41004
message = _('Invalid Mobile Number')
class JsonRpcInvalidDataError(JsonRpcError):
code = -41005
message = _('Data Invalid')
class JsonRpcInvalidData(JsonRpcError):
code = -41005
message = _('Invalid Data')
class JsonRpcEmailFound(JsonRpcError):
code = -41006
message = _('e-mail already found')
class JsonRpcEmailFoundError(JsonRpcEmailFound):
"""Compatibilty"""
class JsonRpcInvalidEmailError(JsonRpcError):
code = -41007
message = _('Invalid e-mail')
class JsonRpcMailError(JsonRpcError):
code = -41008
message = 'Gagal autentikasi mail server'
class JsonRpcUserFound(JsonRpcError):
code = -41009
message = _('User already found')
class JsonRpcUserFoundError(JsonRpcUserFound):
"""Compatibity """
class JsonRpcRegisterFailError(JsonRpcError):
code = -41010
message = _('User Registration Failed ')
class JsonRpcProfileFail(JsonRpcError):
code = -41011
message = _('Update Profile Failed')
class JsonRpcProfileFailError(JsonRpcError):
"""Portabitas"""
class JsonRpcGetPasswordError(JsonRpcError):
code = -41012
message = 'Gagal Request Password'
class JsonRpcUserNotFoundError(JsonRpcError):
code = -41013
message = 'User Tidak Ada'
class JsonRpcInvalidTimeError(JsonRpcError):
code = -41014
message = 'Periksa Date Time Server'
class JsonRpcPermissionError(JsonRpcError):
code = -41015
message = 'Hak Akses dibatasi'
# error terkait transaksi
class JsonRpcInvalidInvoiceError(JsonRpcError):
code = -50001
message = 'Invalid No Bayar'
class JsonRpcBankNotFoundError(JsonRpcError):
code = -51001
message = 'Bank Not Found'
class JsonRpcBillNotFoundError(JsonRpcError):
code = -52001
message = 'Bill Not Found'
# class JsonRpcDataNotFoundError(JsonRpcError):
# code = -52001
# message = 'Data Not Found'
class JsonRpcBillAllreadyPaidError(JsonRpcError):
code = -52002
message = 'Bill Allready Paid'
class JsonRpcBillDifferentError(JsonRpcError):
code = -52003
message = 'Bill Amount Different'
class JsonRpcNtbNotFoundError(JsonRpcError):
code = -53001
message = 'NTB Not Found'
class JsonRpcNtbNotValidError(JsonRpcError):
code = -53002
message = 'NTB Not Valid'
# Reversal Message
class JsonRpcPaymentNotFoundError(JsonRpcError):
code = -54001
message = 'Payment Not Found'
class JsonRpcPaymentNotOwnerError(JsonRpcError):
code = -54002
message = 'Not Your Payment'
class JsonRpcPaymentOpenError(JsonRpcError):
code = -54003
message = 'Payment Open'
class JsonRpcReversedError(JsonRpcError):
code = -54004
message = 'Payment Reversed'
class JsonRpcReversalError(JsonRpcError):
code = -54005
message = 'Cannot Reversed'
class JsonRpcPaymentFoundError(JsonRpcError):
code = -54006
message = 'Payment Found with same invoice and ntb'
# Biller Status
class JsonRpcBillerNotFoundError(JsonRpcError):
code = -55001
message = 'Biller Not Found'
class JsonRpcBillerNetworkError(JsonRpcError):
code = -55002
message = 'Biller Network unrecognized'
class JsonRpcBillerError(JsonRpcError):
code = -55000
message = 'Biller Other Error'
class JsonRpcProdukNotFoundError(JsonRpcError):
code = -56001
message = _('Items Not Found')
# Biller Status
class JsonRpcCaNotRegisteredError(JsonRpcError):
code = -56001
message = 'CA Not Registered'
def custom_error(code, message):
cls = JsonRpcError()
cls.code = code
cls.message = message
return cls
begin_unix_time = datetime(1970, 1, 1)
def get_seconds(current_time=None):
"""
Digunakan untuk menghitung jumlah detik sampai dengan waktu sekarang
:param current_time: optional waktu yang akan dihitung
:return:
"""
if not current_time:
current_time = datetime.utcnow()
durasi = current_time - begin_unix_time
log.info(f"{current_time} - {begin_unix_time}")
return int(durasi.total_seconds())
def json_rpc_header(userid, password, key=None):
"""
Digunakan untuk membuat header autentikasi saat akan mengirim request
:param userid: string,
:param password: string,
:param key: optional string berisi kode enkripsi
:return: {
userid string,
signature=string,
key=string
}
"""
if not key:
key = str(get_seconds())
value = '&'.join([str(userid), str(key)])
password = str(password)
signature = hmac.new(key=str.encode(password), msg=str.encode(value),
digestmod=hashlib.sha256).digest()
if sys.version < '3':
encoded_signature = base64.encodestring(signature).replace('\n', '')
else:
encoded_signature = base64.encodebytes(signature).decode().replace('\n',
'')
return dict(userid=userid, signature=encoded_signature, key=key)
def get_mandatory(data, values):
for value in values:
if value not in data or not data[value]:
raise JsonRpcDataInvalid(message="{} Not Found".format(value))
def validate_time(request):
global lima_menit
env = request.environ
time_stamp = int(env['HTTP_KEY'])
now = get_seconds()
settings = get_settings()
if 'diff_server_time' in settings and settings["diff_server_time"]:
lima_menit = int(settings["diff_server_time"])
if not request.devel and abs(now - time_stamp) > lima_menit:
log.info(f"req time {time_stamp} server time {now}")
raise JsonRpcInvalidTimeError
return time_stamp
def get_jsonrpc(method, params, **kwargs):
rid = kwargs.get("id") or int(get_random_number(6))
return dict(jsonrpc='2.0', method=method, params=params,
id=rid)
def send_rpc(auth, message):
"""
Digunakan untuk mengirim data dengan methode JSONRPC 2.0 with os-auth
:param auth: Dict
{"user": user,
"url": url,
"key": key,
"method": method,
"timeout": optional,
}
:param message: Dict
:return: Dict
"""
userid = auth['user']
password = auth['key']
url = auth['url']
headers = json_rpc_header(userid, password)
params = dict(data=message)
data = get_jsonrpc(auth["method"], params)
timeout = 'timeout' in auth and int(auth['timeout']) or 5
log.info("URL:{} timeout:{} detik".format(url, timeout))
log.warning("REQUEST {}".format(data))
try:
results = requests.post(url, json=data, headers=headers,
timeout=timeout) # data=jsondata,
except Exception as e:
log.warning(str(e))
return
if results.status_code != 200:
log.info(results)
log.info(results.text)
return
rows = results.text and json.loads(results.text) or None
log.info("RESPONSE {}".format(rows))
return rows
##############################
# Web Service Authentication #
##############################
lima_menit = 300
def json_serial(obj):
"""JSON serializer for objects not serializable by default json code"""
if isinstance(obj, (datetime, date)):
return obj.isoformat()
raise TypeError("Type %s not serializable" % type(obj))
def pars_rpc_url(params, method=None):
values = params.split("@")
user, key = values[0].split(":")
url = values[1]
resp = dict(
url=url,
user=user,
key=key
)
if method:
resp["method"] = method
return resp