Browse Source

退款操作

Administrator 2 years ago
parent
commit
d55a8e98d9

+ 2 - 2
mhotel/src/com/happy/Model/Booking.java

@@ -8,8 +8,8 @@ public class Booking {
 
   private Integer id;
   private String orderNum; // 订单号
-  private String orderStatus; // 状态,1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款
-  private String orderName; // 状态名称,1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款
+  private String orderStatus; // 状态,1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款,10退款中
+  private String orderName; // 状态名称,1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款,10退款中
   private String userIdnumber; // 住客身份号
   private String userName;//住客姓名
   private String userPhone;// 住客手机号

+ 1 - 1
mhotel/src/com/happy/Model/weixin/WeiXinUtil.java

@@ -6,7 +6,7 @@ public class WeiXinUtil {
     public static String appid_c = "wxa5cc2178ff34bc8e";
     public static String ip = "171.34.223.11";
     public static String screct_c = "7e6b036298b718da6d1afe4ae5364a1f";
-    public static String key = "密钥设置";
+    public static String key = "v2";
     // 诚欣测试号
     /*public static String appid_c = "wx5fcbd4d313aa4c15";
     public static String screct_c = "a72db0c99cde741eb391af69067aacfa";*/

+ 1 - 1
mhotel/src/com/happy/Until/Enum/OrderEnum.java

@@ -1,5 +1,5 @@
 package com.happy.Until.Enum;
 
 public enum OrderEnum {
-    间, 晚,房间已满,有空房,微信支付;
+    间, 晚,房间已满,有空房,微信支付,微信退款;
 }

+ 2 - 2
mhotel/src/com/happy/Until/Enum/PayEnum.java

@@ -2,11 +2,11 @@ package com.happy.Until.Enum;
 
 /**
  * 订单支付九种状态:
- *     1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款
+ *     1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款,10退款中
  */
 public enum PayEnum {
 
-    ONE(1,"待支付"),TWO(2,"已支付"),THREE(3,"待入住"),FOUR(4,"已入住"),FIVE(5,"已消费"),SIX(6,"支付超时"),SEVEN(7,"已取消"),EIGHT(8,"已退单"),NINE(9,"已退款");
+    ONE(1,"待支付"),TWO(2,"已支付"),THREE(3,"待入住"),FOUR(4,"已入住"),FIVE(5,"已消费"),SIX(6,"支付超时"),SEVEN(7,"已取消"),EIGHT(8,"已退单"),NINE(9,"已退款"),TEN(10,"退款中");
 
     private int num;
     private String payStatus;

+ 226 - 4
mhotel/src/com/happy/action/AppBookingAction.java

@@ -28,9 +28,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.BufferedReader;
 import java.io.PrintWriter;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 
 /**
  * 订单进去Action请求交互
@@ -212,17 +210,19 @@ public class AppBookingAction extends ActionSupport implements ServletRequestAwa
 
     /**
      * bookingId 订单id
+     * open_id
      * 取消订单功能:
      * //1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款
      *
      * @return
      */
-    public String cancelBooking() {
+    public String cancelBooking() throws Exception {
         if (Func.checkNull(getBookingId()))
             return null;
 
         JSONObject jsonObject = new JSONObject();
 
+        // 取消订单,修改订单的支付状态
         String mess = bookService.cancelBooking(getBookingId());
         if (!Func.checkNull(mess)) {
             jsonObject.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
@@ -231,6 +231,89 @@ public class AppBookingAction extends ActionSupport implements ServletRequestAwa
             return null;
         }
 
+        Booking booking = bookService.getById(Func.parseInt(this.bookingId));
+        if (booking == null || Func.checkNull(booking.getRefundWay()))
+            return null;
+
+        // 进行判断如果订单是已取消,并且标识字段微信退款,则进入退款流程
+        if (Func.parseStr(PayEnum.SEVEN.getNum()).equals(booking.getOrderStatus()) && OrderEnum.微信退款.toString().equals(booking.getRefundWay())) {
+            if (Func.checkNull(this.open_id) || Func.checkNull(this.bookingId)) {
+                jsonObject.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
+                jsonObject.put(B.MESSAGE, ResultStatusCode.BAD_REQUEST.getMsg());
+                ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
+                return null;
+            }
+
+            Users users = userService.queryByOpenid(open_id); // 用户信息
+            if (users == null || booking == null) {
+                jsonObject.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
+                jsonObject.put(B.MESSAGE, ResultStatusCode.BAD_REQUEST.getMsg());
+                ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
+                return null;
+            }
+
+            String ip = WeiXinUtil.ip;
+            double money = booking.getPayAccount();
+            int a = (int) (money * 100);
+            WechatUnifiedOrder w = new WechatUnifiedOrder();
+            w.setAppid(WeiXinUtil.appid_c);
+            w.setAttach("chuanghai");
+            w.setBody("chuanghai");
+            w.setMch_id(WeiXinUtil.account);
+            w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
+            w.setNotify_url(WeiXinUtil.ip_h + "/appquery_refund.action");// 支付结果回调地址
+            w.setOpenid(open_id);
+            w.setOut_trade_no(booking.getOrderNum());
+            String refund_order = WxUtil.mchOrderNo();
+            w.setOut_refund_no(refund_order);   // 退款单号
+            w.setSpbill_create_ip(ip);
+            w.setTotal_fee(a);
+            w.setRefund_fee(a);   // 退款金额
+            w.setTrade_type("JSAPI");
+            SortedMap<String, String> params = new TreeMap<>();
+            params.put("appid", w.getAppid());
+            params.put("mch_id", w.getMch_id());
+            params.put("nonce_str", w.getNonce_str());
+            params.put("notify_url", w.getNotify_url());
+            params.put("out_refund_no", w.getOut_refund_no());   // 退款单号
+            params.put("out_trade_no", w.getOut_trade_no());
+            params.put("refund_fee",w.getRefund_fee()+"");  // 退款金额
+            params.put("total_fee", w.getTotal_fee() + "");
+
+            w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
+            params.put("sign", w.getSign());
+            //String retXml = JaxbUtil.getRequestXml(params);
+            String xml = "<xml>" +
+                    "<appid>" + w.getAppid() + "</appid>" +
+                    "<mch_id>" + w.getMch_id() + "</mch_id>" +
+                    "<nonce_str>" + w.getNonce_str() + "</nonce_str>" +
+                    "<notify_url>" + w.getNotify_url() + "</notify_url>" +
+                    "<out_refund_no>" + w.getOut_refund_no() + "</out_refund_no>" +
+                    "<out_trade_no>" + w.getOut_trade_no() + "</out_trade_no>"	+
+                    "<refund_fee>" + w.getRefund_fee()+"" + "</refund_fee>" +
+                    "<total_fee>" + w.getTotal_fee() + "" + "</total_fee>" +
+                    "<sign>" + w.getSign() + "</sign>"	+
+                    "</xml>";
+            //String msg = HttpsClient.sendPost2("https://api.mch.weixin.qq.com/secapi/pay/refund", retXml);
+            String msg = HttpUtils.post2("https://api.mch.weixin.qq.com/secapi/pay/refund", xml);
+            if (msg.indexOf("FAIL") > -1) {
+                jsonObject.put("code", 507);
+                jsonObject.put("message", "退款失败");
+                ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
+                return null;
+            }
+
+            // 设置订单状态为退款中
+            booking.setOrderStatus(Func.parseStr(PayEnum.TEN.getNum()));
+            booking.setUpdateTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
+            bookService.updateBooking(booking);  // 退款中
+
+            jsonObject.put("code", 200);
+            jsonObject.put("message", "退款申请已提交,请稍候查询");
+            ResUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
+            return null;
+        }
+
         jsonObject.put(B.CODE, ResultStatusCode.OK.getStatus());
         jsonObject.put(B.MESSAGE, ResultStatusCode.OK.getMsg());
         ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
@@ -238,6 +321,145 @@ public class AppBookingAction extends ActionSupport implements ServletRequestAwa
     }
 
     /**
+     * open_id
+     * bookingId
+     * 微信退款功能
+     */
+    public String refundBooking() throws Exception {
+        JSONObject resultJson = new JSONObject();
+        if (Func.checkNull(this.open_id) || Func.checkNull(this.bookingId)) {
+            resultJson.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
+            resultJson.put(B.MESSAGE, ResultStatusCode.BAD_REQUEST.getMsg());
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
+            return null;
+        }
+
+        Users users = userService.queryByOpenid(open_id); // 用户信息
+        Booking booking = bookService.getById(Func.parseInt(bookingId)); // 订单信息
+        if (users == null || booking == null) {
+            resultJson.put(B.CODE, ResultStatusCode.BAD_REQUEST.getStatus());
+            resultJson.put(B.MESSAGE, ResultStatusCode.BAD_REQUEST.getMsg());
+            ResponseUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
+            return null;
+        }
+
+        String ip = WeiXinUtil.ip;
+        double money = booking.getPayAccount();
+        int a = (int) (money * 100);
+        WechatUnifiedOrder w = new WechatUnifiedOrder();
+        w.setAppid(WeiXinUtil.appid_c);
+        w.setAttach("chuanghai");
+        w.setBody("chuanghai");
+        w.setMch_id(WeiXinUtil.account);
+        w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
+        w.setNotify_url(WeiXinUtil.ip_h + "/appquery_refund.action");// 支付结果回调地址
+        w.setOpenid(open_id);
+        w.setOut_trade_no(booking.getOrderNum());
+        String refund_order = WxUtil.mchOrderNo();
+        w.setOut_refund_no(refund_order);   // 退款单号
+        w.setSpbill_create_ip(ip);
+        w.setTotal_fee(a);
+        w.setRefund_fee(a);   // 退款金额
+        w.setTrade_type("JSAPI");
+        SortedMap<String, String> params = new TreeMap<>();
+        params.put("appid", w.getAppid());
+        params.put("mch_id", w.getMch_id());
+        params.put("nonce_str", w.getNonce_str());
+        params.put("notify_url", w.getNotify_url());
+        params.put("out_refund_no", w.getOut_refund_no());   // 退款单号
+        params.put("out_trade_no", w.getOut_trade_no());
+        params.put("refund_fee",w.getRefund_fee()+"");  // 退款金额
+        params.put("total_fee", w.getTotal_fee() + "");
+
+        w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
+        params.put("sign", w.getSign());
+        //String retXml = JaxbUtil.getRequestXml(params);
+        String xml = "<xml>" +
+                "<appid>" + w.getAppid() + "</appid>" +
+                "<mch_id>" + w.getMch_id() + "</mch_id>" +
+                "<nonce_str>" + w.getNonce_str() + "</nonce_str>" +
+                "<notify_url>" + w.getNotify_url() + "</notify_url>" +
+                "<out_refund_no>" + w.getOut_refund_no() + "</out_refund_no>" +
+                "<out_trade_no>" + w.getOut_trade_no() + "</out_trade_no>"	+
+                "<refund_fee>" + w.getRefund_fee()+"" + "</refund_fee>" +
+                "<total_fee>" + w.getTotal_fee() + "" + "</total_fee>" +
+                "<sign>" + w.getSign() + "</sign>"	+
+                "</xml>";
+        //String msg = HttpsClient.sendPost2("https://api.mch.weixin.qq.com/secapi/pay/refund", retXml);
+        String msg = HttpUtils.post2("https://api.mch.weixin.qq.com/secapi/pay/refund", xml);
+        if (msg.indexOf("FAIL") > -1) {
+            resultJson.put("code", 507);
+            resultJson.put("message", "退款失败");
+            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
+            return null;
+        }
+
+        // 设置订单状态为退款中
+        booking.setOrderStatus(Func.parseStr(PayEnum.TEN.getNum()));
+        booking.setUpdateTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
+        bookService.updateBooking(booking);  // 退款中
+
+        resultJson.put("code", 200);
+        resultJson.put("message", "退款申请已提交,请稍候查询");
+        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
+        return null;
+    }
+
+    /* 退款回调 */
+    public String query_refund() throws Exception {
+
+        // 查询本地数据库中,正在退款中
+        StringBuffer strSqlBook = new StringBuffer();
+        strSqlBook.append(" and order_status = '").append(PayEnum.TEN.getNum()).append("' ");
+        List<Booking> booking = bookService.queryList(strSqlBook.toString());
+        if (booking==null){
+            return null;
+        }
+        WechatUnifiedOrder w = new WechatUnifiedOrder();
+        Map<String, String> params = new HashMap<String, String>();
+        Booking book = new Booking();
+        for (int i = 0; i < booking.size(); i++) {
+            if (booking.get(i) == null)
+                continue;
+            book = booking.get(i);
+            w = new WechatUnifiedOrder();
+            w.setAppid(WeiXinUtil.appid_c);
+            w.setAttach("chuanghai");
+            w.setBody("chuanghai");
+            w.setMch_id(WeiXinUtil.account);
+            w.setNonce_str(PayWxUtil.getNonceStr());// 随机支付串
+            w.setOut_trade_no(book.getOrderNum());
+            w.setTrade_type("JSAPI");
+            params = new HashMap<String, String>();
+            params.put("appid", w.getAppid());
+            params.put("mch_id", w.getMch_id());
+            params.put("out_trade_no", w.getOut_trade_no());
+            params.put("nonce_str", w.getNonce_str());
+            w.setSign(PayWxUtil.getSign(params, WeiXinUtil.key));
+            params.put("sign", w.getSign());
+            String retXml = JaxbUtil.getRequestXml(params);
+            String msg = HttpUtils.post("https://api.mch.weixin.qq.com/pay/refundquery", retXml);
+            if (msg.indexOf("<refund_status_0><![CDATA[SUCCESS]]></refund_status_0>")>0) {
+                // 退款成功,将数据写入本地数据库
+                book.setRefundTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
+                book.setRefundAmount(book.getHouseTotalPrice());
+                book.setUpdateTime(DateUtil.parseDateToStr(new Date(),DateUtil.Time_Formatter_Second));
+                bookService.updateBooking(book);
+            }
+        }
+        String returnMsg = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
+        HttpServletResponse response = ServletActionContext.getResponse();
+        response.setContentType("text/html;charset=utf-8");
+        PrintWriter out = response.getWriter();
+        out.println(returnMsg);
+        out.flush();
+        out.close(); // 发送成功消息给商家
+        return null;
+    }
+
+
+
+    /**
      * bookingId 订单id
      * 删除订单功能(假删除)
      *

+ 11 - 1
mhotel/src/com/happy/action/AppHomePageAction.java

@@ -47,6 +47,7 @@ public class AppHomePageAction extends ActionSupport implements ServletRequestAw
 
     public String userCode; // 第一次进来获取用户的code
     public String queryValue; // 首页查寻搜索关键词
+    private String hotelTownship; // 所属乡镇
     public Date startTimeQuery; // 查询订房开始时间
     public Date endTimeQuery; // 查询订房结束时间
 
@@ -107,12 +108,13 @@ public class AppHomePageAction extends ActionSupport implements ServletRequestAw
     /**
      * 进入首页,展示民宿数据列表
      * queryValue 查询字段
+     * hotelTownship 所属乡镇
      * @return
      */
     public String homePage()
     {
         // 获取民宿列表
-        IPage iPage = appHomePageService.getHotelList(queryValue,page,rows);
+        IPage iPage = appHomePageService.getHotelList(queryValue, hotelTownship,page,rows);
         ResponseUtil.writeJsonIPage(ServletActionContext.getResponse(),iPage);
         return null;
     }
@@ -212,4 +214,12 @@ public class AppHomePageAction extends ActionSupport implements ServletRequestAw
     public void setEndTimeQuery(Date endTimeQuery) {
         this.endTimeQuery = endTimeQuery;
     }
+
+    public String getHotelTownship() {
+        return hotelTownship;
+    }
+
+    public void setHotelTownship(String hotelTownship) {
+        this.hotelTownship = hotelTownship;
+    }
 }

+ 1 - 1
mhotel/src/com/happy/service/AppHomePageService.java

@@ -19,7 +19,7 @@ public interface AppHomePageService {
      * @param rows
      * @return
      */
-    public IPage getHotelList(String queryValue, int page, int rows);
+    public IPage getHotelList(String queryValue,String hotelTownship, int page, int rows);
 
     public int getHotelTotal(String queryValue);
 

+ 4 - 1
mhotel/src/com/happy/service/impl/AppHomePageImplService.java

@@ -37,12 +37,15 @@ public class AppHomePageImplService implements AppHomePageService {
 
 
     @Override
-    public IPage<Hotel> getHotelList(String queryValue, int page, int rows) {
+    public IPage<Hotel> getHotelList(String queryValue,String hotelTownship, int page, int rows) {
         IPage<Hotel> iPage = new IPage();
         StringBuilder strSql = new StringBuilder("");
         if (!Func.checkNull(queryValue)){
             strSql.append(" and hname like '%").append(queryValue).append("%' ");
         }
+        if (!Func.checkNull(hotelTownship)){
+            strSql.append(" and hotel_township = '").append(hotelTownship).append("' ");
+        }
 
         return hotelService.queryPagePrice(strSql.toString(),page,rows);
     }

+ 4 - 1
mhotel/src/com/happy/service/impl/BookImplService.java

@@ -2,6 +2,7 @@ package com.happy.service.impl;
 
 import com.happy.Model.Booking;
 import com.happy.Until.DateUtil;
+import com.happy.Until.Enum.OrderEnum;
 import com.happy.Until.Enum.PayEnum;
 import com.happy.Until.Func;
 import com.happy.dao.BookDao;
@@ -98,8 +99,10 @@ public class BookImplService implements BookService {
         {
             book.setOrderStatus(Func.parseStr(PayEnum.SEVEN.getNum()));
             book.setUpdateTime(DateUtil.getFormatPaternDate(new Date()));
+            // 注明需要退款:
+            book.setPayWay(OrderEnum.微信退款.toString());
             bookDao.updateBooking(book);
-            // TODO 退款操作
+            // 退款操作
             return null;
         }
         return "不可取消订单";