AppBookingAction.java 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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. * 进行更新数据将订单状态修改为已超时。
  146. * @return
  147. */
  148. public String updateOrderStatus()
  149. {
  150. JSONObject jsonObject = new JSONObject();
  151. if (Func.checkNull(bookingId))
  152. {
  153. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  154. jsonObject.put(B.message, "未传入bookingId---updateOrderStatus");
  155. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  156. return null;
  157. }
  158. try {
  159. Booking booking = bookService.getById(Func.parseInt(bookingId));
  160. Date createTime = DateUtil.parseDate(booking.getCreateTime(),DateUtil.Time_Formatter_Second);
  161. // 对订单时间进行累加
  162. String lockTime = TimeExchange.TimeRangeM(DateUtil.getFormatPaternDate(createTime), Func.parseInt(booking.getLockTime()));
  163. // 当前时间 大于 锁定时间 为true 且支付状态
  164. boolean flag = TimeExchange.CompareDate(lockTime, TimeExchange.getDate());
  165. if (flag)
  166. {
  167. // 如果超时,则将订单状态修改为已超时
  168. booking.setOrderStatus(Func.parseStr(PayEnum.SIX.getNum()));
  169. booking.setUpdateTime(TimeExchange.getDate());
  170. bookService.updateBooking(booking);
  171. System.out.println("已将订单"+booking.getOrderNum()+"标识为支付超时");
  172. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  173. jsonObject.put(B.message, "已将订单"+booking.getOrderNum()+"标识为支付超时");
  174. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  175. }
  176. } catch (ParseException e) {
  177. throw new RuntimeException(e);
  178. }
  179. return null;
  180. }
  181. /**
  182. * houseId; // 房型id
  183. * startTime; // 订房开始时间 2023/08/04
  184. * endTime; // 订房结束时间 2023/08/05
  185. * houseOrderNumber; // 订房数量
  186. * userName; // 住客姓名
  187. * userPhone; // 住客联系电话
  188. * userId; // 下单人id
  189. * 用户点击订房操作,生成订单
  190. */
  191. public String createOrder() {
  192. // 插入订单数据到表中
  193. JSONObject jsonObject = new JSONObject();
  194. if (Func.checkNull(houseId) || Func.checkNull(startTime) || Func.checkNull(endTime)
  195. || Func.checkNull(houseOrderNumber) || Func.checkNull(userName) || Func.checkNull(userPhone) || Func.checkNull(userId)) {
  196. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  197. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  198. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  199. return null;
  200. }
  201. // 将时间统一转化为 yyyy/MM/dd
  202. String startDate = DateUtil.parseDateToStr((Func.parseDate(startTime)), DateUtil.Time_Formatter_Day);
  203. String endDate = DateUtil.parseDateToStr((Func.parseDate(endTime)), DateUtil.Time_Formatter_Day);
  204. // 创建订单之前,先验证是否有房间
  205. String backMessage = appBookService.varificationHouse(houseId, startDate, endDate);
  206. if (!Func.checkNull(backMessage)) {
  207. jsonObject.put(B.code, ResultStatusCode.CANNTBOOKING.getStatus());
  208. jsonObject.put(B.message, backMessage);
  209. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  210. return null;
  211. }
  212. String bookingId= Func.parseStr(UUIDUtil.generateID());
  213. String mess = appBookService.createOrder(bookingId, houseId, startDate, endDate, houseOrderNumber, userName, userPhone, userId);
  214. if (!Func.checkNull(mess)) {
  215. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  216. jsonObject.put(B.message, mess);
  217. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  218. return null;
  219. }
  220. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  221. jsonObject.put(B.message, ResultStatusCode.OK.getMsg());
  222. jsonObject.put(B.data, bookingId);
  223. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  224. return null;
  225. }
  226. /**
  227. * 再次预订
  228. *
  229. * @return
  230. */
  231. public String reCreateOrder() {
  232. // 插入订单数据到表中
  233. JSONObject jsonObject = new JSONObject();
  234. if (Func.checkNull(houseId) || Func.checkNull(startTime) || Func.checkNull(endTime)
  235. || Func.checkNull(houseOrderNumber) || Func.checkNull(userName) || Func.checkNull(userPhone) || Func.checkNull(userId)) {
  236. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  237. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  238. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  239. return null;
  240. }
  241. // 将时间统一转化为 yyyy/MM/dd
  242. String startDate = DateUtil.parseDateToStr((Func.parseDate(startTime)), DateUtil.Time_Formatter_Day);
  243. String endDate = DateUtil.parseDateToStr((Func.parseDate(endTime)), DateUtil.Time_Formatter_Day);
  244. // 创建订单之前,先验证是否有房间
  245. String backMessage = appBookService.varificationHouse(houseId, startDate, endDate);
  246. if (Func.checkNull(backMessage)) {
  247. jsonObject.put(B.code, ResultStatusCode.CANNTBOOKING.getStatus());
  248. jsonObject.put(B.message, backMessage);
  249. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  250. return null;
  251. }
  252. appBookService.reCreateOrder(houseId, startTime, endTime, houseOrderNumber, userName, userPhone, userId);
  253. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  254. jsonObject.put(B.message, ResultStatusCode.OK.getMsg());
  255. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  256. return null;
  257. }
  258. /**
  259. * bookingId 订单id
  260. * 商家在已支付,待入住时点退单 ,进入退款流程后退单
  261. * 在已入住,时点退单,进入退款流程后退款
  262. * @return
  263. */
  264. public String chargeback() throws Exception {
  265. if (Func.checkNull(bookingId))
  266. return null;
  267. JSONObject jsonObject = new JSONObject();
  268. Booking book = bookService.getById(Func.parseInt(bookingId));
  269. Users users = userService.queryByUserId(book.getCreateUserid());
  270. if (book == null || users == null)
  271. return null;
  272. //1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款,10退款中
  273. int status = Func.parseInt(book.getOrderStatus());
  274. if (status == 1)
  275. {
  276. book.setOrderStatus(Func.parseStr(PayEnum.SEVEN.getNum())); // 如果在1待支付情况,用户点击取消订单按钮,后台直接修改取消订单操作
  277. book.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
  278. bookService.updateBooking(book);
  279. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  280. jsonObject.put(B.message, ResultStatusCode.OK.getMsg());
  281. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  282. return null;
  283. }else if (status == 2 || status == 3)
  284. {
  285. book.setOrderStatus(Func.parseStr(PayEnum.TEN.getNum())); // 如果在2已支付,3待入住 情况,商家点击取消订单按钮,后台先修改状态为退款中,然后走退款流程
  286. book.setRemake(Func.parseStr(PayEnum.EIGHT.getNum()));
  287. }else if(status == 4)
  288. {
  289. book.setOrderStatus(Func.parseStr(PayEnum.TEN.getNum())); // 如果在2已支付,3待入住 情况,商家点击取消订单按钮,后台先修改状态为退款中,然后走退款流程
  290. book.setRemake(Func.parseStr(PayEnum.NINE.getNum()));
  291. }
  292. if (book.getPayAccount() > 0)
  293. {
  294. // 注明如果支付金额大于0,则需要退款: (标识走退款操作)
  295. book.setRefundWay(OrderEnum.微信退款.toString());
  296. }
  297. jsonObject = refundMethod(book,users.getOpenid());
  298. ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  299. return null;
  300. }
  301. /**
  302. * bookingId 订单id
  303. * open_id
  304. * 取消订单功能: 用户点击
  305. * //1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款,10退款中
  306. *
  307. * @return
  308. */
  309. public String cancelBooking() throws Exception {
  310. JSONObject jsonObject = new JSONObject();
  311. if (Func.checkNull(this.bookingId)) {
  312. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  313. jsonObject.put(B.message, "未传入bookingId");
  314. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  315. return null;
  316. }
  317. // 取消订单,修改订单的支付状态
  318. Booking book = bookService.getById(Func.parseInt(bookingId));
  319. if (book == null )
  320. {
  321. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  322. jsonObject.put(B.message, "未获取到订单信息,请重新查询");
  323. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  324. return null;
  325. }
  326. //是否能取消订单(1是 2否)
  327. if (Func.parseInt(book.getHotelIsCanorder()) == 2)
  328. {
  329. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  330. jsonObject.put(B.message, "商家设置不可取消订单,请联系商家");
  331. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  332. return null;
  333. }
  334. AdminManager adminManager = adminManagerService.getById(Func.parseInt(book.getHotelManagerId()));
  335. //1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款,10退款中
  336. int status = Func.parseInt(book.getOrderStatus());
  337. if (status == 1)
  338. {
  339. book.setOrderStatus(Func.parseStr(PayEnum.SEVEN.getNum())); // 如果在1待支付情况,用户点击取消订单按钮,后台直接修改取消订单操作
  340. book.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
  341. book.setRemake(Func.parseStr(PayEnum.FIVE.getNum()));
  342. bookService.updateBooking(book);
  343. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  344. jsonObject.put(B.message, "已取消订单");
  345. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  346. return null;
  347. }else if (status == 2 || status == 3)
  348. {
  349. // 如果在2已支付,3待入住 情况,用户点击取消订单按钮,后台先修改状态为退款中,然后走退款流程
  350. book.setOrderStatus(Func.parseStr(PayEnum.TEN.getNum()));
  351. book.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
  352. book.setRemake(Func.parseStr(PayEnum.FIVE.getNum()));
  353. if (book.getPayAccount() > 0)
  354. {
  355. // 注明如果支付金额大于0,则需要退款: (标识走退款操作)
  356. book.setRefundWay(OrderEnum.微信退款.toString());
  357. }
  358. }
  359. Users users = userService.queryByUserId(book.getCreateUserid()); // 用户信息
  360. if (users == null) {
  361. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  362. jsonObject.put(B.message, "未查询到当前用户数据users,请先授权");
  363. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  364. return null;
  365. }
  366. jsonObject = refundMethod(book,users.getOpenid());
  367. jsonObject.put(B.data, book);
  368. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  369. return null;
  370. }
  371. /**
  372. * 退款方法
  373. * @param book
  374. * @param open_id
  375. * @return
  376. * @throws Exception
  377. */
  378. public JSONObject refundMethod(Booking book,String open_id) throws Exception {
  379. JSONObject jsonObject = new JSONObject();
  380. if (book == null || Func.checkNull(open_id))
  381. {
  382. jsonObject.put("code", 400);
  383. jsonObject.put("message", "未传入book和open_id数据---refundMethod");
  384. return jsonObject;
  385. }
  386. // 进行判断如果订单是退款中,并且标识字段微信退款,则进入退款流程
  387. if (Func.parseStr(PayEnum.TEN.getNum()).equals(book.getOrderStatus()) && OrderEnum.微信退款.toString().equals(book.getRefundWay())) {
  388. String ip = WeiXinUtil.ip;
  389. double money = book.getPayAccount();
  390. int a = (int) (money * 100);
  391. WechatUnifiedOrder w = new WechatUnifiedOrder();
  392. w.setAppid(WeiXinUtil.appid_c);
  393. w.setAttach("chuanghai");
  394. w.setBody("chuanghai");
  395. w.setMch_id(WeiXinUtil.account);
  396. w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
  397. w.setNotify_url(WeiXinUtil.ip_h + "/abkquery_refund.action");// 支付结果回调地址
  398. w.setOpenid(open_id);
  399. w.setOut_trade_no(book.getOrderNum());
  400. String refund_order = WxUtil.mchOrderNo();
  401. w.setOut_refund_no(refund_order); // 退款单号
  402. w.setSpbill_create_ip(ip);
  403. w.setTotal_fee(a);
  404. w.setRefund_fee(a); // 退款金额
  405. w.setTrade_type("JSAPI");
  406. SortedMap<String, String> params = new TreeMap<>();
  407. params.put("appid", w.getAppid());
  408. params.put("mch_id", w.getMch_id());
  409. params.put("nonce_str", w.getNonce_str());
  410. params.put("notify_url", w.getNotify_url());
  411. params.put("out_refund_no", w.getOut_refund_no()); // 退款单号
  412. params.put("out_trade_no", w.getOut_trade_no());
  413. params.put("refund_fee",w.getRefund_fee()+""); // 退款金额
  414. params.put("total_fee", w.getTotal_fee() + "");
  415. w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
  416. params.put("sign", w.getSign());
  417. //String retXml = JaxbUtil.getRequestXml(params);
  418. String xml = "<xml>" +
  419. "<appid>" + w.getAppid() + "</appid>" +
  420. "<mch_id>" + w.getMch_id() + "</mch_id>" +
  421. "<nonce_str>" + w.getNonce_str() + "</nonce_str>" +
  422. "<notify_url>" + w.getNotify_url() + "</notify_url>" +
  423. "<out_refund_no>" + w.getOut_refund_no() + "</out_refund_no>" +
  424. "<out_trade_no>" + w.getOut_trade_no() + "</out_trade_no>" +
  425. "<refund_fee>" + w.getRefund_fee()+"" + "</refund_fee>" +
  426. "<total_fee>" + w.getTotal_fee() + "" + "</total_fee>" +
  427. "<sign>" + w.getSign() + "</sign>" +
  428. "</xml>";
  429. //String msg = HttpsClient.sendPost2("https://api.mch.weixin.qq.com/secapi/pay/refund", retXml);
  430. String msg = HttpUtils.post2("https://api.mch.weixin.qq.com/secapi/pay/refund", xml);
  431. if (msg.indexOf("FAIL") > -1) {
  432. jsonObject.put("code", 507);
  433. jsonObject.put("message", "退款失败");
  434. ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  435. return null;
  436. }
  437. // 设置订单状态为退款中
  438. book.setOrderStatus(Func.parseStr(PayEnum.TEN.getNum()));
  439. book.setUpdateTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
  440. bookService.updateBooking(book); // 退款中
  441. jsonObject.put("code", 200);
  442. jsonObject.put("message", "申请已提交,请稍候查询");
  443. return jsonObject;
  444. }
  445. jsonObject.put("code", 400);
  446. jsonObject.put("message", "提交申请失败");
  447. return jsonObject;
  448. }
  449. // /**
  450. // * open_id
  451. // * bookingId
  452. // * 微信退款功能
  453. // */
  454. // public String refundBooking() throws Exception {
  455. // JSONObject resultJson = new JSONObject();
  456. // if (Func.checkNull(this.open_id) || Func.checkNull(this.bookingId)) {
  457. // resultJson.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  458. // resultJson.put(B.message, "未传入open_id和bookingId");
  459. // ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  460. // return null;
  461. // }
  462. //
  463. // Users users = userService.queryByOpenid(open_id); // 用户信息
  464. // Booking booking = bookService.getById(Func.parseInt(bookingId)); // 订单信息
  465. // if (users == null || booking == null) {
  466. // resultJson.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  467. // resultJson.put(B.message, "未查询到当前用户数据users,请先授权");
  468. // ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  469. // return null;
  470. // }
  471. //
  472. // String ip = WeiXinUtil.ip;
  473. // double money = booking.getPayAccount();
  474. // int a = (int) (money * 100);
  475. // WechatUnifiedOrder w = new WechatUnifiedOrder();
  476. // w.setAppid(WeiXinUtil.appid_c);
  477. // w.setAttach("chuanghai");
  478. // w.setBody("chuanghai");
  479. // w.setMch_id(WeiXinUtil.account);
  480. // w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
  481. // w.setNotify_url(WeiXinUtil.ip_h + "/abkquery_refund.action");// 支付结果回调地址
  482. // w.setOpenid(open_id);
  483. // w.setOut_trade_no(booking.getOrderNum());
  484. // String refund_order = WxUtil.mchOrderNo();
  485. // w.setOut_refund_no(refund_order); // 退款单号
  486. // w.setSpbill_create_ip(ip);
  487. // w.setTotal_fee(a);
  488. // w.setRefund_fee(a); // 退款金额
  489. // w.setTrade_type("JSAPI");
  490. // SortedMap<String, String> params = new TreeMap<>();
  491. // params.put("appid", w.getAppid());
  492. // params.put("mch_id", w.getMch_id());
  493. // params.put("nonce_str", w.getNonce_str());
  494. // params.put("notify_url", w.getNotify_url());
  495. // params.put("out_refund_no", w.getOut_refund_no()); // 退款单号
  496. // params.put("out_trade_no", w.getOut_trade_no());
  497. // params.put("refund_fee",w.getRefund_fee()+""); // 退款金额
  498. // params.put("total_fee", w.getTotal_fee() + "");
  499. //
  500. // w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
  501. // params.put("sign", w.getSign());
  502. // //String retXml = JaxbUtil.getRequestXml(params);
  503. // String xml = "<xml>" +
  504. // "<appid>" + w.getAppid() + "</appid>" +
  505. // "<mch_id>" + w.getMch_id() + "</mch_id>" +
  506. // "<nonce_str>" + w.getNonce_str() + "</nonce_str>" +
  507. // "<notify_url>" + w.getNotify_url() + "</notify_url>" +
  508. // "<out_refund_no>" + w.getOut_refund_no() + "</out_refund_no>" +
  509. // "<out_trade_no>" + w.getOut_trade_no() + "</out_trade_no>" +
  510. // "<refund_fee>" + w.getRefund_fee()+"" + "</refund_fee>" +
  511. // "<total_fee>" + w.getTotal_fee() + "" + "</total_fee>" +
  512. // "<sign>" + w.getSign() + "</sign>" +
  513. // "</xml>";
  514. // //String msg = HttpsClient.sendPost2("https://api.mch.weixin.qq.com/secapi/pay/refund", retXml);
  515. // String msg = HttpUtils.post2("https://api.mch.weixin.qq.com/secapi/pay/refund", xml);
  516. // if (msg.indexOf("FAIL") > -1) {
  517. // resultJson.put("code", 507);
  518. // resultJson.put("message", "退款失败");
  519. // ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  520. // return null;
  521. // }
  522. //
  523. // // 设置订单状态为退款中
  524. // booking.setOrderStatus(Func.parseStr(PayEnum.TEN.getNum()));
  525. // booking.setUpdateTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
  526. // bookService.updateBooking(booking); // 退款中
  527. //
  528. // resultJson.put("code", 200);
  529. // resultJson.put("message", "退款申请已提交,请稍候查询");
  530. // ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
  531. // return null;
  532. // }
  533. /* 退款回调 */
  534. public String query_refund() throws Exception {
  535. // 查询本地数据库中,正在退款中
  536. StringBuffer strSqlBook = new StringBuffer();
  537. strSqlBook.append(" and order_status = '").append(PayEnum.TEN.getNum()).append("' ");
  538. List<Booking> booking = bookService.queryList(strSqlBook.toString());
  539. if (booking==null){
  540. return null;
  541. }
  542. WechatUnifiedOrder w = new WechatUnifiedOrder();
  543. Map<String, String> params = new HashMap<String, String>();
  544. Booking book = new Booking();
  545. for (int i = 0; i < booking.size(); i++) {
  546. if (booking.get(i) == null)
  547. continue;
  548. book = booking.get(i);
  549. w = new WechatUnifiedOrder();
  550. w.setAppid(WeiXinUtil.appid_c);
  551. w.setAttach("chuanghai");
  552. w.setBody("chuanghai");
  553. w.setMch_id(WeiXinUtil.account);
  554. w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
  555. w.setOut_trade_no(book.getOrderNum());
  556. w.setTrade_type("JSAPI");
  557. params = new HashMap<String, String>();
  558. params.put("appid", w.getAppid());
  559. params.put("mch_id", w.getMch_id());
  560. params.put("out_trade_no", w.getOut_trade_no());
  561. params.put("nonce_str", w.getNonce_str());
  562. w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
  563. params.put("sign", w.getSign());
  564. String retXml = JaxbUtil.getRequestXml(params);
  565. String msg = HttpUtils.post("https://api.mch.weixin.qq.com/pay/refundquery", retXml);
  566. if (msg.indexOf("<refund_status_0><![CDATA[SUCCESS]]></refund_status_0>")>0) {
  567. // 退款成功,将数据写入本地数据库
  568. book.setRefundTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
  569. book.setRefundAmount(book.getHouseTotalPrice());
  570. book.setUpdateTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
  571. book.setOrderStatus(PayEnum.getValueBykey(Func.parseInt(book.getRemake()))); // 将走微信前的标识,写入回调函数
  572. bookService.updateBooking(book);
  573. // 发送成功消息给商家和用户
  574. AdminManager adminManager = adminManagerService.getById(Func.parseInt(book.getHotelManagerId()));
  575. Users users = userService.queryByUserId(book.getCreateUserid());
  576. String sendMsg = "订单:"+ book.getOrderNum();
  577. // 推给用户
  578. Message.send(users.getOpenid(),book.getOrderNum(),book.getUserName(),sendMsg,book.getCreateTime());
  579. // 推给商家
  580. String live_end = book.getOrderStartTime().substring(0,10) +"~"+ book.getOrderEndTime().substring(0,10);
  581. String hotelName = book.getHotelName();
  582. if (Func.checkNull(book.getHotelName()))
  583. {
  584. hotelName = book.getHouseName();
  585. }
  586. Message2.send(adminManager.getOpenid(),book.getOrderNum(),book.getHouseName(),live_end,book.getUserName(),hotelName,Func.parseStr(book.getId()));
  587. }
  588. }
  589. String returnMsg = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
  590. HttpServletResponse response = ServletActionContext.getResponse();
  591. response.setContentType("text/html;charset=utf-8");
  592. PrintWriter out = response.getWriter();
  593. out.println(returnMsg);
  594. out.flush();
  595. out.close();
  596. return null;
  597. }
  598. /**
  599. * bookingId 订单id
  600. * 删除订单功能(假删除)
  601. *
  602. * @return
  603. */
  604. public String delBooking() {
  605. if (Func.checkNull(getBookingId()))
  606. return null;
  607. JSONObject jsonObject = new JSONObject();
  608. String mess = bookService.delBooking(getBookingId());
  609. if (!Func.checkNull(mess)) {
  610. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  611. jsonObject.put(B.message, mess);
  612. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  613. return null;
  614. }
  615. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  616. jsonObject.put(B.message, ResultStatusCode.OK.getMsg());
  617. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  618. return null;
  619. }
  620. /**
  621. * 验证是否有房间
  622. * houseId, 房型id
  623. * startTime, 开始时间
  624. * endTime 结束时间
  625. *
  626. * @return
  627. */
  628. public String varificationHouse() {
  629. JSONObject jsonObject = new JSONObject();
  630. if (Func.checkNull(houseId) || Func.checkNull(startTime) || Func.checkNull(endTime)) {
  631. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  632. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  633. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  634. return null;
  635. }
  636. String backMessage = appBookService.varificationHouse(houseId, startTime, endTime);
  637. if (!Func.checkNull(backMessage)) {
  638. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  639. jsonObject.put(B.message, backMessage);
  640. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  641. return null;
  642. }
  643. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  644. jsonObject.put(B.message, OrderEnum.有空房.toString());
  645. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  646. return null;
  647. }
  648. /**
  649. * open_id
  650. * bookingId
  651. * 用户支付操作
  652. *
  653. * @return
  654. */
  655. public String pay() {
  656. JSONObject resultjson = new JSONObject();
  657. if (Func.checkNull(this.open_id) || Func.checkNull(this.bookingId)) {
  658. resultjson.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  659. resultjson.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  660. ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  661. return null;
  662. }
  663. Users users = userService.queryByOpenid(open_id); // 用户信息
  664. Booking booking = bookService.getById(Func.parseInt(bookingId)); // 订单信息
  665. if (users == null || booking == null || booking.getOrderStatus().equals(Func.parseStr(PayEnum.SIX.getNum()))) {
  666. resultjson.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  667. resultjson.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  668. ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  669. return null;
  670. }
  671. try {
  672. // 支付费用
  673. double totalPrice = booking.getHouseTotalPrice() ;
  674. int totalfee = Func.parseInt(totalPrice * 100);
  675. WechatUnifiedOrder w = new WechatUnifiedOrder();
  676. w.setAppid(WeiXinUtil.appid_c);
  677. w.setAttach("chuanghai");
  678. w.setBody("chuanghai");
  679. w.setMch_id(WeiXinUtil.account);
  680. w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
  681. w.setNotify_url(WeiXinUtil.ip_h + "/abkpayResulet.action");// 支付结果回调地址
  682. w.setOpenid(this.open_id);
  683. String orderNo = booking.getOrderNum(); // 订单编号
  684. w.setOut_trade_no(orderNo);
  685. w.setSpbill_create_ip(WeiXinUtil.ip);
  686. w.setTotal_fee(totalfee);
  687. w.setTrade_type("JSAPI");
  688. Map<String, String> params = new HashMap<String, String>();
  689. params.put("attach", w.getAttach());
  690. params.put("appid", w.getAppid());
  691. params.put("mch_id", w.getMch_id());
  692. params.put("nonce_str", w.getNonce_str());
  693. params.put("body", w.getBody());
  694. params.put("out_trade_no", w.getOut_trade_no());
  695. params.put("total_fee", Func.parseStr(w.getTotal_fee()));
  696. params.put("spbill_create_ip", w.getSpbill_create_ip());
  697. params.put("notify_url", w.getNotify_url());
  698. params.put("trade_type", w.getTrade_type());
  699. params.put("openid", w.getOpenid());
  700. w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
  701. params.put("sign", w.getSign());
  702. String retXml = JaxbUtil.getRequestXml(params);
  703. String msg = HttpUtils.post("https://api.mch.weixin.qq.com/pay/unifiedorder", retXml);
  704. System.out.println(msg);
  705. // 支付失败
  706. if (msg.indexOf("FAIL") > -1) {
  707. resultjson.put("code", "205");
  708. resultjson.put("message", "支付异常205");
  709. ResUtil.write(resultjson, ServletActionContext.getResponse());
  710. return null;
  711. }
  712. JaxbUtil requestBinder = new JaxbUtil(TongYiReturn.class, JaxbUtil.CollectionWrapper.class);
  713. TongYiReturn to = requestBinder.fromXml(msg);
  714. // 支付成功
  715. if (to.getReturn_code().equals("SUCCESS") && to.getResult_code().equals("SUCCESS")) {
  716. EndPay pay = new EndPay();
  717. pay.setAppId(WeiXinUtil.appid_c);
  718. pay.setSignType("MD5");
  719. pay.setTimeStamp(System.currentTimeMillis() / 1000 + "");
  720. pay.setPrepay_id(to.getPrepay_id());
  721. pay.setNonceStr(PayWxUtil.getNonceStr());
  722. Map<String, String> requestMap = new HashMap<String, String>();
  723. requestMap.put("appId", pay.getAppId());
  724. requestMap.put("timeStamp", pay.getTimeStamp());
  725. requestMap.put("nonceStr", pay.getNonceStr());
  726. requestMap.put("package", "prepay_id=" + pay.getPrepay_id());
  727. requestMap.put("signType", "MD5");
  728. pay.setPaySign(PayWxUtil.getSign(requestMap, WeiXinUtil.key));
  729. // 保存支付信息
  730. booking.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
  731. booking.setOrderStatus(Func.parseStr(PayEnum.ONE.getNum()));
  732. booking.setPayTime(DateUtil.getFormatPaternDate(new Date()));
  733. booking.setPayAccount(totalPrice);
  734. booking.setPayWay(OrderEnum.微信支付.toString());
  735. bookService.updateBooking(booking);
  736. resultjson.put(B.code, ResultStatusCode.OK.getStatus());
  737. resultjson.put(B.message, "返回成功");
  738. resultjson.put(B.data, pay);
  739. ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  740. }
  741. } catch (Exception e) {
  742. resultjson.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  743. resultjson.put(B.message, e.getMessage());
  744. ResponseUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  745. }
  746. return null;
  747. }
  748. /**
  749. * 支付成功后回调,推送消息给商家
  750. *
  751. */
  752. public String payResulet(){
  753. System.out.println("进入支付回调====");
  754. JSONObject resultjson = new JSONObject();
  755. try{
  756. BufferedReader reader = null;// BufferedReader 字符输入流
  757. reader = request.getReader();
  758. String line = "";
  759. String xmlString = null;
  760. StringBuilder inputString = new StringBuilder();
  761. while ((line = reader.readLine()) != null) {
  762. inputString.append(line);
  763. }
  764. xmlString = inputString.toString();
  765. request.getReader().close();
  766. JaxbUtil requestBinder = new JaxbUtil(PayResult.class, PayResult.class);
  767. PayResult result = requestBinder.fromXml(xmlString);// Xml到Java,用fromXML()方法
  768. String order_num = result.getOut_trade_no();
  769. System.out.println("支付成功回调,订单号:"+order_num);
  770. Booking booking = bookService.queryByOrder(order_num);
  771. // 支付成功
  772. if (result.getResult_code().equals("SUCCESS") && booking!=null && booking.getOrderStatus().equals("1")) {
  773. // 保存支付信息
  774. booking.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
  775. booking.setOrderStatus(Func.parseStr(PayEnum.TWO.getNum()));
  776. booking.setPayTime(DateUtil.getFormatPaternDate(new Date()));
  777. booking.setPayWay(OrderEnum.微信支付.toString());
  778. // 进行业务判断,如果是商家设定了自动接单,则支付状态直接变为待入住 接单设置(1自动接单 2手动接单)
  779. if ("1".equals(booking.getHotelIsCanorder()))
  780. {
  781. // 自动接单,订单状态修改为待入住
  782. booking.setOrderStatus(Func.parseStr(PayEnum.THREE.getNum()));
  783. String userId = booking.getCreateUserid();
  784. Users users = userService.queryByUserId(userId);
  785. String mess = "您已成功订房,订单号:"+order_num;
  786. Message.send(users.getOpenid(),order_num, users.getUser_name(),mess, booking.getPayTime());
  787. }
  788. // 手动接单,发消息给商家
  789. AdminManager adminManager = adminManagerService.getById(Func.parseInt(booking.getHotelManagerId()));
  790. String live_end = booking.getOrderStartTime().substring(0,10) +"~"+ booking.getOrderEndTime().substring(0,10);
  791. String hotelName = booking.getHotelName();
  792. if (Func.checkNull(booking.getHotelName()))
  793. {
  794. hotelName = booking.getHouseName();
  795. }
  796. System.out.println(adminManager.getOpenid()+"==="+order_num +"==="+booking.getHouseName()+"==="+live_end+"==="+booking.getUserName()+"==="+hotelName);
  797. Message2.send(adminManager.getOpenid(),order_num,booking.getHouseName(),live_end,booking.getUserName(),hotelName, Func.parseStr(booking.getId()));
  798. int m = bookService.updateBooking(booking);
  799. if (m>0){
  800. String returnMsg = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
  801. HttpServletResponse response = ServletActionContext.getResponse();
  802. response.setContentType("text/html;charset=utf-8");
  803. PrintWriter out = response.getWriter();
  804. out.println(returnMsg);
  805. out.flush();
  806. out.close(); // 发送成功消息给商家
  807. }
  808. return null;
  809. }
  810. } catch (Exception e){
  811. resultjson.put("message", "支付失败");
  812. resultjson.put("code", 205);
  813. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  814. return null;
  815. }
  816. resultjson.put("message", "支付失败");
  817. resultjson.put("code", 205);
  818. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  819. return null;
  820. }
  821. public String getOpen_id() {
  822. return open_id;
  823. }
  824. public void setOpen_id(String open_id) {
  825. this.open_id = open_id;
  826. }
  827. }