__init__.py
7.31 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
import transaction
from ISO8583.ISOErrors import BitNotSet
from deform import (
ValidationFailure,
)
from iso8583_web.scripts.logger import (
log_web_info,
log_web_debug)
from iso8583_web.scripts.views import WebJob
from opensipkd.string import (
FixLength,
)
from .structure import INVOICE_PROFILE, InquiryResponse, PaymentResponse
from .structure import Transaksi
from .. import View as BaseView
# View as BaseView,
from ..linkaja import (
WebJob, is_inquiry, get_db_session, is_reversal, is_payment, get_method,
get_form, date_from_str, csv_method, get_inquiry,
get_payment)
from ..linkaja.exceptions import (
InvoiceIdError, NeedPostError, InternalError, HostError, AlreadyPaidError,
BaseError, AmountError, BillRefNotFound, PaymentNotFound,
)
from ..linkaja.models import (Rpc, LogSam)
ROUTE = 'linkaja/sambat'
conf = dict()
def get_template_response(data):
if is_inquiry(data):
return InquiryResponse()
d = PaymentResponse()
d['Bill Ref'] = data['bill_ref']
return d
class View(BaseView):
def get_web_job_cls(self): # Override
return WebJob
def not_found_error(self, hostname): # Override
return HostError(hostname)
def not_running_error(self, hostname): # Override
msg = 'Host {} belum terhubung'.format(hostname)
return InternalError(msg, 'Sedang offline')
def create_iso_log(self, iso, rpc):
conf = self.get_iso_conf()
iso_log = LogSam(
mti=iso.mti,
rpc_id=rpc.id,
ip=conf['ip'],
conf_name=conf['name'],
message=iso.data,
bit_003=iso.data["processingCode"])
try:
data = iso.data["additionalData"]
if data:
log_web_debug(data)
profile = FixLength(INVOICE_PROFILE)
profile.set_raw(data)
iso_log.bit_062_data = profile.to_dict()
except BitNotSet:
pass
return iso_log
def before_send_iso(self, data, inq, pay, req):
DBSession = get_db_session()
web_conf = self.get_web_conf()
row = Rpc(
ip=self.request.client_addr,
conf_name=web_conf['name'],
merchant=data['merchant'],
terminal=data['terminal'],
trx_type=data['trx_type'],
msisdn=data['msisdn'],
acc_no=data['acc_no'],
stan=req.get_stan())
row.trx_date = date_from_str(data['trx_date']),
if data.get('msg'):
row.msg = data['msg']
if data.get('amount'):
row.amount = int(data['amount'])
if not is_inquiry(data):
row.inquiry_id = inq and inq.id or pay.inquiry_id
row.ntb = data['trx_id']
with transaction.manager:
DBSession.add(row)
DBSession.flush()
DBSession.expunge_all() # Agar dapat row.id
iso_log = self.create_iso_log(req, row)
DBSession.add(iso_log)
return row
def after_send_iso(self, data, inq, pay, row, iso_resp):
DBSession = get_db_session()
iso_log = self.create_iso_log(iso_resp, row)
iso_data = iso_resp.response
web_data = get_template_response(data)
if iso_data["responseCode"] == '00':
web_data['Response Code'] = '00'
if is_inquiry(data):
web_data['Bill Ref'] = str(row.id)
if iso_data["additionalData"]:
profile = FixLength(INVOICE_PROFILE)
profile.set_raw(iso_data["additionalData"])
iso_log.bit_062_data = profile.to_dict()
web_data['Biller Name'] = row.biller_name = \
' '.join([profile['NOMOR POLISI'], profile['NAMA PEMILIK'].strip()])
web_data['Bill Data'] = profile.to_dict()
web_data['Bill Amount'] = iso_data["amountTransaction"].lstrip('0')
if "numberTransactionP" in iso_data and iso_data["numberTransactionP"]:
web_data['Transaction ID'] = row.ntp = iso_data["numberTransactionP"] # NTP
err = None
elif iso_data["responseCode"] in ['33', '55']:
err = InvoiceIdError()
elif iso_data["responseCode"] == '54':
if is_reversal(data):
err = PaymentNotFound()
else:
err = AlreadyPaidError()
elif iso_data["responseCode"] == '51':
err = AmountError()
else:
err = BaseError()
if err:
web_data.from_err(err)
row.resp_msg = web_data['Notification Message']
self.log_send(web_data.values)
row.resp_code = web_data['Response Code']
row.resp_msg = web_data['Notification Message']
with transaction.manager:
DBSession.add(row)
DBSession.add(iso_log)
return web_data
def get_response(self, data):
# todo: merubah get response
log_web_debug("Start Response")
p = dict(invoice_id=data['acc_no'])
inq = pay = None
if not is_inquiry(data):
p['amount'] = data['amount']
p['ntb'] = data['trx_id']
if is_payment(data):
inq = get_inquiry(data)
if not inq:
print("Inquiry Not Found")
raise BillRefNotFound()
else:
pay = get_payment(data)
if not pay:
print("Payment Not Found")
raise BillRefNotFound()
p['stan'] = pay.stan
# conn = self.get_connection()
method = get_method(data)
# iso_func = getattr(conn.job, method)
iso_req = Transaksi(p, method, inq, pay, conf)
row = self.before_send_iso(data, inq, pay, iso_req)
iso_req.send()
if type(iso_req.response) != dict:
raise BaseError
iso_req.data = iso_req.response
return self.after_send_iso(data, inq, pay, row, iso_req)
@csv_method(route_name=ROUTE)
def view_trx_sambat(self):
self.validate()
if not self.request.POST:
self.log_receive('GET {}'.format(self.request.GET))
raise NeedPostError()
items = self.request.POST.items()
self.log_receive('POST {}'.format(dict(items)))
items = self.request.POST.items()
form = get_form()
try:
c = form.validate(items)
except ValidationFailure as e:
d = e.error.asdict()
msg = e.error
for field, err in d.items():
msg = '{} {}'.format(field, err)
break
raise InternalError(msg)
data = dict(c)
return self.get_response(data)
# Dipanggil read_conf.py
def init(cfg):
conf.update(cfg)
# Dipanggil forwarder.py
def pyramid_init(config):
# config.add_renderer(RENDERER, 'iso8583_web.scripts.views.linkaja.Renderer')
config.add_route(ROUTE, conf['route_path'])
log_web_info('LinkAja Sambat route path {}'.format(conf['route_path']))
# pool_size = int(conf.get('db_pool_size', 50))
# max_overflow = int(conf.get('db_max_overflow', 100))
# engine = create_engine(
# conf['db_url'], pool_size=pool_size, max_overflow=max_overflow)
# session_factory = sessionmaker(bind=engine)
# conf['db_session'] = scoped_session(session_factory)
# register(conf['db_session'])