pay_dlg.cpp
6.71 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
// 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>
// 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()
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);
}
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.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();
}
void PayDlg::OnBnClickedPayButtonPay()
{
std::string statement_data;
do
{
CString error_msg;
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.GetSubmitOrdersPath();
auto& request = win_http.OpenRequest(WinHttp::Method::POST, url);
CString pass_word;
password_edit_.GetWindowText(pass_word);
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(str_2_wstr(err_msg.c_str()));
error_msg = err_msg.c_str();
break;
}
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
{
PT::ptree ptree;
std::stringstream ss;
ss << response_body;
PT::read_json(ss, ptree);
if (ptree.get<int>("code") == 0)
{
statement_data = ptree.get<std::string>("data");
}
else
{
error_msg = ptree.get<std::string>("message").c_str();
break;
}
}
catch (...)
{
LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!");
error_msg = CString(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!");
break;
}
} while (0);
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;
}