AppBookingAction.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. package com.happy.action;
  2. import com.happy.Model.AdminManager;
  3. import com.happy.Model.Booking;
  4. import com.happy.Model.weixin.PayResult;
  5. import com.happy.Model.weixin.Users;
  6. import com.happy.Model.weixin.WechatUnifiedOrder;
  7. import com.happy.Model.weixin.WeiXinUtil;
  8. import com.happy.Until.*;
  9. import com.happy.Until.Enum.B;
  10. import com.happy.Until.Enum.OrderEnum;
  11. import com.happy.Until.Enum.PayEnum;
  12. import com.happy.Until.weixin.EndPay;
  13. import com.happy.Until.weixin.JaxbUtil;
  14. import com.happy.Until.weixin.PayWxUtil;
  15. import com.happy.Until.weixin.TongYiReturn;
  16. import com.happy.common.http.Message;
  17. import com.happy.common.http.Message2;
  18. import com.happy.common.wx.WxUtil;
  19. import com.happy.constant.ResultStatusCode;
  20. import com.happy.service.AdminManagerService;
  21. import com.happy.service.AppBookService;
  22. import com.happy.service.BookService;
  23. import com.happy.service.UserService;
  24. import com.opensymphony.xwork2.ActionSupport;
  25. import net.sf.json.JSONObject;
  26. import org.apache.struts2.ServletActionContext;
  27. import org.apache.struts2.interceptor.ServletRequestAware;
  28. import javax.annotation.Resource;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import java.io.BufferedReader;
  32. import java.io.PrintWriter;
  33. import java.text.ParseException;
  34. import java.util.*;
  35. /**
  36. * 订单进去Action请求交互
  37. */
  38. public class AppBookingAction extends ActionSupport implements ServletRequestAware {
  39. private HttpServletRequest request;
  40. public HttpServletResponse response;
  41. @Resource
  42. public BookService bookService;
  43. @Resource
  44. public UserService userService;
  45. @Resource
  46. public AppBookService appBookService;
  47. @Resource
  48. public AdminManagerService adminManagerService;
  49. public int page; // 当前页
  50. public int rows;// 每页显示的行数rows
  51. public Integer id;
  52. public String status;
  53. public String hotelId; // 酒店id
  54. public String houseId; // 房型id
  55. public String startTime; // 订房开始时间
  56. public String endTime; // 订房结束时间
  57. public String houseOrderNumber; // 订房数量
  58. public String userName; // 住客姓名
  59. public String userPhone; // 住客联系电话
  60. public String userId; // 下单人id
  61. public String getBookingId() {
  62. return bookingId;
  63. }
  64. public void setBookingId(String bookingId) {
  65. this.bookingId = bookingId;
  66. }
  67. private String bookingId; // 订单Id
  68. private String open_id; // 用户open_Id
  69. public HttpServletRequest getRequest() {
  70. return request;
  71. }
  72. public void setRequest(HttpServletRequest request) {
  73. this.request = request;
  74. }
  75. public void setServletRequest(HttpServletRequest request) {
  76. this.request = request;
  77. }
  78. public HttpServletResponse getResponse() {
  79. return response;
  80. }
  81. public void setResponse(HttpServletResponse response) {
  82. this.response = response;
  83. }
  84. public int getPage() {
  85. return page;
  86. }
  87. public void setPage(int page) {
  88. this.page = page;
  89. }
  90. public int getRows() {
  91. return rows;
  92. }
  93. public void setRows(int rows) {
  94. this.rows = rows;
  95. }
  96. public Integer getId() {
  97. return id;
  98. }
  99. public void setId(Integer id) {
  100. this.id = id;
  101. }
  102. public String getStatus() {
  103. return status;
  104. }
  105. public void setStatus(String status) {
  106. this.status = status;
  107. }
  108. /**
  109. * 定时任务
  110. * @return
  111. */
  112. public String scheduledTasks()
  113. {
  114. // 获取所有的订单待支付状态
  115. StringBuffer strSqlBook = new StringBuffer();
  116. strSqlBook.append(" and order_status = '").append(PayEnum.ONE.getNum()).append("' ");
  117. List<Booking> bookingList = bookService.queryList(strSqlBook.toString());
  118. if (bookingList == null || bookingList.size() <= 0){
  119. return null;
  120. }
  121. try {
  122. for (Booking booking: bookingList) {
  123. if (booking == null || Func.checkNull(booking.getCreateTime()) || Func.checkNull(booking.getLockTime()))
  124. continue;
  125. Date createTime = DateUtil.parseDate(booking.getCreateTime(),DateUtil.Time_Formatter_Second);
  126. // 对订单时间进行累加
  127. String lockTime = TimeExchange.TimeRangeM(DateUtil.getFormatPaternDate(createTime),Func.parseInt(booking.getLockTime()));
  128. // 当前时间 大于 锁定时间 为true
  129. boolean flag = TimeExchange.CompareDate(lockTime, TimeExchange.getDate());
  130. if (flag)
  131. {
  132. // 如果超时,则将订单状态修改为已超时
  133. booking.setOrderStatus(Func.parseStr(PayEnum.SIX.getNum()));
  134. booking.setUpdateTime(TimeExchange.getDate());
  135. bookService.updateBooking(booking);
  136. System.out.println("已将订单"+booking.getOrderNum()+"标识为支付超时");
  137. }
  138. }
  139. } catch (ParseException e) {
  140. e.printStackTrace();
  141. }
  142. return null;
  143. }
  144. /**
  145. * houseId; // 房型id
  146. * startTime; // 订房开始时间 2023/08/04
  147. * endTime; // 订房结束时间 2023/08/05
  148. * houseOrderNumber; // 订房数量
  149. * userName; // 住客姓名
  150. * userPhone; // 住客联系电话
  151. * userId; // 下单人id
  152. * 用户点击订房操作,生成订单
  153. */
  154. public String createOrder() {
  155. // 插入订单数据到表中
  156. JSONObject jsonObject = new JSONObject();
  157. if (Func.checkNull(houseId) || Func.checkNull(startTime) || Func.checkNull(endTime)
  158. || Func.checkNull(houseOrderNumber) || Func.checkNull(userName) || Func.checkNull(userPhone) || Func.checkNull(userId)) {
  159. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  160. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  161. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  162. return null;
  163. }
  164. // 将时间统一转化为 yyyy/MM/dd
  165. String startDate = DateUtil.parseDateToStr((Func.parseDate(startTime)), DateUtil.Time_Formatter_Day);
  166. String endDate = DateUtil.parseDateToStr((Func.parseDate(endTime)), DateUtil.Time_Formatter_Day);
  167. // 创建订单之前,先验证是否有房间
  168. String backMessage = appBookService.varificationHouse(houseId, startDate, endDate);
  169. if (!Func.checkNull(backMessage)) {
  170. jsonObject.put(B.code, ResultStatusCode.CANNTBOOKING.getStatus());
  171. jsonObject.put(B.message, backMessage);
  172. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  173. return null;
  174. }
  175. String bookingId= Func.parseStr(UUIDUtil.generateID());
  176. String mess = appBookService.createOrder(bookingId, houseId, startDate, endDate, houseOrderNumber, userName, userPhone, userId);
  177. if (!Func.checkNull(mess)) {
  178. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  179. jsonObject.put(B.message, mess);
  180. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  181. return null;
  182. }
  183. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  184. jsonObject.put(B.message, ResultStatusCode.OK.getMsg());
  185. jsonObject.put(B.data, bookingId);
  186. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  187. return null;
  188. }
  189. /**
  190. * 再次预订
  191. *
  192. * @return
  193. */
  194. public String reCreateOrder() {
  195. // 插入订单数据到表中
  196. JSONObject jsonObject = new JSONObject();
  197. if (Func.checkNull(houseId) || Func.checkNull(startTime) || Func.checkNull(endTime)
  198. || Func.checkNull(houseOrderNumber) || Func.checkNull(userName) || Func.checkNull(userPhone) || Func.checkNull(userId)) {
  199. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  200. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  201. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  202. return null;
  203. }
  204. // 将时间统一转化为 yyyy/MM/dd
  205. String startDate = DateUtil.parseDateToStr((Func.parseDate(startTime)), DateUtil.Time_Formatter_Day);
  206. String endDate = DateUtil.parseDateToStr((Func.parseDate(endTime)), DateUtil.Time_Formatter_Day);
  207. // 创建订单之前,先验证是否有房间
  208. String backMessage = appBookService.varificationHouse(houseId, startDate, endDate);
  209. if (Func.checkNull(backMessage)) {
  210. jsonObject.put(B.code, ResultStatusCode.CANNTBOOKING.getStatus());
  211. jsonObject.put(B.message, backMessage);
  212. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  213. return null;
  214. }
  215. appBookService.reCreateOrder(houseId, startTime, endTime, houseOrderNumber, userName, userPhone, userId);
  216. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  217. jsonObject.put(B.message, ResultStatusCode.OK.getMsg());
  218. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  219. return null;
  220. }
  221. /**
  222. * bookingId 订单id
  223. * open_id
  224. * 取消订单功能:
  225. * //1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款
  226. *
  227. * @return
  228. */
  229. public String cancelBooking() throws Exception {
  230. if (Func.checkNull(getBookingId()))
  231. return null;
  232. JSONObject jsonObject = new JSONObject();
  233. // 取消订单,修改订单的支付状态
  234. String mess = bookService.cancelBooking(getBookingId());
  235. if (!Func.checkNull(mess)) {
  236. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  237. jsonObject.put(B.message, mess);
  238. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  239. return null;
  240. }
  241. Booking booking = bookService.getById(Func.parseInt(this.bookingId));
  242. if (booking == null || Func.checkNull(booking.getRefundWay()))
  243. return null;
  244. // 进行判断如果订单是已取消,并且标识字段微信退款,则进入退款流程
  245. if (Func.parseStr(PayEnum.SEVEN.getNum()).equals(booking.getOrderStatus()) && OrderEnum.微信退款.toString().equals(booking.getRefundWay())) {
  246. if (Func.checkNull(this.open_id) || Func.checkNull(this.bookingId)) {
  247. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  248. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  249. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  250. return null;
  251. }
  252. Users users = userService.queryByOpenid(open_id); // 用户信息
  253. if (users == null || booking == null) {
  254. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  255. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  256. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  257. return null;
  258. }
  259. String ip = WeiXinUtil.ip;
  260. double money = booking.getPayAccount();
  261. int a = (int) (money * 100);
  262. WechatUnifiedOrder w = new WechatUnifiedOrder();
  263. w.setAppid(WeiXinUtil.appid_c);
  264. w.setAttach("chuanghai");
  265. w.setBody("chuanghai");
  266. w.setMch_id(WeiXinUtil.account);
  267. w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
  268. w.setNotify_url(WeiXinUtil.ip_h + "/appquery_refund.action");// 支付结果回调地址
  269. w.setOpenid(open_id);
  270. w.setOut_trade_no(booking.getOrderNum());
  271. String refund_order = WxUtil.mchOrderNo();
  272. w.setOut_refund_no(refund_order); // 退款单号
  273. w.setSpbill_create_ip(ip);
  274. w.setTotal_fee(a);
  275. w.setRefund_fee(a); // 退款金额
  276. w.setTrade_type("JSAPI");
  277. SortedMap<String, String> params = new TreeMap<>();
  278. params.put("appid", w.getAppid());
  279. params.put("mch_id", w.getMch_id());
  280. params.put("nonce_str", w.getNonce_str());
  281. params.put("notify_url", w.getNotify_url());
  282. params.put("out_refund_no", w.getOut_refund_no()); // 退款单号
  283. params.put("out_trade_no", w.getOut_trade_no());
  284. params.put("refund_fee",w.getRefund_fee()+""); // 退款金额
  285. params.put("total_fee", w.getTotal_fee() + "");
  286. w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
  287. params.put("sign", w.getSign());
  288. //String retXml = JaxbUtil.getRequestXml(params);
  289. String xml = "<xml>" +
  290. "<appid>" + w.getAppid() + "</appid>" +
  291. "<mch_id>" + w.getMch_id() + "</mch_id>" +
  292. "<nonce_str>" + w.getNonce_str() + "</nonce_str>" +
  293. "<notify_url>" + w.getNotify_url() + "</notify_url>" +
  294. "<out_refund_no>" + w.getOut_refund_no() + "</out_refund_no>" +
  295. "<out_trade_no>" + w.getOut_trade_no() + "</out_trade_no>" +
  296. "<refund_fee>" + w.getRefund_fee()+"" + "</refund_fee>" +
  297. "<total_fee>" + w.getTotal_fee() + "" + "</total_fee>" +
  298. "<sign>" + w.getSign() + "</sign>" +
  299. "</xml>";
  300. //String msg = HttpsClient.sendPost2("https://api.mch.weixin.qq.com/secapi/pay/refund", retXml);
  301. String msg = HttpUtils.post2("https://api.mch.weixin.qq.com/secapi/pay/refund", xml);
  302. if (msg.indexOf("FAIL") > -1) {
  303. jsonObject.put("code", 507);
  304. jsonObject.put("message", "退款失败");
  305. ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  306. return null;
  307. }
  308. // 设置订单状态为退款中
  309. booking.setOrderStatus(Func.parseStr(PayEnum.TEN.getNum()));
  310. booking.setUpdateTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
  311. bookService.updateBooking(booking); // 退款中
  312. jsonObject.put("code", 200);
  313. jsonObject.put("message", "退款申请已提交,请稍候查询");
  314. ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  315. return null;
  316. }
  317. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  318. jsonObject.put(B.message, ResultStatusCode.OK.getMsg());
  319. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  320. return null;
  321. }
  322. /**
  323. * open_id
  324. * bookingId
  325. * 微信退款功能
  326. */
  327. public String refundBooking() throws Exception {
  328. JSONObject resultJson = new JSONObject();
  329. if (Func.checkNull(this.open_id) || Func.checkNull(this.bookingId)) {
  330. resultJson.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  331. resultJson.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  332. ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  333. return null;
  334. }
  335. Users users = userService.queryByOpenid(open_id); // 用户信息
  336. Booking booking = bookService.getById(Func.parseInt(bookingId)); // 订单信息
  337. if (users == null || booking == null) {
  338. resultJson.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  339. resultJson.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  340. ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  341. return null;
  342. }
  343. String ip = WeiXinUtil.ip;
  344. double money = booking.getPayAccount();
  345. int a = (int) (money * 100);
  346. WechatUnifiedOrder w = new WechatUnifiedOrder();
  347. w.setAppid(WeiXinUtil.appid_c);
  348. w.setAttach("chuanghai");
  349. w.setBody("chuanghai");
  350. w.setMch_id(WeiXinUtil.account);
  351. w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
  352. w.setNotify_url(WeiXinUtil.ip_h + "/appquery_refund.action");// 支付结果回调地址
  353. w.setOpenid(open_id);
  354. w.setOut_trade_no(booking.getOrderNum());
  355. String refund_order = WxUtil.mchOrderNo();
  356. w.setOut_refund_no(refund_order); // 退款单号
  357. w.setSpbill_create_ip(ip);
  358. w.setTotal_fee(a);
  359. w.setRefund_fee(a); // 退款金额
  360. w.setTrade_type("JSAPI");
  361. SortedMap<String, String> params = new TreeMap<>();
  362. params.put("appid", w.getAppid());
  363. params.put("mch_id", w.getMch_id());
  364. params.put("nonce_str", w.getNonce_str());
  365. params.put("notify_url", w.getNotify_url());
  366. params.put("out_refund_no", w.getOut_refund_no()); // 退款单号
  367. params.put("out_trade_no", w.getOut_trade_no());
  368. params.put("refund_fee",w.getRefund_fee()+""); // 退款金额
  369. params.put("total_fee", w.getTotal_fee() + "");
  370. w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
  371. params.put("sign", w.getSign());
  372. //String retXml = JaxbUtil.getRequestXml(params);
  373. String xml = "<xml>" +
  374. "<appid>" + w.getAppid() + "</appid>" +
  375. "<mch_id>" + w.getMch_id() + "</mch_id>" +
  376. "<nonce_str>" + w.getNonce_str() + "</nonce_str>" +
  377. "<notify_url>" + w.getNotify_url() + "</notify_url>" +
  378. "<out_refund_no>" + w.getOut_refund_no() + "</out_refund_no>" +
  379. "<out_trade_no>" + w.getOut_trade_no() + "</out_trade_no>" +
  380. "<refund_fee>" + w.getRefund_fee()+"" + "</refund_fee>" +
  381. "<total_fee>" + w.getTotal_fee() + "" + "</total_fee>" +
  382. "<sign>" + w.getSign() + "</sign>" +
  383. "</xml>";
  384. //String msg = HttpsClient.sendPost2("https://api.mch.weixin.qq.com/secapi/pay/refund", retXml);
  385. String msg = HttpUtils.post2("https://api.mch.weixin.qq.com/secapi/pay/refund", xml);
  386. if (msg.indexOf("FAIL") > -1) {
  387. resultJson.put("code", 507);
  388. resultJson.put("message", "退款失败");
  389. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  390. return null;
  391. }
  392. // 设置订单状态为退款中
  393. booking.setOrderStatus(Func.parseStr(PayEnum.TEN.getNum()));
  394. booking.setUpdateTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
  395. bookService.updateBooking(booking); // 退款中
  396. resultJson.put("code", 200);
  397. resultJson.put("message", "退款申请已提交,请稍候查询");
  398. ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  399. return null;
  400. }
  401. /* 退款回调 */
  402. public String query_refund() throws Exception {
  403. // 查询本地数据库中,正在退款中
  404. StringBuffer strSqlBook = new StringBuffer();
  405. strSqlBook.append(" and order_status = '").append(PayEnum.TEN.getNum()).append("' ");
  406. List<Booking> booking = bookService.queryList(strSqlBook.toString());
  407. if (booking==null){
  408. return null;
  409. }
  410. WechatUnifiedOrder w = new WechatUnifiedOrder();
  411. Map<String, String> params = new HashMap<String, String>();
  412. Booking book = new Booking();
  413. for (int i = 0; i < booking.size(); i++) {
  414. if (booking.get(i) == null)
  415. continue;
  416. book = booking.get(i);
  417. w = new WechatUnifiedOrder();
  418. w.setAppid(WeiXinUtil.appid_c);
  419. w.setAttach("chuanghai");
  420. w.setBody("chuanghai");
  421. w.setMch_id(WeiXinUtil.account);
  422. w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
  423. w.setOut_trade_no(book.getOrderNum());
  424. w.setTrade_type("JSAPI");
  425. params = new HashMap<String, String>();
  426. params.put("appid", w.getAppid());
  427. params.put("mch_id", w.getMch_id());
  428. params.put("out_trade_no", w.getOut_trade_no());
  429. params.put("nonce_str", w.getNonce_str());
  430. w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
  431. params.put("sign", w.getSign());
  432. String retXml = JaxbUtil.getRequestXml(params);
  433. String msg = HttpUtils.post("https://api.mch.weixin.qq.com/pay/refundquery", retXml);
  434. if (msg.indexOf("<refund_status_0><![CDATA[SUCCESS]]></refund_status_0>")>0) {
  435. // 退款成功,将数据写入本地数据库
  436. book.setRefundTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
  437. book.setRefundAmount(book.getHouseTotalPrice());
  438. book.setUpdateTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
  439. bookService.updateBooking(book);
  440. // 发送成功消息给商家和用户
  441. AdminManager adminManager = adminManagerService.getById(Func.parseInt(book.getHotelManagerId()));
  442. Users users = userService.queryByUserId(book.getCreateUserid());
  443. String sendMsg = "订单:"+ book.getOrderNum() +"已退款成功,请注意查看";
  444. // 推给商家
  445. Message.send(adminManager.getOpenid(),book.getOrderNum(),book.getUserName(),sendMsg,book.getCreateTime());
  446. // 推给用户
  447. Message.send(users.getOpenid(),book.getOrderNum(),book.getUserName(),sendMsg,book.getCreateTime());
  448. }
  449. }
  450. String returnMsg = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
  451. HttpServletResponse response = ServletActionContext.getResponse();
  452. response.setContentType("text/html;charset=utf-8");
  453. PrintWriter out = response.getWriter();
  454. out.println(returnMsg);
  455. out.flush();
  456. out.close();
  457. return null;
  458. }
  459. /**
  460. * bookingId 订单id
  461. * 删除订单功能(假删除)
  462. *
  463. * @return
  464. */
  465. public String delBooking() {
  466. if (Func.checkNull(getBookingId()))
  467. return null;
  468. JSONObject jsonObject = new JSONObject();
  469. String mess = bookService.delBooking(getBookingId());
  470. if (!Func.checkNull(mess)) {
  471. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  472. jsonObject.put(B.message, mess);
  473. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  474. return null;
  475. }
  476. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  477. jsonObject.put(B.message, ResultStatusCode.OK.getMsg());
  478. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  479. return null;
  480. }
  481. /**
  482. * 验证是否有房间
  483. * houseId, 房型id
  484. * startTime, 开始时间
  485. * endTime 结束时间
  486. *
  487. * @return
  488. */
  489. public String varificationHouse() {
  490. JSONObject jsonObject = new JSONObject();
  491. if (Func.checkNull(houseId) || Func.checkNull(startTime) || Func.checkNull(endTime)) {
  492. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  493. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  494. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  495. return null;
  496. }
  497. String backMessage = appBookService.varificationHouse(houseId, startTime, endTime);
  498. if (!Func.checkNull(backMessage)) {
  499. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  500. jsonObject.put(B.message, backMessage);
  501. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  502. return null;
  503. }
  504. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  505. jsonObject.put(B.message, OrderEnum.有空房.toString());
  506. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  507. return null;
  508. }
  509. /**
  510. * open_id
  511. * bookingId
  512. * 用户支付操作
  513. *
  514. * @return
  515. */
  516. public String pay() {
  517. JSONObject resultjson = new JSONObject();
  518. if (Func.checkNull(this.open_id) || Func.checkNull(this.bookingId)) {
  519. resultjson.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  520. resultjson.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  521. ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  522. return null;
  523. }
  524. Users users = userService.queryByOpenid(open_id); // 用户信息
  525. Booking booking = bookService.getById(Func.parseInt(bookingId)); // 订单信息
  526. if (users == null || booking == null) {
  527. resultjson.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  528. resultjson.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  529. ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  530. return null;
  531. }
  532. try {
  533. // 支付费用
  534. Double totalPrice = booking.getHouseTotalPrice() * 100;
  535. int totalfee = Func.parseInt(totalPrice);
  536. WechatUnifiedOrder w = new WechatUnifiedOrder();
  537. w.setAppid(WeiXinUtil.appid_c);
  538. w.setAttach("chuanghai");
  539. w.setBody("chuanghai");
  540. w.setMch_id(WeiXinUtil.account);
  541. w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
  542. w.setNotify_url(WeiXinUtil.ip_h + "/abkpayResulet.action");// 支付结果回调地址
  543. w.setOpenid(this.open_id);
  544. String orderNo = booking.getOrderNum(); // 订单编号
  545. w.setOut_trade_no(orderNo);
  546. w.setSpbill_create_ip(WeiXinUtil.ip);
  547. w.setTotal_fee(totalfee);
  548. w.setTrade_type("JSAPI");
  549. Map<String, String> params = new HashMap<String, String>();
  550. params.put("attach", w.getAttach());
  551. params.put("appid", w.getAppid());
  552. params.put("mch_id", w.getMch_id());
  553. params.put("nonce_str", w.getNonce_str());
  554. params.put("body", w.getBody());
  555. params.put("out_trade_no", w.getOut_trade_no());
  556. params.put("total_fee", Func.parseStr(w.getTotal_fee()));
  557. params.put("spbill_create_ip", w.getSpbill_create_ip());
  558. params.put("notify_url", w.getNotify_url());
  559. params.put("trade_type", w.getTrade_type());
  560. params.put("openid", w.getOpenid());
  561. w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
  562. params.put("sign", w.getSign());
  563. String retXml = JaxbUtil.getRequestXml(params);
  564. String msg = HttpUtils.post("https://api.mch.weixin.qq.com/pay/unifiedorder", retXml);
  565. System.out.println(msg);
  566. // 支付失败
  567. if (msg.indexOf("FAIL") > -1) {
  568. resultjson.put("code", "205");
  569. resultjson.put("message", "支付异常205");
  570. ResUtil.write(resultjson, ServletActionContext.getResponse());
  571. return null;
  572. }
  573. JaxbUtil requestBinder = new JaxbUtil(TongYiReturn.class, JaxbUtil.CollectionWrapper.class);
  574. TongYiReturn to = requestBinder.fromXml(msg);
  575. // 支付成功
  576. if (to.getReturn_code().equals("SUCCESS") && to.getResult_code().equals("SUCCESS")) {
  577. EndPay pay = new EndPay();
  578. pay.setAppId(WeiXinUtil.appid_c);
  579. pay.setSignType("MD5");
  580. pay.setTimeStamp(System.currentTimeMillis() / 1000 + "");
  581. pay.setPrepay_id(to.getPrepay_id());
  582. pay.setNonceStr(PayWxUtil.getNonceStr());
  583. Map<String, String> requestMap = new HashMap<String, String>();
  584. requestMap.put("appId", pay.getAppId());
  585. requestMap.put("timeStamp", pay.getTimeStamp());
  586. requestMap.put("nonceStr", pay.getNonceStr());
  587. requestMap.put("package", "prepay_id=" + pay.getPrepay_id());
  588. requestMap.put("signType", "MD5");
  589. pay.setPaySign(PayWxUtil.getSign(requestMap, WeiXinUtil.key));
  590. // 返回支付参数
  591. resultjson.put("code", 200);
  592. resultjson.put("message", "返回成功");
  593. resultjson.put("data", pay);
  594. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  595. // 保存支付信息
  596. booking.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
  597. booking.setOrderStatus(Func.parseStr(PayEnum.ONE.getNum()));
  598. booking.setPayTime(DateUtil.getFormatPaternDate(new Date()));
  599. booking.setPayAccount(totalPrice);
  600. booking.setPayWay(OrderEnum.微信支付.toString());
  601. bookService.updateBooking(booking);
  602. resultjson.put(B.code, ResultStatusCode.OK.getStatus());
  603. resultjson.put(B.message, "支付成功");
  604. ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  605. }
  606. } catch (Exception e) {
  607. resultjson.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  608. resultjson.put(B.message, e.getMessage());
  609. ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  610. }
  611. return null;
  612. }
  613. /**
  614. * 支付成功后回调,推送消息给商家
  615. *
  616. */
  617. public String payResulet(){
  618. System.out.println("进入支付回调====");
  619. JSONObject resultjson = new JSONObject();
  620. try{
  621. BufferedReader reader = null;// BufferedReader 字符输入流
  622. reader = request.getReader();
  623. String line = "";
  624. String xmlString = null;
  625. StringBuilder inputString = new StringBuilder();
  626. while ((line = reader.readLine()) != null) {
  627. inputString.append(line);
  628. }
  629. xmlString = inputString.toString();
  630. request.getReader().close();
  631. JaxbUtil requestBinder = new JaxbUtil(PayResult.class, PayResult.class);
  632. PayResult result = requestBinder.fromXml(xmlString);// Xml到Java,用fromXML()方法
  633. String order_num = result.getOut_trade_no();
  634. System.out.println("支付成功回调,订单号:"+order_num);
  635. Booking booking = bookService.queryByOrder(order_num);
  636. // 支付成功
  637. if (result.getResult_code().equals("SUCCESS") && booking!=null && booking.getOrderStatus().equals("1")) {
  638. // 保存支付信息
  639. booking.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
  640. booking.setOrderStatus(Func.parseStr(PayEnum.TWO.getNum()));
  641. booking.setPayTime(DateUtil.getFormatPaternDate(new Date()));
  642. booking.setPayWay(OrderEnum.微信支付.toString());
  643. // 进行业务判断,如果是商家设定了自动接单,则支付状态直接变为待入住 接单设置(1自动接单 2手动接单)
  644. if ("1".equals(booking.getHotelIsCanorder()))
  645. {
  646. // 自动接单,订单状态修改为待入住
  647. booking.setOrderStatus(Func.parseStr(PayEnum.THREE.getNum()));
  648. // String userId = booking.getCreateUserid();
  649. // Users users = userService.queryByUserId(userId);
  650. // String mess = "您已成功订房,订单号:"+order_num;
  651. // Message.send(users.getOpenid(),order_num, users.getUser_name(),mess, booking.getPayTime());
  652. }
  653. // 手动接单,发消息给商家
  654. AdminManager adminManager = adminManagerService.getById(Func.parseInt(booking.getHotelManagerId()));
  655. String live_end = booking.getOrderStartTime() +"~"+ booking.getOrderEndTime();
  656. String hotelName = "";
  657. if (Func.checkNull(booking.getHotelName()))
  658. {
  659. hotelName = booking.getHouseName();
  660. }
  661. Message2.send(adminManager.getOpenid(),order_num,booking.getHouseName(),live_end,booking.getUserName(),hotelName);
  662. int m = bookService.updateBooking(booking);
  663. if (m>0){
  664. String returnMsg = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
  665. HttpServletResponse response = ServletActionContext.getResponse();
  666. response.setContentType("text/html;charset=utf-8");
  667. PrintWriter out = response.getWriter();
  668. out.println(returnMsg);
  669. out.flush();
  670. out.close(); // 发送成功消息给商家
  671. }
  672. return null;
  673. }
  674. } catch (Exception e){
  675. resultjson.put("message", "支付失败");
  676. resultjson.put("code", 205);
  677. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  678. return null;
  679. }
  680. resultjson.put("message", "支付失败");
  681. resultjson.put("code", 205);
  682. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  683. return null;
  684. }
  685. public String getOpen_id() {
  686. return open_id;
  687. }
  688. public void setOpen_id(String open_id) {
  689. this.open_id = open_id;
  690. }
  691. }