-
Notifications
You must be signed in to change notification settings - Fork 1
/
pos_pec.py
208 lines (182 loc) · 6.16 KB
/
pos_pec.py
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
import os
from gui_for_message import tk_gui
from .db.sqlite import SqliteDb
import json as jsn
from dotenv import load_dotenv
load_dotenv()
def pec(doch_id, price_to_send, gui_tray):
pec_service_api_dir = os.getenv('PEC_API_DIR')
# Init Status
stat = 'بدون وضعیت'
stat_flg = True
# Check for install service
if not os.path.exists(pec_service_api_dir):
stat = 'سرویس تاپ نصب نیست'
stat_flg = False
pec_gui = tk_gui()
pec_gui.dialog(
'button_3.png',
gui_tray.stop,
True,
None,
None,
stat+'\nلطفا سرویس را نصب کنید\nو برنامه را مجدد باز کنید'
)
# Get last sent-pay
sqlite = SqliteDb()
sqlite.execute('''
SELECT * FROM pay ORDER BY rowid DESC;
''')
last_pay_record_id = sqlite.fetchone()
# Remove TransAction - if removed
if last_pay_record_id[0] > doch_id:
sqlite.execute('''
DELETE FROM Pay WHERE id='{}';
'''.format(last_pay_record_id[0]))
sqlite.commit()
# Close sqlite connection
sqlite.close()
# Do a new TransAction if it's a new
elif last_pay_record_id[0] < doch_id:
# Request file
request_file = os.path.join(
pec_service_api_dir+'request/', 'TransAction.txt'
)
# Response file
response_file = os.path.join(
pec_service_api_dir+'response/', 'TransAction.txt'
)
# Clean old result (if exist)
if os.path.exists(response_file):
not_remove = True
while not_remove:
try:
os.remove(response_file)
not_remove = False
except:
pass
# Write new TransAction request for send to pay-terminal
def write_request():
if os.path.exists(request_file):
not_remove = True
while not_remove:
try:
os.remove(request_file)
not_remove = False
except:
pass
file = open(request_file, 'w')
file.write(
'Amount={}\ntype={}\nIP={}\nport={}'.format(
price_to_send, os.getenv('PEC_CON_TYPE'), os.getenv(
'PEC_DEVICE_IP'), os.getenv('PEC_DEVICE_PORT')
)
)
file.close()
# TransAction is Sent?
def check_for_sent():
not_sent = True
while (not_sent):
if (os.path.exists(request_file)):
pass
else:
not_sent = False
global stat
stat = 'درخواست ارسال شد'
# Check for receive response
def check_for_receive():
response_is_not_exits = True
while (response_is_not_exits):
if (os.path.exists(response_file)):
response_is_not_exits = False
global stat
stat = 'نتیجه درخواست - آمد'
else:
pass
# TransAction
def do_trans_action():
try:
os.remove(response_file)
except:
pass
# Prepare request
write_request()
check_for_sent()
check_for_receive()
# Do TransAction
do_trans_action()
# For cancell TransAction
not_cancel = True
def abort_pay():
global not_cancel
not_cancel = False
# Check response for resend or done the mission
while (not_cancel):
# know errors
def error_message(r):
if r == '00':
return 'تراکنش موفق'
elif r == '99':
return 'لغو توسط کاربر'
elif r == '51':
return 'عدم موجودی کافی'
elif r == '55':
return 'رمز نامعتبر است'
else:
return 'خطای ناشناخته'
# Open response file
file = open(response_file, 'r')
# Read a line of file and close & remove it
txt = file.readline()
file.close()
# Get responseCode
etxt = txt.split()
result = '01'
try:
result = etxt[2]
except:
pass
# Create Json Result
global pec_json
json_text = '{ "PcPosStatusCode":"'+result+'", "PcPosStatus":"' + \
stat+'", "ResponseCodeMessage":"' + \
error_message(result)+'"}'
pec_json = jsn.loads(json_text)
# For delete extra Result File
def remove_result():
not_remove = True
while not_remove:
try:
os.remove(response_file)
not_remove = False
except:
pass
# Success TransAction
if result == '00':
remove_result()
break
# Fail TransAction -> Show Error
else:
# Gui
pec_gui = tk_gui()
pec_gui.show_message(
do_trans_action, abort_pay, pec_json, 'تاپ')
if not_cancel == False:
remove_result()
# Show result in terminal
print('*********[Pec]*********')
for key in pec_json:
value = pec_json[key]
print(key, ' : ', value)
print('*******[End-Pec]*******')
# Save result in sqlite pay table
sqlite.execute('''
INSERT INTO pay(
id, price, status
)VALUES(
{}, {}, {}
)
'''.format(doch_id, price_to_send, pec_json['PcPosStatusCode']))
sqlite.commit()
# Close sqlite connection
sqlite.close()