Selaa lähdekoodia

处理安卓调用修改订单状态接口的处理逻辑

wangyuanbo 2 vuotta sitten
vanhempi
commit
afc5acae19

+ 54 - 17
imcs-bt-be/imcs-authority-server/src/main/java/com/github/zuihou/api/AndroidApi.java

@@ -163,16 +163,46 @@ public class AndroidApi {
 
     @ApiOperation("订单状态查询接口")
     @GetMapping({"/getOrder"})
-    public R<Map<Object, Object>> getOrder(@RequestParam(name = "orderId", required = true) Long orderId) {
+    public R<Map<Object, Object>> getOrder(@RequestParam(name = "orderId", required = true) Long orderId) throws Exception {
         BaseContextHandler.setTenant("0000");
-        HashMap<Object, Object> resultMap = new HashMap<>();
-        Order order = orderService.getById(orderId);
-        String orderStatus = order.getOrderStatus();
-        if ("1" == orderStatus) {
+        String orderStatus = "";
+        String status = wxPayService.wxQueryOrder(orderId + "");
+        UpdateWrapper<Order> orderUpdateWrapper = new UpdateWrapper<>();
+//        orderService.upd
+
+        if (status.equals("NOTPAY")) {
+//未支付
+            HashMap<Object, Object> resultMap = new HashMap<>();
+            resultMap.put("order", "0");
+            return R.success(resultMap);
+        } else if (status.equals("SUCCESS")) {
+//支付成功
+            HashMap<Object, Object> resultMap = new HashMap<>();
+            Order order = orderService.getById(orderId);
             ProductConfig productConfig = productConfigService.getProdCfg(order.getOrderSpecs());
             resultMap.put("productConfig", productConfig);
+            resultMap.put("order", "1");
+            return R.success(resultMap);
+        } else if (status.equals("REFUND")) {
+//转入退款
+            HashMap<Object, Object> resultMap = new HashMap<>();
+            resultMap.put("order", "5");
+            return R.success(resultMap);
+        } else if (status.equals("CLOSED")) {
+//已关闭
+            HashMap<Object, Object> resultMap = new HashMap<>();
+            resultMap.put("order", "2");
+            return R.success(resultMap);
         }
-        resultMap.put("order", order);
+        HashMap<Object, Object> resultMap = new HashMap<>();
+//        Order order = orderService.getById(orderId);
+//        String orderStatus = order.getOrderStatus();
+//        if ("1" == orderStatus) {
+//            ProductConfig productConfig = productConfigService.getProdCfg(order.getOrderSpecs());
+//            resultMap.put("productConfig", productConfig);
+//        }
+
+        resultMap.put("order", "0");
 
         return R.success(resultMap);
     }
@@ -292,7 +322,14 @@ public class AndroidApi {
                 String trade_state = cipherDto.getTrade_state();
                 Order localOrder = orderService.getById(cipherDto.getOut_trade_no());
                 localOrder.setUpdateTime(LocalDateTime.now());
-                localOrder.setOrderStatus(trade_state);
+                if(trade_state.equals("SUCCESS")){
+                    localOrder.setOrderStatus("1");
+                }else if(trade_state.equals("CLOSED")){
+                    localOrder.setOrderStatus("2");
+                }else if(trade_state.equals("REFUND")){
+                    localOrder.setOrderStatus("5");
+                }
+//                localOrder.setOrderStatus(trade_state);
 //                LocalDateTime parse = LocalDateTime.parse(success_time, dtf);
                 LocalDateTime localDateTime = DateUtil.parseLocalDateTime(success_time, "yyyy-MM-dd'T'HH:mm:ssXXX");
                 localOrder.setOrderPayTime(localDateTime);
@@ -345,20 +382,20 @@ public class AndroidApi {
         //        生产异常,发起退款
         if (orderStatus == "4") {
             //调用微信支付api,发起退款
-            int i = wxPayService.wxCloseOrder(orderId);
-            if (i == 204) {
+//            int i = wxPayService.wxCloseOrder(orderId);
+//            if (i == 204) {
 //                订单关闭成功,修改本地订单状态
-                UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
-                updateWrapper.eq("id", orderId).set("order_status", orderStatus);
-                boolean update = orderService.update(updateWrapper);
-                return R.success(update);
-            } else {
-                return R.fail("订单关闭失败,建议等待5秒,然后调用被扫订单结果查询API,查询当前订单的不同状态,决定下一步的操作");
-            }
+//                UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
+//                updateWrapper.eq("id", orderId).set("order_status", orderStatus);
+//                boolean update = orderService.update(updateWrapper);
+//                return R.success(update);
+//            } else {
+//                return R.fail("订单关闭失败,建议等待5秒,然后调用被扫订单结果查询API,查询当前订单的不同状态,决定下一步的操作");
+//            }
         }
 
 
-        if (orderStatus == "5") {
+        if (orderStatus == "3") {
             UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
             updateWrapper.eq("id", orderId).set("order_status", orderStatus);
             boolean update = orderService.update(updateWrapper);

+ 26 - 30
imcs-bt-be/imcs-authority-server/src/main/java/com/github/zuihou/service/WxPayService.java

@@ -6,6 +6,7 @@ import com.github.zuihou.business.wxpay.UnifiedorderDto;
 import com.github.zuihou.cfg.WxPayCfg;
 import com.github.zuihou.utils.WxPayUtil;
 import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.util.EntityUtils;
@@ -15,6 +16,9 @@ import org.springframework.stereotype.Service;
 import java.io.IOException;
 import java.nio.charset.Charset;
 
+import static org.apache.http.HttpHeaders.ACCEPT;
+import static org.apache.http.entity.ContentType.APPLICATION_JSON;
+
 /**
  * @author User
  */
@@ -61,6 +65,7 @@ public class WxPayService {
 
     /**
      * 调用关闭订单接口
+     *
      * @param out_trade_no
      * @return
      * @throws Exception
@@ -102,49 +107,40 @@ public class WxPayService {
 
 
     /**
-     * 调用关闭订单接口
+     * 调用查询订单接口
+     *
      * @param out_trade_no
      * @return
      * @throws Exception
      */
-    public int wxCloseOrder(String out_trade_no) throws Exception {
-        int statusCode = 0;
-        String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + out_trade_no + "/close";
+    public String wxQueryOrder(String out_trade_no) throws Exception {
         String mchId = wxPayCfg.getMchId();
+        String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + out_trade_no + "?mchid=" + mchId;
 
-        JSONObject payObj = new JSONObject();
-        payObj.put("mchid", mchId);
-        String body = payObj.toJSONString();
-
-
-        HttpPost httpPost = new HttpPost(url);
-        StringEntity entity = new StringEntity(body, Charset.forName("UTF-8"));
-        entity.setContentType("application/json");
-        httpPost.setEntity(entity);
-        httpPost.addHeader("Accept", "application/json");
-        httpPost.addHeader("Content-type", "application/json; charset=utf-8");
-        CloseableHttpResponse response = WxPayUtil.getClint(wxPayCfg.getMchId(), wxPayCfg.getCertNo(), wxPayCfg.getPrivateKeyPath(), wxPayCfg.getApiV3Key()).execute(httpPost);
+        HttpGet httpGet = new HttpGet(url);
+        httpGet.addHeader(ACCEPT, APPLICATION_JSON.toString());
+        CloseableHttpResponse response = WxPayUtil.getClint(wxPayCfg.getMchId(), wxPayCfg.getCertNo(), wxPayCfg.getPrivateKeyPath(), wxPayCfg.getApiV3Key()).execute(httpGet);
 
         try {
-            //响应码
-            statusCode = response.getStatusLine().getStatusCode();
-            System.out.println("关闭订单响应码:{},无响应体" + statusCode);
+            int statusCode = response.getStatusLine().getStatusCode();
+            if (statusCode == 200) {
+                String s = EntityUtils.toString(response.getEntity());
+                JSONObject jsonObject = JSONObject.parseObject(s);
+                String tradeState = jsonObject.getString("trade_state");
+
+                return tradeState;
+            } else if (statusCode == 204) {
+                System.out.println("微信统查询订单接口返回204,success,处理成功,但是无返回");
 
+            } else {
+                System.out.println("微信查询订单接口返回fail,resp  code=" + statusCode + "return body =" + EntityUtils.toString(response.getEntity()));
+                throw new IOException("微信查询订单接口失败");
+            }
         } finally {
             response.close();
         }
-
-        return statusCode;
-
-//        202用户支付中,需要输入密码	等待5秒,然后调用被扫订单结果查询API,查询当前订单的不同状态,决定下一步的操作
-//
-//
-//        202  用户支付中,需要输入密码   204 正常  400 已关闭
+        return "";
     }
 
 
-
-
-
-
 }