vendor.py
13.3 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
"""
Module proses:
Save
Invoice To Vendor
Invoice To Customer
"""
import hashlib
import logging
from importlib import import_module
from ..models import (flush_row, PartnerProduk)
from ..tools import (get_settings)
# from opensipkd.pasar.models import PartnerLog
log = logging.getLogger(__name__)
class VendorClass(object):
def __init__(self, vendor_produk, invoice_det, **kwargs):
"""
:param vendor_produk:
:param bill_no:
:param customer_id:
:param cust_trx:
:param row:
"""
self.settings = get_settings()
self.vendor_produk = vendor_produk
self.invoice_det = invoice_det
self.v_produk_kd = vendor_produk.kode
self.kategori = vendor_produk.produk.kategori.kode
self.id_pel = invoice_det and invoice_det.id_pel or None
self.request = None
self.response = None
self.result = None
self.vend_inv_no = invoice_det and invoice_det.vend_inv_no or None
self.status = invoice_det and invoice_det.status or 0
self.serial_number = invoice_det and hasattr(invoice_det, 'serial_number') \
and invoice_det.serial_number or None
self.amt_buy = 0
self.amt_sell = 0
self.discount = 0
self.trx_id = 0
def set_response(self, data=None, message=None, code=999, typ="inquiry"):
if not data and message:
message = message and message or "No Response From Biller"
data = dict(code=code,
message=message)
self.result = data
self.save_log(typ)
log.info("Result: %s" % data)
return data
def set_success(self, data=None, typ="payment"):
self.status = 1
self.result = data
data = data and data or {}
data.update(dict(code=0,
message="SUCCESS",
status="SUCCESS"))
func = self.vendor_produk.produk.kategori.module
pckgs = 'agratek.api.merchant.views.vendor'
log.info("Module: %s Pckgs: %s" % (func, pckgs))
modul = import_module(pckgs)
cls = hasattr(modul, func) and getattr(modul, func) or None
if cls:
data['denom'] = self.invoice_det.produk.kode
data['id_pel'] = self.id_pel
data['nama'] = 'nama' in data and data['nama'] or None
cls_module = cls(data)
data = cls_module.sukses()
return self.set_response(data, typ=typ)
def set_failed(self, data=None, message=None, typ="payment"):
self.status = -3
data = data and data or {}
message = message and message or "FAILED"
data.update(dict(denom=self.vendor_produk.produk.kode,
id_pel=self.id_pel,
code=13,
message=message,
status="FAILED"))
return self.set_response(data, typ=typ)
def set_double(self, data=None, message=None, typ="payment"):
self.status = -3
message = message and message or "DOUBLE"
data = data and data or {}
data.update(dict(denom=self.vendor_produk.produk.kode,
id_pel=self.id_pel,
code=67,
message=message,
status="FAILED"))
return self.set_response(data, typ=typ)
def set_pending(self, data=None, message=None, typ="payment"):
self.status = -1
data = data and data or {}
message = "TRX ke {} sedang di proses".format(self.id_pel)
data.update(dict(denom=self.vendor_produk.produk.kode,
id_pel=self.id_pel,
code=68,
status="PENDING",
message=message)
)
return self.set_response(data, typ=typ)
def get_price(self, pokok=0):
product_id = self.invoice_det.produk_id
self.amt_buy = pokok + self.vendor_produk.harga
if hasattr(self.invoice_det, "customer_id"):
customer_id = self.invoice_det.customer_id
else:
customer_id = self.invoice_det.h2h_ar_invoice.customer_id
harga = pokok + self.vendor_produk.produk.harga
self.discount = PartnerProduk.get_discount(customer_id, product_id)
self.amt_sell = harga - self.discount
return dict(
subtotal=int(harga),
discount=int(self.discount),
total=int(self.amt_sell)
)
def save_log(self, typ):
self.invoice_det.status = self.status
if self.result:
if "total" in self.result:
self.amt_sell = self.result["total"]
if "rincian" in self.result and self.result["rincian"] \
and "token" in self.result["rincian"] \
and self.result["rincian"]["token"]:
self.serial_number = self.result["rincian"]["token"]
self.result["serial_number"] = self.serial_number
if hasattr(self.invoice_det, 'serial_number'):
self.invoice_det.serial_number = self.serial_number
if self.vend_inv_no:
self.invoice_det.vend_inv_no = self.vend_inv_no
if self.amt_buy:
self.invoice_det.amt_buy = self.amt_buy
if self.amt_sell:
self.invoice_det.amt_sell = self.amt_sell
if typ == 'inquiry':
self.invoice_det.inquiry = dict(request=self.request,
response=self.response,
result=self.result)
elif typ == 'payment':
self.invoice_det.payment = dict(request=self.request,
response=self.response,
result=self.result)
elif typ == 'advice':
self.invoice_det.advice = dict(request=self.request,
response=self.response,
result=self.result)
elif typ == 'info':
self.invoice_det.info = dict(request=self.request,
response=self.response,
result=self.result)
elif typ == 'notify':
# by tatang
self.invoice_det.notify = dict(request=self.request,
response=self.response,
result=self.result)
flush_row(self.invoice_det)
class Result(object):
def __init__(self, values, **kwargs):
self.values = values
if "rincian" in values:
discount = int(self.values["discount"])
subtotal = int(values["rincian"]["total"])
self.values["subtotal"] = subtotal
self.values["discount"] = discount
self.values["total"] = subtotal - discount
"""
{
"denom": "TSEL5",
"id_pel": "0812121212",
"subtotal": 5675,
"discount": 0,
"total": 5675,
"code":0
"status": "PENDING"
"message":""
}
"""
def set_denom(self):
return {
'denom': self.values['denom'],
'id_pel': self.values['id_pel']}
def set_value(self):
result = {
'subtotal': self.values['subtotal'],
'discount': self.values['discount'],
'total': self.values['total']}
if self.values["nama"]:
result.update({'nama': self.values["nama"]})
serial_numnber = 'serial_number' in self.values and self.values["serial_number"] or ""
result.update({'serial_number': serial_numnber})
return result
def sukses(self):
result = self.set_denom()
result.update(self.set_value())
result.update({'code': 0,
'message': 'Transaksi Berhasil',
'status': 'SUCCESS',
})
return result
def pending(self):
result = self.set_denom()
result.update(self.set_value())
result.update({'code': 68,
'message': 'Silahkan lakukan manual advice setelah beberapa saat',
'status': 'PENDING'})
return result
def gagal(self):
result = self.set_denom()
result.update({'code': 99,
'message': 'Silahkan coba beberapa saat lagi',
'status': 'FAILED',
})
return result
def not_found(self):
result = self.set_denom()
result.update({'code': 55,
'message': 'Data Tidak ditemukan',
'status': 'FAILED',
})
return result
def allready_paid(self):
result = self.set_denom()
result.update({'code': 54,
'message': 'Data sudah dibayar',
'status': 'FAILED',
})
return result
class PulsaResult(Result):
pass
class PajakResult(Result):
pass
class Rincian(Result):
def rincian(self):
pass # Override Please
def sukses(self):
result = super().sukses()
result["rincian"] = self.rincian()
return result
def pending(self):
result = super().pending()
result["rincian"] = self.rincian()
return result
class PlnPreResult(Rincian):
def rincian(self):
log.info("Values: {}".format(self.values))
pokok = self.values["rincian"]["pokok"]
admin = self.values["rincian"]["admin"]
denda = self.values["rincian"]["denda"]
result = dict(
pokok=pokok,
denda=denda,
admin=admin,
total=pokok + denda + admin,
tarif=self.values["rincian"]["tarif"],
no_meter=self.values["rincian"]["no_meter"],
power=self.values["rincian"]["power"],
)
if self.values["rincian"]["token"]:
result.update({'token': self.values["rincian"]["token"]})
return result
class PlnPascaResult(Rincian):
def rincian(self):
pokok = self.values["rincian"]["pokok"]
admin = self.values["rincian"]["admin"]
denda = self.values["rincian"]["denda"]
result = dict(
period=self.values["rincian"]["period"],
jml_bulan=self.values["rincian"]["jml_bulan"],
pokok=pokok,
denda=denda,
admin=admin,
total=pokok + denda + admin,
tarif=self.values["rincian"]["tarif"],
no_meter=self.values["rincian"]["no_meter"],
power=self.values["rincian"]["power"],
)
return result
class PgnResult(Rincian):
def rincian(self):
pokok = self.values["rincian"]["pokok"]
admin = self.values["rincian"]["admin"]
denda = self.values["rincian"]["denda"]
return dict(
pokok=self.values["rincian"]["pokok"],
denda=self.values["rincian"]["denda"],
admin=self.values["rincian"]["admin"],
total=pokok + denda + admin,
period=self.values["rincian"]["period"],
meter=self.values["rincian"]["meter"],
penggunaan=self.values["rincian"]["penggunaan"],
)
class MfResult(Rincian):
def rincian(self):
pokok = self.values["rincian"]["pokok"]
admin = self.values["rincian"]["admin"]
denda = self.values["rincian"]["denda"]
coll_fee = self.values["rincian"]["coll_fee"]
return dict(
pokok=pokok,
denda=denda,
admin=admin,
coll_fee=coll_fee,
total=pokok + denda + admin+ coll_fee,
period=self.values["rincian"]["period"],
jth_tempo=self.values["rincian"]["jth_tempo"],
platform=self.values["rincian"]["platform"],
)
class TelkomResult(Rincian):
def rincian(self):
pokok = self.values["rincian"]["pokok"]
admin = self.values["rincian"]["admin"]
denda = self.values["rincian"]["denda"]
return dict(
period=self.values["rincian"]["period"],
pokok=pokok,
denda=denda,
admin=admin,
total=pokok + denda + admin,
)
class BpjsResult(Rincian):
def rincian(self):
pokok = self.values["rincian"]["pokok"]
admin = self.values["rincian"]["admin"]
denda = self.values["rincian"]["denda"]
return dict(
bill_rest=self.values["rincian"]["bill_rest"],
cabang=self.values["rincian"]["cabang"],
nm_cabang=self.values["rincian"]["nm_cabang"],
anggota=self.values["rincian"]["anggota"],
jml_bulan=self.values["rincian"]["jml_bulan"],
pokok=pokok,
denda=denda,
admin=admin,
total=pokok + denda + admin,
)
class PdamResult(Rincian):
def rincian(self):
pokok = self.values["rincian"]["pokok"]
admin = self.values["rincian"]["admin"]
denda = self.values["rincian"]["denda"]
result = dict(
period=self.values["rincian"]["period"],
jml_bulan=self.values["rincian"]["jml_bulan"],
meter=self.values["rincian"]["meter"],
pokok=pokok,
denda=denda,
admin=admin,
total=pokok + denda + admin,
)
return result
def sha256(hash_string):
return hashlib.sha256(hash_string.encode()).hexdigest()