pay_request.py
15.4 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
import colander
import requests
import json
from datetime import datetime
from opensipkd.base.views import BaseView
from pyramid.view import view_config
from deform import widget, Form
from pyramid.httpexceptions import HTTPFound
from ..models import (Produk, DBSession, ProdukKategori)
from sqlalchemy import or_
from opensipkd.base import get_settings
from ..tools import json_rpc_header
from opensipkd.base.models import Partner
from deform.form import Button
from ..tools import btn_add, btn_delete
form_params_view = dict(
# scripts="""
# $(document).ready(function(){
# $("form#deform input[type=text]").each(function() {
# var input = $(this).prop("disabled", false);
# })
# $("form#deform textarea").each(function() {
# var input = $(this).prop("disabled", false);
# })
# });
# """
scripts = ""
)
# @colander.deferred
# def deffered_produk(node, kw):
# values = kw.get('daftar_produk', [])
# # print('isi daftar_kelurahan >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
# # print(values)
# return widget.SelectWidget(values=values)
def get_products_e_det():
query = DBSession.query(Produk.id, ProdukKategori.nama.label('kat'), Produk.harga)\
.join(ProdukKategori, Produk.kategori_id == ProdukKategori.id) \
.filter(or_(ProdukKategori.kode.ilike("e-%"),
ProdukKategori.kode.in_(['pemda', 'pajak'])
)
).all()
return query
def get_products_e():
query = DBSession.query(Produk.id, Produk.nama)\
.join(ProdukKategori, Produk.kategori_id == ProdukKategori.id) \
.filter(or_(ProdukKategori.kode.ilike("e-%"),
ProdukKategori.kode.in_(['pemda', 'pajak'])
)
).all()
return query
btn_TambahItem = Button('add_item', type= 'button', title='Tambah', css_class="btn-success")
btn_HapusItem = Button('del_item', type= 'button', title='Hapus', css_class="btn-danger")
class ParamsSchema(colander.Schema):
time_stamp = colander.SchemaNode(colander.String(), oid="time_stamp", widget=widget.HiddenWidget(readonly=True))
currency = colander.SchemaNode(colander.String(), title = "Mata Uang ", widget=widget.HiddenWidget(readonly=True))
amount = colander.SchemaNode(colander.String(), title = "Total Bayar")
invoice_no = colander.SchemaNode(colander.String(), title = "Nomor Invoice")
response_url = colander.SchemaNode(colander.String(), title = "Response URL")
description = colander.SchemaNode(colander.String(), title = "Deskripsi Transaksi", missing=colander.drop)
fee = colander.SchemaNode(colander.Integer(), title = "Fee")
vat = colander.SchemaNode(colander.Integer(), title = "VAT")
notax_amt = colander.SchemaNode(colander.String(), title = "No tax amount")
req_dt = colander.SchemaNode(colander.String(), title = "Tanggal ", widget=widget.HiddenWidget(readonly=True))
req_tm = colander.SchemaNode(colander.String(), title = "Jam ", widget=widget.HiddenWidget(readonly=True))
class BillerSchema(colander.Schema):
name_biller = colander.SchemaNode(colander.String(), title = "Nama ")
phone_biller = colander.SchemaNode(colander.String(), title = "No. Telp ")
email_biller = colander.SchemaNode(colander.String(), title = "Email")
address_biller = colander.SchemaNode(colander.String(), title = "Alamat", missing=colander.drop)
city_biller = colander.SchemaNode(colander.String(), title = "Kota")
state_biller = colander.SchemaNode(colander.Integer(), title = "Provinsi")
post_code_biller = colander.SchemaNode(colander.Integer(), title = "Kode Pos")
country_biller = colander.SchemaNode(colander.String(), title = "Negara")
class CustomerSchema(colander.Schema):
name_cust = colander.SchemaNode(colander.String(), title = "Nama ")
phone_cust = colander.SchemaNode(colander.String(), title = "No. Telp ")
email_cust = colander.SchemaNode(colander.String(), title = "Email")
address_cust = colander.SchemaNode(colander.String(), title = "Alamat", missing=colander.drop)
city_cust = colander.SchemaNode(colander.String(), title = "Kota")
state_cust = colander.SchemaNode(colander.Integer(), title = "Provinsi")
post_code_cust = colander.SchemaNode(colander.Integer(), title = "Kode Pos")
country_cust = colander.SchemaNode(colander.String(), title = "Negara")
ip_cust = colander.SchemaNode(colander.String(), oid="ip_cust", widget=widget.HiddenWidget(readonly=True))
session_id_cust = colander.SchemaNode(colander.Integer(), oid="session_id_cust", widget=widget.HiddenWidget(readonly=True))
agent_cust = colander.SchemaNode(colander.Integer(), oid="agent_cust", widget=widget.HiddenWidget(readonly=True))
language_cust = colander.SchemaNode(colander.String(), oid="language_cust", widget=widget.HiddenWidget(readonly=True))
class DeliverToSchema(colander.Schema):
name_deliver = colander.SchemaNode(colander.String(), title = "Nama ")
phone_deliver = colander.SchemaNode(colander.String(), title = "No. Telp ")
email_deliver = colander.SchemaNode(colander.String(), title = "Email", missing=colander.drop)
address_deliver = colander.SchemaNode(colander.String(), title = "Alamat", missing=colander.drop)
city_deliver = colander.SchemaNode(colander.String(), title = "Kota")
state_deliver = colander.SchemaNode(colander.Integer(), title = "Provinsi")
post_code_deliver = colander.SchemaNode(colander.Integer(), title = "Kode Pos")
country_deliver = colander.SchemaNode(colander.String(), title = "Negara")
class ItemsSchema(colander.Schema):
img_url = colander.SchemaNode(colander.String(), title = "URL Gambar ")
goods_name = colander.SchemaNode(colander.String(), title = "Nama Produk")
goods_detail = colander.SchemaNode(colander.String(), title = "Detail")
goods_amt = colander.SchemaNode(colander.String(), title = "Harga")
qty = colander.SchemaNode(colander.String(), title = "Qty")
subtotal = colander.SchemaNode(colander.String(), title = "Subtotal")
class CartSchema(colander.Schema):
count = colander.SchemaNode(colander.String(), title = "Jumlah", oid="count")
class ServerSchema(colander.Schema):
domain_server = colander.SchemaNode(colander.String(), oid = "domain_server", missing=colander.drop)
ip_server = colander.SchemaNode(colander.String(), oid = "ip_server", missing=colander.drop)
def form_validator(form, value):
pass
def get_form(request, class_form, buttons=None, row=None):
schema = class_form(validator=form_validator)
# schema = class_form()
# schema = schema.bind(
# daftar_produk=Produk.get_e_list()
# )
schema.request = request
if row:
schema.deserialize(row)
if not buttons:
# buttons = (btn_inquiry, btn_reset)
buttons = ()
return Form(schema, buttons=buttons)
class view_pay(BaseView):
@view_config(route_name = 'pay-request', permission = 'pay-request', renderer = 'templates/pay_request.pt')#permission = 'pay-form',
@view_config(route_name = 'pay-request', request_param='fmt=lanjut', renderer='templates/pay.pt')
def the_form(self):
request = self.req
session = self.ses
url_dict = request.matchdict
buttons_cart = (btn_TambahItem,)
formParams = Form(ParamsSchema())
formCart = get_form(request, CartSchema, buttons_cart)
itemCartSchema = ItemsSchema()
itemCartSchema.request = request
formItemCart = Form(itemCartSchema)
billerSchema = BillerSchema()
billerSchema.request = request
formBiller = Form(billerSchema)
customerSchema = CustomerSchema()
customerSchema.request = request
formCustomer = Form(customerSchema)
deliverSchema = DeliverToSchema()
deliverSchema.request = request
formDeliver = Form(deliverSchema)
formServer = ServerSchema()
formServer.request = request
formServer = Form(formServer)
post_data = request.POST
if post_data:
items = dict(post_data)
if 'lanjut' in post_data and 'bayar' not in post_data:
produks = []
for key in items:
print(key)
if key[:10] == 'goods_name':
uniq = key.split('-')
uniq = uniq[1]
produk = (items[key], items['goods_detail-' + uniq], "{:n}".format(int(items['goods_amt-' + uniq])), items['img_url-' + uniq])
produk = dict(
img_url = items['img_url-' + uniq],
goods_name = items[key],
goods_detail = items['goods_detail-' + uniq],
goods_amt = items['goods_amt-' + uniq],
qty = items['qty-' + uniq],
subtotal = items['subtotal-' + uniq]
)
produks.append(produk)
# REQUEST PAYMENT
print("isi session di pay request >>>>>>>>>>>>>>>>>>>>>>>>.")
print(request.session)
print("isi items >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(items)
partner_kd = request.session['partner_kd']
customer = Partner.query().filter(Partner.kode == partner_kd).first()
user = customer.users
print("isi user >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(user.user_name)
print(user.api_key)
headers = json_rpc_header(user.user_name, user.api_key)
print("isi header >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
# print(header)
settings = get_settings()
hostnya = settings['_host']
url = hostnya + "/api/merchant"
time_stamp = datetime.now().strftime('%Y%m%d%H%M%S')
req_dt = datetime.now().strftime('%Y%m%d')
req_tm = datetime.now().strftime('%H%M%S')
body = {
"jsonrpc": "2.0",
"id": "1",
"method": "pay_request",
"params": {
"data": {
"response_url": items['response_url'],
"time_stamp": time_stamp,
"currency": "IDR",
"amount": items['amount'],
"invoice_no": items['invoice_no'],
"description": items['description'],
"fee": items['fee'],
"vat": items['vat'],
"notax_amt": items["notax_amt"],
"req_dt": req_dt,
"req_tm": req_tm,
"biller": {
"name": items['name_biller'],
"phone": items['phone_biller'],
"email": items['email_biller'],
"address": items['address_biller'],
"city": items['city_biller'],
"state": items['state_biller'],
"post_code": items['post_code_biller'],
"country": items['country_biller']
},
"customer": {
"name": items['name_cust'],
"phone": items['phone_cust'],
"email": items['email_cust'],
"address": items['address_cust'],
"city": items['city_cust'],
"state": items['state_cust'],
"post_code": items['post_code_cust'],
"country": items['country_cust'],
"ip": "127.0.0.1",
"session_id": "697D6922C961070967D3BA1BA5699C2C",
"agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
"language": "en-US"
},
"deliver_to": {
"name": items['name_deliver'],
"phone": items['phone_deliver'],
"email": items['email_deliver'],
"address": items['address_deliver'],
"city": items['city_deliver'],
"state": items['state_deliver'],
"post_code": items['post_code_deliver'],
"country": items['country_deliver']
},
"cart": {
"count": len(produks),
"item": produks
},
"server": {
"domain": "merchant.com",
"ip": "127.0.0.1"
}
}
}
}
print('isi json >>>>>>>>>>>>>>>>>>>>>>>>')
print(json.dumps(body))
r = requests.post(url, data=json.dumps(body), headers=headers)
dict_results = json.loads(r.text)
print('result >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
print(dict_results)
a = request.route_url(route_name='pay-form', pay_token = dict_results['result']['pay_token'])
# print('isi request route >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
# print(a)
# return HTTPFound(location = 'http://localhost:6543/pay/' + dict_results['result']['token'])
return HTTPFound(location = a )
# def jprint(obj):
# text = json.dumps(obj, sort_keys=True, indent=4)
# print(text)
# if 'result' in r.json():
# jprint(r.json())
# va_number = r.json()['result']['token']
# print('tokeeeeeen >>>>>>>>>>>>>>>>>>>>>>>>>>>.')
# print(va_number)
# else:
# jprint(r.json())
# err_code = r.json()['error']['code']
# err_msg = r.json()['error']['message']
# raise custom_error(err_code, err_msg)
listOfTuple = get_products_e_det()
icikiwir = []
for item in listOfTuple:
item = list(item)
icikiwir.append(item)
return dict(
formParams = formParams,
formBiller = formBiller,
formCustomer = formCustomer,
formDeliver = formDeliver,
formServer = formServer,
formCart = formCart,
formItemCart = formItemCart,
params = form_params_view,
produks = get_products_e(),
itemDet = icikiwir
)