AppBookingAction.java 40 KB

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