liuye
authored
|
1
2
3
4
5
6
7
|
// D:\GitProject\etradeclient\central_clearing_system\ETradeClient\mfc_ui\pay_dlg.cpp : 实现文件
//
#include "stdafx.h"
#include "ETradeClient.h"
#include "D:\GitProject\etradeclient\central_clearing_system\ETradeClient\mfc_ui\pay_dlg.h"
#include "afxdialogex.h"
|
liuye
authored
|
8
9
|
#include "etradeclient/hardware/hardware_cmd.h"
#include "etradeclient/boost_patch/property_tree/json_parser.hpp"
|
liuye
authored
|
10
11
12
13
14
15
16
17
18
19
20
21
|
#include "ETradeClient/utility/win_msg_define.h"
#include "ETradeClient/utility/logging.h"
#include "etradeclient/utility/url_config.h"
#include "etradeclient/utility/application_config.h"
#include "etradeclient/browser/session.h"
#include "ETradeClient/utility/string_converter.h"
#include "ETradeClient/utility/win_msg_define.h"
#include <vector>
#include "pay_dlg.h"
#include "etradeclient/hardware/hardware_cmd.h"
#include "ETradeClient/utility/logging.h"
#include <sstream>
|
liuye
authored
|
22
|
#include "LoginDialog.h"
|
liuye
authored
|
23
|
#include "place_order_dlg.h"
|
liuye
authored
|
24
|
|
liuye
authored
|
25
26
27
28
29
|
// PayDlg 对话框
IMPLEMENT_DYNAMIC(PayDlg, CDialogEx)
|
liuye
authored
|
30
|
PayDlg::PayDlg(long statement_id, CString total_money, int card_type, CWnd* pParent /*=NULL*/)
|
liuye
authored
|
31
32
|
: CDialogEx(PayDlg::IDD, pParent)
{
|
liuye
authored
|
33
34
35
|
statement_id_ = statement_id;
total_money_ = total_money;
card_type_ = card_type;
|
liuye
authored
|
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
}
PayDlg::~PayDlg()
{
}
void PayDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PAY_STATIC_TOTAL_MONEY, total_money_static_);
DDX_Control(pDX, IDC_PAY_STATIC_REAL_MONEY, real_money_static_);
DDX_Control(pDX, IDC_PAY_STATIC_PASSWORD, password_static_);
DDX_Control(pDX, IDC_PAY_EDIT_PASSWORD, password_edit_);
DDX_Control(pDX, IDC_PAY_BUTTON_PAY, pay_button_);
DDX_Control(pDX, IDC_PAY_BUTTON_CLOSE, close_button_);
DDX_Control(pDX, IDC_PAY_BUTTON_PASSWORD, password_button_);
|
liuye
authored
|
52
|
DDX_Control(pDX, IDC_STATIC_ERROR_MSG, error_msg_static_);
|
liuye
authored
|
53
54
55
56
57
|
}
BEGIN_MESSAGE_MAP(PayDlg, CDialogEx)
ON_BN_CLICKED(IDC_PAY_BUTTON_CLOSE, &PayDlg::OnBnClickedPayButtonClose)
|
liuye
authored
|
58
|
ON_BN_CLICKED(IDC_PAY_BUTTON_PAY, &PayDlg::OnBnClickedPayButtonPay)
|
liuye
authored
|
59
|
ON_WM_CTLCOLOR()
|
liuye
authored
|
60
|
ON_BN_CLICKED(IDC_PAY_BUTTON_PASSWORD, &PayDlg::OnBnClickedPayButtonPassword)
|
liuye
authored
|
61
|
ON_COMMAND(ID_ACCELERATOR_A, &PayDlg::OnBnClickedPayButtonPassword)
|
liuye
authored
|
62
|
ON_COMMAND(ID_ACCELERATOR_F, &PayDlg::OnBnClickedPayButtonPay)
|
liuye
authored
|
63
64
|
ON_COMMAND(ID_ACCELERATOR_X, &PayDlg::OnBnClickedPayButtonClose)
ON_COMMAND(ID_ACCELERATOR_ENTER, &PayDlg::OnBnClickedEnter)
|
liuye
authored
|
65
|
ON_MESSAGE(WM_CEF_SESSION_EXPIRED, &PayDlg::OnSessionExpired)
|
liuye
authored
|
66
67
68
69
70
71
72
73
74
75
76
77
|
END_MESSAGE_MAP()
// PayDlg 消息处理程序
BOOL PayDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetCtrlPos();
|
liuye
authored
|
78
|
real_money_static_.SetWindowText(total_money_);
|
liuye
authored
|
79
|
|
liuye
authored
|
80
81
82
83
84
|
if (card_type_ == 30)
{
password_static_.ShowWindow(SW_HIDE);
password_edit_.ShowWindow(SW_HIDE);
password_button_.ShowWindow(SW_HIDE);
|
liuye
authored
|
85
|
pay_button_.SetFocus();
|
liuye
authored
|
86
87
88
89
90
91
|
}
else
{
password_static_.ShowWindow(SW_SHOW);
password_edit_.ShowWindow(SW_SHOW);
password_button_.ShowWindow(SW_SHOW);
|
liuye
authored
|
92
|
password_button_.SetFocus();
|
liuye
authored
|
93
94
|
}
|
liuye
authored
|
95
|
acce_ = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR_PAY));
|
liuye
authored
|
96
|
|
liuye
authored
|
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void PayDlg::SetCtrlPos()
{
const int StaticWidth = 80, StaticHeigth = 35;
const int EditWidth = 140, EditHeigth = 35;
const int EditWidthShort = 70, StaticWidthShort = 70;
const int ButtonWidth = 120, ButtonHeight = 40;
CRect ctrl_rect;
ctrl_rect.top = 30 + 10;
ctrl_rect.bottom = ctrl_rect.top + StaticHeigth;
ctrl_rect.left = 30;
ctrl_rect.right = ctrl_rect.left + StaticWidth;
total_money_static_.MoveWindow(&ctrl_rect);
|
liuye
authored
|
115
116
|
CRect dlg_rect;
GetClientRect(&dlg_rect);
|
liuye
authored
|
117
|
ctrl_rect.left = ctrl_rect.right;
|
liuye
authored
|
118
|
ctrl_rect.right = dlg_rect.right - 2;
|
liuye
authored
|
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
real_money_static_.MoveWindow(&ctrl_rect);
ctrl_rect.top = ctrl_rect.bottom + 30;
ctrl_rect.bottom = ctrl_rect.top + StaticHeigth;
ctrl_rect.left = 30;
ctrl_rect.right = ctrl_rect.left + StaticWidth;
password_static_.MoveWindow(ctrl_rect);
ctrl_rect.left = ctrl_rect.right;
ctrl_rect.right += EditWidth;
password_edit_.MoveWindow(&ctrl_rect);
ctrl_rect.left = ctrl_rect.right + 10;
ctrl_rect.right += ButtonWidth;
password_button_.MoveWindow(&ctrl_rect);
|
liuye
authored
|
135
136
|
ctrl_rect.top = ctrl_rect.bottom + 10;
ctrl_rect.bottom = ctrl_rect.top + StaticHeigth;
|
liuye
authored
|
137
138
139
140
141
142
|
ctrl_rect.left = 30;
ctrl_rect.right = dlg_rect.right - 30;
error_msg_static_.MoveWindow(&ctrl_rect);
ctrl_rect.bottom = dlg_rect.bottom - 20;
|
liuye
authored
|
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
ctrl_rect.top = ctrl_rect.bottom - ButtonHeight;
ctrl_rect.right = dlg_rect.right - 100;
ctrl_rect.left = ctrl_rect.right - ButtonWidth;
close_button_.MoveWindow(&ctrl_rect);
ctrl_rect.right = ctrl_rect.left - 100;
ctrl_rect.left = ctrl_rect.right - ButtonWidth;
pay_button_.MoveWindow(ctrl_rect);
CRect rect;
password_edit_.GetClientRect(&rect);
OffsetRect(&rect, 0, 10);
password_edit_.SendMessage(EM_SETRECT, 0, (LPARAM)&rect);
password_edit_.EnableWindow(FALSE);
}
void PayDlg::OnBnClickedPayButtonClose()
{
CDialogEx::OnCancel();
}
|
liuye
authored
|
164
|
|
liuye
authored
|
165
166
167
|
#include "etradeclient/hardware/hardware_cmd.h"
#include "etradeclient/boost_patch/property_tree/json_parser.hpp"
#include "ETradeClient/utility/logging.h"
|
liuye
authored
|
168
169
170
|
void PayDlg::OnBnClickedPayButtonPay()
{
|
liuye
authored
|
171
|
LOG_TRACE(L"支付订单");
|
liuye
authored
|
172
173
|
CString error_msg;
|
liuye
authored
|
174
175
176
|
std::string statement_data;
do
{
|
liuye
authored
|
177
178
179
180
181
182
183
184
185
186
187
188
|
CString pass_word;
password_edit_.GetWindowText(pass_word);
if (card_type_ != 30)
{
if (pass_word.IsEmpty() || pass_word.GetLength() < 6)
{
SetErrorMsg(L"错误提示:密码为空或密码长度不足!");
break;
}
}
|
liuye
authored
|
189
190
191
192
|
const uint32_t kHTTPOK = 200;
WinHttp win_http;
auto& url_cfg = URLConfig::Instance();
win_http.ConnectHost(url_cfg.Host(), url_cfg.Port(), url_cfg.IsHttps());
|
liuye
authored
|
193
194
|
std::string url = url_cfg.GetFinishStatementPath();
auto& request = win_http.OpenRequest(WinHttp::Method::JSONPOST, url);
|
liuye
authored
|
195
196
197
198
199
200
201
|
std::ostringstream os;
os << statement_id_;
std::string statement_id_str;
std::istringstream is(os.str());
is >> statement_id_str;
|
liuye
authored
|
202
|
std::string post_data = "{\"statementId\":" + statement_id_str + ",\"password\":\"" + wstr_2_str(pass_word) + "\"}";
|
liuye
authored
|
203
204
205
206
207
208
209
210
211
212
213
214
215
|
request.SetPostData(post_data);
if (url_cfg.IsHttps())
{
auto& app_cfg = ApplicationConfig::Instance();
request.SetClientCertificate(app_cfg.ClientCertStore(), app_cfg.ClientCertSubject());
}
request.SetCookies(Session::Instance().Cookies());
request.Send();
uint32_t status_code = request.GetResponseStatus();
if (kHTTPOK != status_code)
{
std::string err_msg = "网络请求错误! 错误码: " + std::to_string(status_code);
|
liuye
authored
|
216
|
LOG_ERROR(CString(err_msg.c_str()).GetBuffer());
|
liuye
authored
|
217
218
219
|
error_msg = err_msg.c_str();
break;
}
|
liuye
authored
|
220
|
std::string response_header = request.ReadResponseHeader();
|
liuye
authored
|
221
222
223
224
225
226
227
228
229
230
231
232
|
std::string response_body = request.ReadResponseBody();
if (response_body.empty())
{
LOG_ERROR(L"获取服务器响应数据失败,请确保网络连接正常!");
error_msg = CString(L"获取服务器响应数据失败,请确保网络连接正常!");
break;
}
namespace PT = boost::property_tree;
bool card_res = false;
try //Parse the configuration file
{
|
liuye
authored
|
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
int status_index = response_header.find("sessionStatus");
if (std::string::npos == status_index)
{
LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!");
error_msg = CString(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!");
break;
}
std::string status = response_header.substr(status_index + 15, 1);
if (status.compare("0") == 0)
{
SendMessage(WM_CEF_SESSION_EXPIRED);
break;
}
|
liuye
authored
|
247
248
249
250
|
PT::ptree ptree;
std::stringstream ss;
ss << response_body;
PT::read_json(ss, ptree);
|
liuye
authored
|
251
|
if (ptree.get<long>("code") == 0)
|
liuye
authored
|
252
|
{
|
liuye
authored
|
253
254
255
256
257
|
PT::ptree data_child = ptree.get_child("data");
std::ostringstream os;
PT::write_json(os, data_child);
CString data = str_2_wstr(std::string(os.str())).c_str();
statement_data = CT2A(data);
|
liuye
authored
|
258
259
260
|
}
else
{
|
liuye
authored
|
261
|
error_msg = str_2_wstr(ptree.get<std::string>("message")).c_str();
|
liuye
authored
|
262
263
264
265
266
267
268
269
270
271
272
273
|
break;
}
}
catch (...)
{
LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!");
error_msg = CString(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!");
break;
}
} while (0);
|
liuye
authored
|
274
275
276
277
278
279
280
281
282
283
|
if (!error_msg.IsEmpty())
{
SetErrorMsg(error_msg);
}
else if (!statement_data.empty())
{
auto printer_device = StylusPrinterCmd();
HardwareCmd::Reply reply = printer_device.Execute(statement_data);
CDialogEx::OnCancel();
}
|
liuye
authored
|
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
}
void PayDlg::SetErrorMsg(CString error)
{
error_msg_static_.SetWindowTextW(error);
}
HBRUSH PayDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
if (nCtlColor == CTLCOLOR_STATIC)
{
if (IDC_STATIC_ERROR_MSG == pWnd->GetDlgCtrlID())
{
pDC->SetTextColor(RGB(200, 0, 0));
}
}
return hbr;
|
liuye
authored
|
305
|
}
|
liuye
authored
|
306
307
308
309
|
void PayDlg::OnBnClickedPayButtonPassword()
{
|
liuye
authored
|
310
|
password_edit_.SetWindowTextW(L"");
|
liuye
authored
|
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
344
345
346
347
348
349
|
auto pin_pad = ReadPINPadCmd();
HardwareCmd::Reply reply = pin_pad.Execute("");
CString error_msg;
do
{
if (reply.error_code.compare("") != 0)
{
if (reply.error_code.compare("100") == 0)
{
error_msg = L"连接密码键盘失败";
}
else if (reply.error_code.compare("101") == 0)
{
error_msg = L"用户密码输入超时";
}
else if (reply.error_code.compare("102") == 0)
{
error_msg = L"用户取消密码输入";
}
else if (reply.error_code.compare("103") == 0)
{
error_msg = L"读取密码失败";
}
else
{
error_msg = L"读卡失败";
}
std::wstring log = L"支付时输入密码错误,错误码:" + str_2_wstr(reply.error_code);
LOG_ERROR(log);
break;
}
boost::property_tree::ptree password_child = reply.data.get_child("password");
std::string password = password_child.data();
password_edit_.SetWindowText(CString(password.c_str()));
} while (0);
if (!error_msg.IsEmpty())
{
SetErrorMsg(error_msg);
|
liuye
authored
|
350
351
352
353
354
|
password_edit_.SetWindowTextW(L"");
}
else
{
SetErrorMsg(CString(L""));
|
liuye
authored
|
355
356
|
}
}
|
liuye
authored
|
357
358
359
360
361
362
363
364
365
366
367
368
|
BOOL PayDlg::PreTranslateMessage(MSG* pMsg)
{
if (WM_KEYFIRST <= pMsg->message&&pMsg->message <= WM_KEYLAST)
{
if (acce_ && ::TranslateAccelerator(m_hWnd, acce_, pMsg))
return TRUE;
}
return CDialogEx::PreTranslateMessage(pMsg);
}
|
liuye
authored
|
369
370
371
372
373
374
375
376
377
378
379
|
LRESULT PayDlg::OnSessionExpired(WPARAM wParam, LPARAM lParam)
{
if (!CLoginDialog(L"当前连接已过期,请重新登录!", this).Launch())
{
AfxGetMainWnd()->PostMessage(WM_CLOSE);
return 0;
}
Session::Instance().OnValid(); // If login succeeded, recover the session state to be "not expired".
return 0;
}
|
liuye
authored
|
380
|
|
liuye
authored
|
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
void PayDlg::OnBnClickedEnter()
{
int item_id = GetFocus()->GetDlgCtrlID();
switch (item_id)
{
case IDC_PAY_BUTTON_PASSWORD:
{
OnBnClickedPayButtonPassword();
CString error;
error_msg_static_.GetWindowText(error);
if (error.IsEmpty())
{
pay_button_.SetFocus();
}
else
{
password_button_.SetFocus();
}
break;
}
case IDC_PAY_BUTTON_PAY:
{
OnBnClickedPayButtonPay();
break;
}
case IDC_PAY_BUTTON_CLOSE:
{
OnBnClickedPayButtonClose();
break;
}
}
}
|