pln_post.py
8.79 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
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):
"""
<?xml version="1.0"?>
<root>
<status>SUCCESS</status>
<rc>0000</rc>
<rcm>[0] CEK TAGIHAN SUKSES ke:532110000060 refnum:33ECE3543E62474AA0C6C3147D94A31D</rcm>
<text>532110000060
DU'MMY-VSI-532110000
1 BLN
JUN16
RP TAG PLN RP 887.817
ADM RP 1.600
TOTAL RP 890.929
</text>
<refnum>33ECE3543E62474AA0C6C3147D94A31D</refnum>
<idpel>532110000060</idpel>
<nama>DU'MMY-VSI-532110000060</nama>
<bulan>1 BLN</bulan>
<blntagihan>JUN16</blntagihan>
<admin>1600</admin>
<tagihan>889329</tagihan>
<total>890929</total>
</root>
"""
"""
<?xml version="1.0"?>
<root>
<status>SUCCESS</status>
<rc>0000</rc>
<rcm>[0] PEMBAYARAN SUKSES ke:532110000060 refnum:0506213515100D3399B27293E3DBC535</rcm>
<text>22/06/16 14:29
0506213515100D3399B27293E3DBC535
532110000060
3
DU'MMY-VSI-532110000
R1/450
BLN JUN16
RP 887.729
ADM RP 1.600
TOTAL RP 890.929
SUKSES</text>
<date>2016-06-22 14:29:25</date>
<refnum>0506213515100D3399B27293E3DBC535</refnum>
<idpel>532110000060</idpel>
<nama>DU'MMY-VSI-532110000060</nama>
<segmenpower>R1/450</segmenpower>
<startend>888800-889900</startend>
<blntagihan>JUN16</blntagihan>
<ppn>0</ppn>
<denda>0</denda>
<totaltagihanpln>887729</totaltagihanpln>
<admin>1600</admin>
<total>890929</total>
<info1>Informasi Hubungi Call Center: 123 Atau Hub. PLN Terdekat:</info1>
<info2>Terima Kasih</info2>
</root>
"""
power = "segmenpower" in data and data["segmenpower"].split("/") or [""]
power = len(power)>1 and power[-1:][0] or power[0]
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
discount = PartnerProduk.get_discount(customer_id, product_id)
admin = self.vendor_produk.produk.harga
harga = "nominal" in data and data["nominal"] or 0
subtotal = harga + admin
token = "token" in data and data["token"] or ""
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
meter = "startend" in data and data["startend"] or ""
"""
{
"data": [{
"nopel": "530678910024",
"nama": "SUBCRIBER NAME",
"refno": "25641544",
"rincian":{
"pokok": 300000,
"denda": 0,
"admin": 2500,
"period": "2016-08",
"jml_bulan": 1,
"tarif": "R1\/1300VA",
"no_meter": "530678910012",
"meter": "567-756"
},
"subtotal": 302500,
"discount": 800,
"total": 301700
}]
"""
bulan = "bulan" in data and data["bulan"].split(" ")
bulan = bulan and bulan[0] or 0
code = "rc" in data and data['rc']=='0000' and 0 or 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"
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,
"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_pending(data)
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
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
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 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 = "ref_num" in data and data["ref_num"] or self.vend_inv_no
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