__init__.py
17.7 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import base64
import json
import re
from time import sleep
import requests
from opensipkd.base import get_settings
from opensipkd.pasar.models import PartnerProduk, H2hArInvoiceDet
from ..vendor import VendorClass
import logging
log = logging.getLogger(__name__)
import urllib3
# from . import notify
urllib3.disable_warnings()
timeout = 30
class Vendor(VendorClass):
def __init__(self, vendor_produk, invoice_det):
VendorClass.__init__(self, vendor_produk, invoice_det = invoice_det)
# an = Animal()
# print('isi vendor_produk >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
# attrs = vars(vendor_produk.produk)
# {'kids': 0, 'name': 'Dog', 'color': 'Spotted', 'age': 10, 'legs': 2, 'smell': 'Alot'}
# now dump this in some way or another
# print(', '.join("%s: %s" % item for item in attrs.items()))
# id_pel, customer_id, cust_trx, row
settings = get_settings()
# self.mid = 'odeo_mid' in settings and settings['odeo_mid'] or None
# self.key = 'odeo_key' in settings and settings['odeo_key'] or None
self.url = 'mkm_url' in settings and settings['mkm_url'] or None
self.userid = 'mkm_userid' in settings and settings['mkm_userid'] or None
self.internal_kode_produk = vendor_produk.produk.kode
# key = ":".join([self.mid, self.key]).encode()
# self.auth = base64.b64encode(key).decode()
def get_headers(self):
return {'Content-Type': 'application/json'}
def get_url(self, url=None):
return url and self.url + url or self.url
def _inquiry(self):
params = {}
params['Action'] = 'inquiry'
params['ClientId'] = self.userid
params['MCC'] = 6025
params['KodeProduk'] = self.v_produk_kd
params['NomorPelanggan'] = self.id_pel
params = json.dumps(params)
self.request = params
log.info("Inquiry Request: url: %s params %s" % (self.url, params))
self.save_log("inquiry")
try:
resp = requests.post(self.url, data=params, verify=False,
headers=self.get_headers())
except Exception as e:
log.info(e)
return
return resp
def _integrate_inquiry(self):
parsd = None
response_inquiry = self._inquiry() #plain json type
if response_inquiry is None or not response_inquiry.text:
log.info("Resp Tidak Ada, {}".format(response_inquiry))
return
try:
result_inquiry = json.loads(response_inquiry.text) # converting to dictionary type
except:
self.response = response_inquiry.text #converting to readable string
return self.set_failed(typ="inquiry") # failing while parsing json to dictionary
log.info("Inquiry Response: %s" % self.response)
self.response = result_inquiry # should be json serializable
message_log = 'Success'
parsd = self.pars_data(result_inquiry) # parsing to user information
if response_inquiry.status_code == 200: #answered request
is_error = 'ErrorMessage' in result_inquiry and result_inquiry['ErrorMessage'] or ''
error_status = result_inquiry['Status']
error_msg = is_error
message_log = '[' + error_status + '] ' + error_msg
is_error = is_error != '' and error_status != '0000'
if is_error :
self.status = 0
return self.set_failed(parsd, message = message_log, typ = "inquiry") # error means failed
else :
# nothing goes wrong, will continue to payment sequences
self.status = 1 # sukses
return result_inquiry
# return self.set_success(parsd, typ = "inquiry")
else: # network fail or any other errors
return self.set_failed(parsd, typ="inquiry")
def inquiry(self):
if not self.v_produk_kd or not self.id_pel:
return self.set_response(message='Parameter tidak lengkap')
response_inquiry = self._inquiry()
if response_inquiry is None or not response_inquiry.text:
log.info("Resp Tidak Ada, {}".format(response_inquiry))
return
try:
result_inquiry = json.loads(response_inquiry.text)
except:
self.response = response_inquiry.text
return self.set_failed(typ="inquiry")
self.response = result_inquiry
log.info("Inquiry Response: %s" % self.response)
parsd = self.pars_data(result_inquiry) # parsing to user information
if response_inquiry.status_code == 200: #answered request
is_error = 'ErrorMessage' in result_inquiry and result_inquiry['ErrorMessage'] or ''
error_status = result_inquiry['Status']
error_msg = is_error
message_log = '[' + error_status + '] ' + error_msg
is_error = is_error != '' and error_status != '0000'
if is_error :
self.status = 0
return self.set_failed(parsd, message = message_log, typ = "inquiry") # error means failed
else :
# nothing goes wrong, will continue to payment sequences
self.status = 1 # sukses
return self.set_success(parsd, typ = "inquiry")
else: # network fail or any other errors
return self.set_failed(parsd, typ="inquiry")
def payment(self):
parsd = None
result_inquiry = self._integrate_inquiry()
tagihan_list_inq = result_inquiry['Tagihan']
tagihan_list_pay = []
# total_admin = 0
total_admin = int(self.vendor_produk.produk.harga * len(tagihan_list_inq))
voucher_nominal = None
if self.v_produk_kd == '522' :
nominal = self.internal_kode_produk
nominal = nominal.replace('PLN', '')
nominal = int(nominal) * 1000
voucher_nominal = nominal
for item in tagihan_list_inq:
tagihan_pay = {}
tagihan_pay['Periode'] = item['Periode']
if voucher_nominal:
tagihan_pay['Total'] = voucher_nominal
else :
tagihan_pay['Total'] = item['Total']
# total_admin = total_admin + item['Total']
tagihan_list_pay.append(tagihan_pay)
params = dict(
Action = "payment",
ClientId = self.userid,
KodeProduk = self.v_produk_kd,
MCC = 6025,
SessionId = result_inquiry['SessionId'],
NomorPelanggan = result_inquiry['NomorPelanggan'],
Tagihan = tagihan_list_pay,
TotalAdmin = total_admin
)
self.request = params
log.info("Payment Request: %s" % self.request)
self.save_log("payment")
url = self.get_url()
try:
response_payment = requests.post(url, data=json.dumps(params), verify=False,
headers=self.get_headers(), timeout=timeout)
except:
self.response = response_payment.text #converting to readable string
return self.set_failed(typ="payment") # failing while parsing json to dictionary
if response_payment is None or not response_payment.text:
return self.set_failed(typ="payment")
try:
result_payment = json.loads(response_payment.text)
except:
self.response = response_payment.text
log.info("Payment Response: %s" % self.response)
return self.set_failed(typ="payment")
self.response = result_payment
log.info("Payment Response: %s" % self.response)
parsd = self.pars_data(result_payment) # parsing to user information
if response_payment.status_code == 200: #answered request
is_error = 'ErrorMessage' in result_payment and result_payment['ErrorMessage'] or ''
error_status = result_payment['Status']
error_msg = is_error
message_log = '[' + error_status + '] ' + error_msg
is_error = is_error != '' and error_status != '0000'
if is_error :
self.status = 0
if error_status == '0068' :
return self.set_pending(parsd, typ = "payment")
else :
return self.set_failed(parsd, message = message_log, typ = "payment") # error means failed
else :
# nothing goes wrong, will continue to payment sequences
self.status = 1 # sukses
return self.set_pending(parsd, typ = "payment")
else: # network fail or any other errors
return self.set_failed(parsd, typ="payment")
def advice(self):
# if not self.v_produk_kd or not self.id_pel or not self.invoice_det:
# return dict(code=9999,
# message='Parameter tidak lengkap')
parsd = None
result_inquiry = self._integrate_inquiry()
tagihan_list_inq = result_inquiry['Tagihan']
tagihan_list_pay = []
# total_admin = 0
total_admin = int(self.vendor_produk.produk.harga * len(tagihan_list_inq))
voucher_nominal = None
if self.v_produk_kd == '522' :
nominal = self.internal_kode_produk
nominal = nominal.replace('PLN', '')
nominal = int(nominal) * 1000
voucher_nominal = nominal
for item in tagihan_list_inq:
tagihan_pay = {}
tagihan_pay['Periode'] = item['Periode']
if voucher_nominal:
tagihan_pay['Total'] = voucher_nominal
else :
tagihan_pay['Total'] = item['Total']
# total_admin = total_admin + item['Total']
tagihan_list_pay.append(tagihan_pay)
params = dict(
Action = "advice",
ClientId = self.userid,
KodeProduk = self.v_produk_kd,
MCC = 6025,
SessionId = result_inquiry['SessionId'],
NomorPelanggan = result_inquiry['NomorPelanggan'],
Tagihan = tagihan_list_pay,
TotalAdmin = total_admin
)
url = self.get_url()
self.save_log("advice")
try:
response_advice = requests.post(url, data=json.dumps(params), verify=False,
headers=self.get_headers(), timeout=timeout)
except:
self.response = response_advice.text #converting to readable string
return self.set_response() # failing while parsing json to dictionary
if response_advice is None or not response_advice.text:
return self.set_failed(typ="advice")
try:
result_advice = json.loads(response_advice.text)
except:
result_advice = response_advice.text
self.response = result_advice
parsd = self.pars_data(result_advice)
save_log('advice')
return parsd
def pars_data(self, data):
result = dict()
print('isi data pars data >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
print(data)
if not data:
return result
if "NomorPelanggan" in data and data["NomorPelanggan"]:
result['id_pel'] = data["NomorPelanggan"]
if "Nama" in data and data["Nama"]:
result['nama'] = data["Nama"]
if "Nama" in data and data["Nama"]:
result['nama'] = data["Nama"]
rincian = {}
if "Tarif" in data and data["Tarif"]:
rincian['tarif'] = data["Tarif"]
if "NomorMeter" in data:
rincian['no_meter'] = data["NomorMeter"]
if "Daya" in data:
rincian['power'] = data["Daya"]
print('isi vp_produk_kd >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
print(self.v_produk_kd)
# if self.v_produk_kd[:3] == "PLN" and self.v_produk_kd != "PLNPASCA":
if self.v_produk_kd == "522":
print('masuk pra bayar >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
# harga = "price" in data and data["price"] or 0
# pokok = harga and int(re.sub("\D","", self.v_produk_kd))*1000 or 0
pokok = int(self.vendor_produk.produk.harga)
admin = 0
rincian["pokok"] = pokok
rincian["admin"] = admin
rincian["denda"] = 0
rincian["total"] = pokok + admin
rincian["token"] = "token" in data and data["token"] or ""
log.info(">Rincian: {}".format(rincian))
elif self.v_produk_kd == "521":
print('masuk 521 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
if 'tarif' in rincian and rincian['tarif']:
p = rincian['tarif'].split('/')
if len(p) > 1:
rincian["power"] = p[1][:-2]
if "NomorPelanggan" in data and data["NomorPelanggan"]:
rincian['no_meter'] = data["NomorPelanggan"]
i = 1
inquiries = 'Tagihan' in data and data['Tagihan'] \
or 'Tagihan' in data and data['Tagihan'] or {}
if not inquiries:
result["jml_data"] = 1
result["rincian"] = rincian
return result
inquirieses = type(inquiries) is list and inquiries or [inquiries]
pokok = denda = admin = 0
period = meter = ""
jml_period = 0
i = 0
log.info(inquirieses)
for inquiries in inquirieses:
# odeo ngerubah data inquiry
if "Tarif" in inquiries and inquiries["tarif"]:
rincian['tarif'] = inquiries["tariff"]
if "NomorMeter" in inquiries:
rincian['no_meter'] = inquiries["NomorMeter"]
if "Daya" in inquiries:
rincian['power'] = inquiries["Daya"]
if "NomorPelanggan" in inquiries and inquiries["NomorPelanggan"]:
result['id_pel'] = inquiries["NomorPelanggan"]
if "Nama" in inquiries and inquiries["Nama"]:
result['nama'] = inquiries["Nama"]
if "NomorPelanggan" in inquiries and inquiries["NomorPelanggan"]:
result['nama'] = inquiries["NomorPelanggan"]
if "TagihanListrik" in inquiries:
pokok += int(inquiries["TagihanListrik"])
if "fine" in inquiries:
denda += int(inquiries["Denda"])
if "admin" in inquiries:
admin += int(inquiries["admin"])
if "admin_fee" in inquiries:
admin += int(inquiries["admin_fee"])
if "coll_fee" in inquiries:
if "coll_fee" in rincian:
rincian['coll_fee'] += int(inquiries["coll_fee"])
else:
rincian['coll_fee'] = int(inquiries["coll_fee"])
if "bill_rest" in inquiries:
if "bill_rest" in rincian:
rincian['bill_rest'] += int(inquiries["bill_rest"])
else:
rincian['bill_rest'] = int(inquiries["bill_rest"])
if "Periode" in inquiries:
period += " " + inquiries["Periode"]
if "meter_changes" in inquiries:
if "meter" in rincian:
rincian['meter'] += inquiries["meter_changes"]
else:
rincian['meter'] = inquiries["meter_changes"]
if "usages" in inquiries:
if "penggunaan" in rincian:
rincian['penggunaan'] += inquiries["usages"]
else:
rincian['penggunaan'] = inquiries["usages"]
if "installment" in inquiries:
if "installment" in rincian:
rincian['installment'] += " " + inquiries["installment"]
else:
rincian['installment'] = inquiries["installment"]
if "platform" in inquiries:
if "platform" in rincian:
rincian['platform'] += " " + inquiries["platform"]
else:
rincian['platform'] = inquiries["platform"]
if "TanggalAkhirPeriode" in inquiries:
rincian['jth_tempo'] = inquiries["TanggalAkhirPeriode"]
# if "jth_tempo" in rincian:
# rincian['jth_tempo'] += " " + inquiries["due_date"]
# else:
if "branch_code" in inquiries:
rincian['cabang'] = inquiries["branch_code"]
if "branch_name" in inquiries:
rincian['nm_cabang'] = inquiries["branch_name"]
if "month_counts" in inquiries:
rincian['jml_bulan'] = inquiries["month_counts"]
if "participant_counts" in inquiries:
rincian['anggota'] = inquiries["participant_counts"]
i += 1
self.amt_buy = "TotalTagihan" in data and data["TotalTagihan"] or 0
rincian["pokok"] = pokok
rincian["denda"] = denda
admin = int(self.vendor_produk.produk.harga * i)
rincian["admin"] = admin
rincian["total"] = pokok + denda + admin
rincian["period"] = period
if not 'jml_bulan' in rincian:
rincian["jml_bulan"] = i
result["subtotal"] = rincian["total"]
product_id = self.invoice_det.produk.id
if hasattr(self.invoice_det, "customer_id"):
partner_id = self.invoice_det.customer_id
else:
partner_id = self.invoice_det.h2h_ar_invoice.customer_id
discount = PartnerProduk.get_discount(partner_id, product_id)
self.discount = discount * i
result["discount"] = self.discount
# self.amt_sell = result["total"]
result['rincian'] = rincian
return result