rpc.py
16.9 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import sys
import json
import requests
import hmac
import hashlib
import base64
from datetime import datetime, time, date
from sqlalchemy import not_, func, between, and_, literal_column
from pyramid_rpc.jsonrpc import jsonrpc_method
from pyramid_rpc.jsonrpc import JsonRpcError
from ..tools import (date_from_str, dict_to_str, to_str, get_settings, ymdhms)
##MODEL
from ..models.isipkd import (DBSession, ARInvoice, ARSspd, User, Unit,
ObjekPajak, SubjekPajak, Rekening, Wilayah)
from .arinvoice import save as save_invoice
from ..models.rpc import (DepartemenRoute, auth_from_rpc, auth_from_rpc, json_rpc_header, get_jsonrpc,
clear_null_value, ymd, get_mandatory)
from ..models.rpc import (JsonRpcBillNotFoundError, JsonRpcPaymentNotFoundError,
JsonRpcBillAllreadyPaidError, JsonRpcInvalidDataError, JsonRpcInvalidLoginError,
custom_error)
from .bjb_qris import (BJBQRIS, cekqris, calculate_tagihan, rpc_params as rpc_qris)
from .bjb_va import (BJBVA, cekva, calculate_tagihan, rpc_params as rpc_va)
import logging
log = logging.getLogger('RPC ESIPKD')
from time import sleep
###########################################################################
def save_departemen_route(request, values, row=None):
if not row:
row = DepartemenRoute()
if not 'kode' in values or not values['kode']:
return 0
if not 'no_skrd' in values or not values['no_skrd']:
return 0
if not 'tgl_skrd' in values or not values['tgl_skrd']:
return 0
if not 'ar_invoice_id' in values or not values['ar_invoice_id']:
return 0
if not 'status' in values:
values['status'] = 0
row.from_dict(values)
DBSession.add(row)
DBSession.flush()
return row
@jsonrpc_method(method='set_invoice', endpoint='api-webr')
def set_invoice(request, data):
"""
Digunakan untuk menerima data tagihan
:param request:
:param data:
- op_kode
- rek_kode
- dasar
- tarif
- pokok
- denda
- bunga
- jumlah
- periode_1
- periode_2
- jatuh_tempo
- no_skrd
- tgl_skrd
:return:
- kd_bayar
"""
auth_from_rpc(request)
param = ['op_kode','rek_kode','dasar','tarif',
'pokok','denda','bunga','jumlah','periode_1',
'periode_2','jatuh_tempo','tgl_tetap','no_skrd']
get_mandatory(data, param)
## LOGGING SET INVOICE ##
log.error('==== SET INVOICE ====')
log.error(data)
values = clear_null_value(data)
if 'jatuh_tempo' in values and values['jatuh_tempo']:
values['jatuh_tempo'] = date_from_str(values['jatuh_tempo'])
if 'periode_1' in values and values['periode_1']:
values['periode_1'] = date_from_str(values['periode_1'])
if 'periode_2' in values and values['periode_2']:
values['periode_2'] = date_from_str(values['periode_2'])
values['status_bayar'] = 0
op = ObjekPajak.get_by_kode(values['op_kode'])
if not op:
return custom_error(-1,'Objek Pajak kode {} not found'.format(values['op_kode']))
values['objek_pajak_id'] = op.id
values['op_kode'] = op.kode
values['op_nama'] = op.nama
values['op_alamat_1'] = op.alamat_1
values['op_alamat_2'] = op.alamat_2
values['wilayah_id'] = op.wilayah_id
values['subjek_pajak_id'] = op.subjekpajak_id
values['unit_id'] = op.unit_id
subjek = SubjekPajak.get_by_id(values['subjek_pajak_id'])
if not subjek:
return custom_error(-1,'Subjek Pajak id {} not found, contact admin'.format(values['subjek_pajak_id']))
values['wp_kode'] = subjek.kode
values['wp_nama'] = 'wp_nama' in values and values['wp_nama'] and \
values['wp_nama'] or subjek.nama
values['wp_alamat_1'] = subjek.alamat_1
values['wp_alamat_2'] = subjek.alamat_2
unit = Unit.get_by_id(values['unit_id'])
if not unit:
return custom_error(-1,'Unit id {} not found, contact admin'.format(values['unit_id']))
values['unit_kode'] = unit.id
values['unit_nama'] = unit.nama
rek = Rekening.get_by_kode(values['rek_kode'])
if not rek:
return custom_error(-1,'Rekening kode {} not found, contact admin'.format(values['rek_kode']))
values['rekening_id'] = rek.id
values['rek_nama'] = rek.nama
rdata = {}
for v in values:
v = v.strip()
row_invoice = DBSession.query(ARInvoice).\
filter(ARInvoice.no_skrd == values['no_skrd']).first()
if row_invoice:
if row_invoice.status_bayar == 1:
raise JsonRpcBillAllreadyPaidError
return custom_error(-1,'Bill already exist')
else:
invoice = save_invoice(request, values)
# Save Departemen_Route
values['kode'] = invoice.kode
values['no_skrd'] = invoice.no_skrd
values['tgl_skrd'] = invoice.tgl_tetap
values['ar_invoice_id'] = invoice.id
save_departemen_route(request, values)
if not save_departemen_route:
return dict(code=-32603,
message='Gagal menambahkan Invoice',
data=rdata)
rdata['kd_bayar'] = invoice.kode
return dict(code=0,
message='Sukses menambahkan Invoice',
data=rdata)
@jsonrpc_method(method='get_payment', endpoint='api-webr')
def get_payment(request, data):
"""
Digunakan untuk informasi pembayaran
:param request:
:param data:
- kd_bayar
:return:
- ntp
- ntb
- pembayaran_ke
- bunga
- bayar
- tgl_bayar
- jatuh_tempo
- bank_id
- channel_id
"""
auth_from_rpc(request)
get_mandatory(data, ['kd_bayar'])
## LOGGING GET PAYMENT ##
log.error('==== GET PAYMENT ====')
log.error(data)
row_invoice = DBSession.query(ARInvoice).filter_by(kode=data['kd_bayar']).first()
if not row_invoice:
raise JsonRpcBillNotFoundError
row_payment = DBSession.query(ARSspd.ntp, ARSspd.pembayaran_ke, ARSspd.bunga,
ARSspd.bayar, ARSspd.tgl_bayar, ARInvoice.jatuh_tempo,
ARSspd.ntb, ARSspd.bank_id, ARSspd.channel_id).\
join(ARInvoice, ARInvoice.id == ARSspd.arinvoice_id).\
filter(ARInvoice.kode==data['kd_bayar']).\
order_by(ARSspd.pembayaran_ke).all()
if not row_payment:
raise JsonRpcPaymentNotFoundError
rdata = []
for k in row_payment:
d = dict(
kd_bayar = data['kd_bayar'],
ntp = hasattr(k,'ntp') and k.ntp and k.ntp.strip() or '',
pembayaran_ke = k.pembayaran_ke,
bunga = k.bunga,
bayar = k.bayar,
tgl_bayar = ymd(k.tgl_bayar),
jatuh_tempo = ymd(k.jatuh_tempo),
ntb = hasattr(k,'ntb') and k.ntb and k.ntb.strip() or '',
bank_id = k.bank_id,
channel_id = k.channel_id,
)
rdata.append(d)
message = ""
return dict(code=0,
message=message,
data=rdata)
@jsonrpc_method(method='send_payment', endpoint='api-webr')
def send_payment(request, data):
"""
Digunakan untuk Scheduler pengiriman data pembayaran
:param request:
:param data:
- url
- token
:return:
-
"""
auth_from_rpc(request)
get_mandatory(data, ['url','token'])
ws_url = data['url']
token = data['token']
datar = []
row_payment = DBSession.query(
ARSspd.ntp, ARSspd.pembayaran_ke,
ARSspd.bunga, ARSspd.bayar, ARSspd.tgl_bayar,
ARSspd.ntb, ARSspd.bank_id, ARSspd.channel_id,
ARInvoice.kode.label("kd_bayar"), ARInvoice.jatuh_tempo,
DepartemenRoute.id).\
join(DepartemenRoute, DepartemenRoute.ar_invoice_id == ARSspd.arinvoice_id).\
join(ARInvoice, ARInvoice.id == ARSspd.arinvoice_id).\
filter(DepartemenRoute.status==0,
ARInvoice.status_bayar==1).\
order_by(ARSspd.pembayaran_ke).all()
if row_payment:
for k in row_payment:
responsetxt = None
d = dict(
kd_bayar = k.kd_bayar,
ntp = k.ntp,
pembayaran_ke = k.pembayaran_ke,
bunga = k.bunga,
bayar = k.bayar,
tgl_bayar = ymd(k.tgl_bayar),
jatuh_tempo = ymd(k.jatuh_tempo),
ntb = k.ntb,
bank_id = k.bank_id,
channel_id = k.channel_id,
token = token
)
# print('>>>>>>>>>>> DEBUG DATA PAYMENT',d)
## LOGGING SEND PAYMENT ##
log.error('==== SEND PAYMENT ====')
log.error(d)
response = requests.post(ws_url, data=json.dumps(d), verify=False)
if response.status_code != 200:
continue
# try:
# responsetxt = json.loads(response.text)
# if 'result' in responsetxt and responsetxt['result'] != 'success':
# continue
# except:
# continue
# Save Departemen_Route
values = dict(id=k.id,status=1)
row = DepartemenRoute.get_by_id(data['id'])
save_departemen_route(request, values, row)
del d['token']
datar.append(d)
return dict(code=0,
message="Send Payment Success",
data=datar,
)
@jsonrpc_method(method='create_qris', endpoint='api-webr')
def create_qris(request, data):
"""
Digunakan untuk menambahkan create qris
:param request:
:param data:
- kd_bayar
- unit_kode
:return:
-
"""
sleep(0.75)
auth_from_rpc(request)
get_mandatory(data, ['kd_bayar','unit_kode'])
log.error('CREATE QRIS BJBQRIS')
log.error(data)
cekQr=DBSession.query(BJBQRIS).filter(BJBQRIS.invoice_no==data['kd_bayar']).first()
if cekQr and cekQr.expired_date>datetime.now():
dt_qris = dict(client_type =cekQr.client_type,
product_code=cekQr.product_code,
invoice_no = cekQr.invoice_no,
description=cekQr.description,
customer_name=cekQr.customer_name,
customer_email=cekQr.customer_email and cekQr.customer_email or '-',
customer_phone=cekQr.customer_phone and cekQr.customer_phone or '-',
expired_date=ymdhms(datetime.combine(cekQr.expired_date,time(23,59,59))),
amount=cekQr.amount,
qrcode=cekQr.qrcode)
return dict(code=0, message="Success Create Qris", data=dt_qris)
row = DBSession.query(ARInvoice.id.label('id'),
ARInvoice.jumlah.label('jumlah'),
ARInvoice.kode.label('kode'),
ARInvoice.jatuh_tempo.label('jatuh_tempo'),
func.concat(ARInvoice.no_skrd ,
literal_column("' Tgl:'") ,
func.to_char(ARInvoice.tgl_tetap, 'DD/MM/YYYY') ,
literal_column("' Tgl:'") ,
func.to_char(ARInvoice.jatuh_tempo, 'DD/MM/YYYY') ,
literal_column("' '") ,
ARInvoice.keterangan).label('description'),
ARInvoice.wp_nama.label('wp_nama'),
SubjekPajak.email.label('subjek_email'),
ARInvoice.status_bayar.label('status')).\
filter(func.trim(ARInvoice.kode) == data['kd_bayar'].strip(),
func.trim(ARInvoice.unit_kode) == data['unit_kode'].strip()).\
outerjoin(SubjekPajak, SubjekPajak.id==ARInvoice.subjek_pajak_id).first()
if not hasattr(row,'status') or row.status != 0:
return dict(code=-1, message="No bayar tidak ditemukan / Sudah dibayar")
pokok, denda = calculate_tagihan(dict(pokok=row.jumlah,jatuh_tempo=row.jatuh_tempo))
if row.jatuh_tempo < date.today():
jatuh_tempo = ymdhms(datetime.combine(date.today(),time(23,55,00)))
else:
jatuh_tempo = ymdhms(datetime.combine(row.jatuh_tempo,time(23,55,00)))
expired_date = jatuh_tempo
dt_qris = dict(client_type ="3",
product_code="30",
invoice_no = row.kode,
description=row.description,
customer_name=row.wp_nama,
customer_email=row.subjek_email and row.subjek_email or '-',
customer_phone='-',
expired_date=expired_date,
amount=(pokok + denda))
cekqris(dt_qris, request)
val = dt_qris
val['amount'] = (pokok+denda)
row_qris = BJBQRIS.create_va(val, rpc_qris())
log.error(row_qris)
Qqris=DBSession.query(BJBQRIS).filter(BJBQRIS.invoice_no==data['kd_bayar']).first()
dt_qris['qrcode']=Qqris.qrcode
if row_qris:
return dict(code=0, message="Success Create Qris", data=dt_qris)
else:
return dict(code=-1, message="Failed Create Qris")
@jsonrpc_method(method='create_va', endpoint='api-webr')
def create_va(request, data):
"""
Digunakan untuk menambahkan create va
:param request:
:param data:
- kd_bayar
- unit_kode
:return:
-
"""
sleep(0.75)
auth_from_rpc(request)
get_mandatory(data, ['kd_bayar','unit_kode'])
log.error('CREATE VA BJBVA')
log.error(data)
cekVa=DBSession.query(BJBVA).filter(BJBVA.invoice_no==data['kd_bayar']).first()
if cekVa and cekVa.expired_date>datetime.now():
dt_va = dict(client_type =cekVa.client_type,
product_code=cekVa.product_code,
invoice_no = cekVa.invoice_no,
description=cekVa.description,
customer_name=cekVa.customer_name,
customer_email=cekVa.customer_email and cekVa.customer_email or '-',
customer_phone=cekVa.customer_phone and cekVa.customer_phone or '-',
expired_date=ymdhms(datetime.combine(cekVa.expired_date,time(23,59,59))),
amount=cekVa.amount,
va_number=cekVa.va_number)
return dict(code=0, message="Success Create Va", data=dt_va)
row = DBSession.query(ARInvoice.id.label('id'),
ARInvoice.jumlah.label('jumlah'),
ARInvoice.kode.label('kode'),
ARInvoice.jatuh_tempo.label('jatuh_tempo'),
func.concat(ARInvoice.no_skrd ,
literal_column("' Tgl:'") ,
func.to_char(ARInvoice.tgl_tetap, 'DD/MM/YYYY') ,
literal_column("' Tgl:'") ,
func.to_char(ARInvoice.jatuh_tempo, 'DD/MM/YYYY') ,
literal_column("' '") ,
ARInvoice.keterangan).label('description'),
ARInvoice.wp_nama.label('wp_nama'),
SubjekPajak.email.label('subjek_email'),
ARInvoice.status_bayar.label('status')).\
filter(func.trim(ARInvoice.kode) == data['kd_bayar'].strip(),
func.trim(ARInvoice.unit_kode) == data['unit_kode'].strip()).\
outerjoin(SubjekPajak, SubjekPajak.id==ARInvoice.subjek_pajak_id).first()
if not hasattr(row,'status') or row.status != 0:
return dict(code=-1, message="No bayar tidak ditemukan / Sudah dibayar")
pokok, denda = calculate_tagihan(dict(pokok=row.jumlah,jatuh_tempo=row.jatuh_tempo))
if row.jatuh_tempo < date.today():
jatuh_tempo = ymdhms(datetime.combine(date.today(),time(23,55,00)))
else:
jatuh_tempo = ymdhms(datetime.combine(row.jatuh_tempo,time(23,55,00)))
expired_date = jatuh_tempo
dt_va = dict(client_type ="3",
product_code="30",
invoice_no = row.kode,
description=row.description,
customer_name=row.wp_nama,
customer_email=row.subjek_email and row.subjek_email or '-',
customer_phone='-',
expired_date=expired_date,
amount=(pokok + denda))
cekva(dt_va, request)
val = dt_va
val['amount'] = (pokok+denda)
row_va = BJBVA.create_va(val, rpc_va())
log.error(row_va)
Qva=DBSession.query(BJBVA).filter(BJBVA.invoice_no==data['kd_bayar']).first()
dt_va['va_number']=Qva.va_number
if row_va:
return dict(code=0, message="Success Create Va", data=dt_va)
else:
return dict(code=-1, message="Failed Create Va")