pln_post.py
7.26 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
from opensipkd.pasar.models import PartnerProduk
import requests
import xmltodict
from . import Vendor as VendorClass, vsi_status
import logging
log = logging.getLogger(__name__)
class Vendor(VendorClass):
def pars_data(self, data):
pokok = "tagihan" in data and int(data["tagihan"]) or 0
self.amt_buy = "total" in data and int(data["total"]) or 0
tarif = "tarif" in data and data["tarif"] or ""
p = tarif.split("/")
power = len(p)>1 and p[1] or ""
product_id = self.invoice_det.produk.id
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
bulan = "bulan" in data and data["bulan"].split()[0] or 1
bulan = bulan and int(bulan) or 1
discount = int(PartnerProduk.get_discount(customer_id, product_id) * bulan)
admin = int(self.vendor_produk.produk.harga * bulan)
subtotal = pokok + admin
meter = "standmeter" in data and data["standmeter"] or ""
if "rc" in data:
if data['rc']=='0000':
code = 0
else:
code = int(data["rc"])
else:
code = 500
# code = "rc" in data and data['rc']=='0000' and 0 or int(data["rc"]) or 500
if code == 0:
return {
"code": 0,
"status": "SUCCESS",
"message": "rcm" in data and "Transaksi berhasil",
"nama": "nama" in data and data["nama"] or "NO-NAME",
"rincian": {
"pokok": pokok,
"denda": 0,
"admin": admin,
"total": subtotal,
"period": "blth" in data and data["blth"] or "",
"jml_bulan": bulan,
"tarif": tarif,
"no_meter": data["subid"] or "",
"power": power,
"meter": meter,
},
"subtotal": subtotal,
"discount": discount,
"total": subtotal - discount,
}
else:
self.status = vsi_status[str(code)]["status"]
if self.status == -3:
message = "GAGAL"
else:
message = "PENDING"
if code==14:
code = 54
message = "GAGAL"
return {
"code": code,
"status": message,
"message": "rcm" in data and "Transaksi %s" % message or "No Response From Biller",
"refno": "refnum" in data and data["refnum"] or "",
"nopel": "idpel" in data and data["idpel"] or self.id_pel,
"nama": "nama" in data and data["nama"] or "NO-NAME",
"rincian":{
"pokok": "tagihan" in data and int(data["tagihan"]) or 0,
"denda": "denda" in data and int(data["denda"]) or 0,
"admin": admin,
"total": subtotal,
"periode": "blntagihan" in data and data["blntagihan"] or "",
"jml_bulan": bulan,
"tarif": "tarifdaya" in data and data["tarifdaya"] or "",
"no_meter": "msn" in data and data["msn"] or "",
"power": power,
"meter": meter,
},
"subtotal": subtotal,
"discount": discount,
"total": subtotal-discount,
}
def save_response(self, data, typ="inquiry"):
self.response = data
log.info("VSI PLN PRE Response: %s", data)
self.save_log(typ)
return data
def inquiry(self):
params = self.get_params(cmd='INQ', modul='pln')
params["idpel"] = self.id_pel
self.request = params
self.save_log('inquiry')
params['trxid'] = self.invoice_det.id
log.info("VSI PLN PRE Request: %s", params)
try:
resp = requests.get(self.url, params=params, timeout=15)
except requests.exceptions.RequestException as e:
data = dict(message = e.strerror,
code = e.errno)
return self.set_failed(data)
log.info(resp)
if resp and resp.ok and resp.text:
log.info(resp.text)
if resp and resp.ok:
data = dict(xmltodict.parse(resp.text)["root"])
self.response = data
data = self.pars_data(data)
if data["code"]!=0:
return self.set_failed()
self.result = data
log.info(data)
return self.set_success(data)
elif resp:
data = dict(message=resp.text,
code=resp.status_code)
self.response = data
return self.set_failed(data)
else:
data = dict(message="No Response From Biller ",
code=500)
return self.set_failed(data)
def payment(self):
params = self.get_params(cmd='PAY', modul='pln')
params["idpel"] = self.id_pel
# params["nominal"] = self.v_produk_kd
self.request = params
# todo: apakah harus di cek dulu data inquirynya
self.save_log('payment')
params['trxid'] = self.invoice_det.id
resp = requests.get(self.url, params=params, timeout=15)
try:
if resp.ok:
data = dict(xmltodict.parse(resp.text)["root"])
else:
data = dict(code=resp.status_code,
message=resp.text)
self.response = data
log.info("VSI Payment Result: {}".format(self.response))
# except requests.exceptions.Timeout as errt:
# message = "Biller Timeout Transaksi will be retry"
# self.status = -1
# return self.set_pending(data, message=message)
# except requests.exceptions.RequestException as e:
# message = "Biller Error Transaksi will be retry"
# self.status = -1
# return self.set_pending(data, message=message)
except:
self.status = 0
#self.response = dict(code=resp.status_code, message=resp.text)
log.info("Payment Response: %s" % (self.response))
return self.set_pending()
#self.amt_buy = "harga" in data and data["harga"] or self.amt_buy
self.vend_inv_no = str(self.invoice_det.id)
data = self.pars_data(self.response)
return self.set_success(data)
def advice(self):
params = self.get_params(cmd='ADV', modul='pln')
params["idpel"] = self.id_pel
#params["nominal"] = self.v_produk_kd
self.request = params
# todo: apakah harus di cek dulu data inquirynya
self.save_log('advice')
try:
resp = requests.get(self.url, params=params, timeout=15)
if resp.ok:
data = dict(xmltodict.parse(resp.text)["root"])
else:
data = dict(code=resp.status_code,
message=resp.text)
self.response = data
except:
data = dict(code=500,
message="No Response From Biller")
data = self.pars_data(data)
self.result = data
self.save_log('advice')
return data