pay_dlg.cpp
10.1 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// 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"
#include "etradeclient/hardware/hardware_cmd.h"
#include "etradeclient/boost_patch/property_tree/json_parser.hpp"
#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>
#include "LoginDialog.h"
// PayDlg 对话框
IMPLEMENT_DYNAMIC(PayDlg, CDialogEx)
PayDlg::PayDlg(long statement_id, CString total_money, int card_type, CWnd* pParent /*=NULL*/)
: CDialogEx(PayDlg::IDD, pParent)
{
statement_id_ = statement_id;
total_money_ = total_money;
card_type_ = card_type;
}
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_);
DDX_Control(pDX, IDC_STATIC_ERROR_MSG, error_msg_static_);
}
BEGIN_MESSAGE_MAP(PayDlg, CDialogEx)
ON_BN_CLICKED(IDC_PAY_BUTTON_CLOSE, &PayDlg::OnBnClickedPayButtonClose)
ON_BN_CLICKED(IDC_PAY_BUTTON_PAY, &PayDlg::OnBnClickedPayButtonPay)
ON_WM_CTLCOLOR()
ON_BN_CLICKED(IDC_PAY_BUTTON_PASSWORD, &PayDlg::OnBnClickedPayButtonPassword)
ON_COMMAND(ID_ACCELERATOR_X, &PayDlg::OnBnClickedPayButtonPassword)
ON_COMMAND(ID_ACCELERATOR_F, &PayDlg::OnBnClickedPayButtonPay)
ON_COMMAND(ID_ACCELERATOR_S, &PayDlg::OnBnClickedPayButtonClose)
ON_MESSAGE(WM_CEF_SESSION_EXPIRED, &PayDlg::OnSessionExpired)
END_MESSAGE_MAP()
// PayDlg 消息处理程序
BOOL PayDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
SetCtrlPos();
real_money_static_.SetWindowText(total_money_);
if (card_type_ == 30)
{
password_static_.ShowWindow(SW_HIDE);
password_edit_.ShowWindow(SW_HIDE);
password_button_.ShowWindow(SW_HIDE);
}
else
{
password_static_.ShowWindow(SW_SHOW);
password_edit_.ShowWindow(SW_SHOW);
password_button_.ShowWindow(SW_SHOW);
}
acce_ = LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR_ORDER));
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);
ctrl_rect.left = ctrl_rect.right;
ctrl_rect.right += StaticWidth;
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);
CRect dlg_rect;
GetClientRect(&dlg_rect);
ctrl_rect.top = ctrl_rect.bottom + 10;
ctrl_rect.bottom = ctrl_rect.top + StaticHeigth;
ctrl_rect.left = 30;
ctrl_rect.right = dlg_rect.right - 30;
error_msg_static_.MoveWindow(&ctrl_rect);
ctrl_rect.bottom = dlg_rect.bottom - 20;
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();
}
#include "etradeclient/hardware/hardware_cmd.h"
#include "etradeclient/boost_patch/property_tree/json_parser.hpp"
#include "ETradeClient/utility/logging.h"
void PayDlg::OnBnClickedPayButtonPay()
{
//for test
//OnSessionExpired(NULL, NULL);
//
LOG_TRACE(L"支付订单");
CString error_msg;
std::string statement_data;
do
{
CString pass_word;
password_edit_.GetWindowText(pass_word);
if (card_type_ != 30)
{
if (pass_word.IsEmpty() || pass_word.GetLength() < 6)
{
SetErrorMsg(L"错误提示:密码为空或密码长度不足!");
break;
}
}
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());
std::string url = url_cfg.GetFinishStatementPath();
auto& request = win_http.OpenRequest(WinHttp::Method::JSONPOST, url);
std::ostringstream os;
os << statement_id_;
std::string statement_id_str;
std::istringstream is(os.str());
is >> statement_id_str;
std::string post_data = "{\"statementId\":" + statement_id_str + ",\"password\":\"" + wstr_2_str(pass_word) + "\"}";
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);
LOG_ERROR(CString(err_msg.c_str()).GetBuffer());
error_msg = err_msg.c_str();
break;
}
std::string response_header = request.ReadResponseHeader();
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
{
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;
}
PT::ptree ptree;
std::stringstream ss;
ss << response_body;
PT::read_json(ss, ptree);
if (ptree.get<long>("code") == 0)
{
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);
}
else
{
error_msg = str_2_wstr(ptree.get<std::string>("message")).c_str();
break;
}
}
catch (...)
{
LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!");
error_msg = CString(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!");
break;
}
} while (0);
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();
}
}
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;
}
void PayDlg::OnBnClickedPayButtonPassword()
{
password_edit_.SetWindowTextW(L"");
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);
password_edit_.SetWindowTextW(L"");
}
else
{
SetErrorMsg(CString(L""));
}
}
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);
}
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;
}