pdam.py
6.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
from opensipkd.pasar.models import PartnerProduk
import requests
import xmltodict
from . import Vendor as VendorClass, vsi_status
from opensipkd.base.tools import log
class Vendor(VendorClass):
def pars_data(self, data):
product_id = self.invoice_det.produk.id
partner_id = self.invoice_det.h2h_ar_invoice.customer_id
bulan = "totalperiod" in data and data["totalperiod"].split(" ")
bulan = bulan and bulan[0] or 1
discount = PartnerProduk.get_discount(partner_id, product_id) * bulan
admin = self.vendor_produk.produk.harga * bulan
harga = "transamount" in data and data["transamount"] or 0
admin_chg = "admincharge" in data and data["admincharge"] or 0
pokok = harga - admin_chg
subtotal = pokok + admin
total = subtotal - discount
if "status" in data and data["status"] and data["status"] == "SUCCESS":
self.status = 1
elif "status" in data and data["status"] and data["status"] == "ERROR":
self.status = -1
code = "rc" in data and int(data["rc"]) or 500
if code == 0:
self.status = 1
message="SUCCESS"
else:
self.status = vsi_status[str(code)]["status"]
if self.status == -3:
message = "GAGAL"
else:
message = "PENDING"
meter = "startend" in data and data["startend"] or ""
"""
<?xml version="1.0"?>
<root>
<status>SUCCESS</status>
<rc>0000</rc>
<rcm>[0] Cek Tagihan Sukses ke:51110000100 refnum:BE1D2979288141D595C19C2A2A8ADAA4</rcm>
<text>51110000100
Dmmy'Smltor.Name-511
1 BLN
AGU17
RP TAG PDAM RP 270.100
ADM RP 1.600
TOTAL RP 271.700
</text>
<refnum>BE1D2979288141D595C19C2A2A8ADAA4</refnum>
<idpel>51110000100</idpel>
<name>Dmmy'Smltor.Name51110000100</name>
<totalperiod>1 BLN</totalperiod>
<billperiod>AGU17</billperiod>
<admincharge>1600</admincharge>
<transamount>271700</transamount>
</root>
"""
"""
{
"data": [{
"nopel": "636289",
"nama": "SUBCRIBER NAME",
"refno": "25641544",
"rincian":{
"pokok": 68000,
"denda": 0,
"admin": 2500,
"period": "2016-07",
"jml_bulan": 1,
"meter": "140-162"
},
"subtotal": 70500,
"discount": 600,
"total": 69900
}]
"""
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": "name" in data and data["name"] or "NO-NAME",
"rincian":{
"pokok": pokok,
"denda": 0,
"admin": admin,
"periode": "billperiod" in data and data["billperiod"] 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": total,
}
def save_response(self, data, typ="inquiry"):
self.response = data
log.info("Response: %s", data)
self.save_log(typ)
return data
def inquiry(self):
params = self.get_params(cmd='INQ', modul='PDAM')
params["idpel"] = self.id_pel
params["biller"] = self.vendor_produk.kode
self.request = params
self.save_log('inquiry')
params['trxid'] = self.invoice_det.id
log.info("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.save_response(data)
if resp and resp.ok:
data = dict(xmltodict.parse(resp.text)["root"])
log.info("Response: %s", data)
data = self.pars_data(data)
elif resp:
data = dict(message=resp.text,
code=resp.status_code)
else:
data = dict(message="No Response From Biller ",
code=500)
return self.save_response(data)
return self.save_response(data)
def payment(self):
params = self.get_params(cmd='PAY', modul='PDAM')
params["idpel"] = self.id_pel
self.request = params
self.save_log('payment')
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)
except:
data = dict(code=500,
message="No Response From Biller")
self.response = data
data = self.pars_data(data)
self.save_log('payment')
return data
def advice(self):
params = self.get_params(cmd='ADV', modul='pln')
params["idpel"] = self.id_pel
self.request = params
self.save_log('advice')
try:
resp = requests.get(self.url, params=params, timeout=15)
if resp.ok:
data = dict(xmltodict.parse(resp.text)["root"])
self.response = data
else:
data = dict(code=resp.status_code,
message=resp.text)
self.response = data
except:
data = dict(code=500,
message="No Response From Biller")
self.response = data
data = self.pars_data(data)
self.result = data
self.save_log('advice')
return data