wangming vor 1 Monat
Ursprung
Commit
348dd0bb02

+ 2 - 2
yami-shop-api/src/main/java/com/yami/shop/api/controller/WxPayController.java

@@ -1,7 +1,7 @@
 package com.yami.shop.api.controller;
 
 import com.alibaba.fastjson2.JSONObject;
-import com.yami.shop.wx.po.JsapiPo;
+import com.yami.shop.wx.po.JsapiPayInfoPo;
 import com.yami.shop.wx.service.WxProviderService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -27,7 +27,7 @@ public class WxPayController {
 
     @PostMapping("jsapiPay")
     @ApiOperation(value = "小程序支付", notes = "小程序支付")
-    public ResponseEntity<Map<String, Object>> jsapiPay(@Validated @RequestBody JsapiPo po) {
+    public ResponseEntity<Map<String, Object>> jsapiPay(@Validated @RequestBody JsapiPayInfoPo po) {
         return ResponseEntity.ok(wxProviderService.subJsapi(po));
     }
 

+ 1 - 1
yami-shop-wx/src/main/java/com/yami/shop/wx/po/JsapiPo.java → yami-shop-wx/src/main/java/com/yami/shop/wx/po/JsapiPayInfoPo.java

@@ -11,7 +11,7 @@ import javax.validation.constraints.NotNull;
  */
 @Data
 @ApiModel(value = "小程序下单")
-public class JsapiPo {
+public class JsapiPayInfoPo {
 
     @ApiModelProperty(value = "详情")
     @NotNull(message = "详情不能为空...")

+ 2 - 2
yami-shop-wx/src/main/java/com/yami/shop/wx/service/WxProviderService.java

@@ -1,7 +1,7 @@
 package com.yami.shop.wx.service;
 
 import com.alibaba.fastjson2.JSONObject;
-import com.yami.shop.wx.po.JsapiPo;
+import com.yami.shop.wx.po.JsapiPayInfoPo;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -9,7 +9,7 @@ import java.util.Map;
 
 public interface WxProviderService {
 
-    Map<String, Object> subJsapi(JsapiPo po);
+    Map<String, Object> subJsapi(JsapiPayInfoPo po);
 
     String refundOrder(String orderNo,Integer refundMoney,Integer total);
 

+ 7 - 32
yami-shop-wx/src/main/java/com/yami/shop/wx/service/impl/WxProviderServiceImpl.java

@@ -12,9 +12,8 @@ import com.wechat.pay.contrib.apache.httpclient.cert.CertificatesManager;
 import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
 import com.yami.shop.wx.config.CombinePayUrlEnum;
 import com.yami.shop.wx.config.WechatPayServiceConfig;
-import com.yami.shop.wx.po.JsapiPo;
+import com.yami.shop.wx.po.JsapiPayInfoPo;
 import com.yami.shop.wx.service.WxProviderService;
-import com.yami.shop.wx.utils.HttpUtils;
 import com.yami.shop.wx.utils.OrderUtils;
 import com.yami.shop.wx.utils.WechatPayValidator;
 import lombok.SneakyThrows;
@@ -45,13 +44,12 @@ import java.util.concurrent.locks.ReentrantLock;
 @Slf4j
 @Service
 public class WxProviderServiceImpl implements WxProviderService {
-    
+
     @Autowired
     private WechatPayServiceConfig wechatPayServiceConfig;
 
-    public Map<String, Object> subJsapi(JsapiPo po) {
+    public Map<String, Object> subJsapi(JsapiPayInfoPo po) {
         System.out.println("微信支付传入参数===========" + po);
-        String type = "sub_jsapi";
         Map<String, Object> params = new HashMap<>(8);
         params.put("sp_appid", wechatPayServiceConfig.getSpAppId());
         params.put("sp_mchid", wechatPayServiceConfig.getSpMchId());
@@ -76,15 +74,13 @@ public class WxProviderServiceImpl implements WxProviderService {
         params.put("scene_info", sceneInfoMap);
         String paramsStr = JSON.toJSONString(params);
         log.info("请求参数 ===> {}" + paramsStr);
-        String[] split = type.split("_");
-        String newType = split[split.length - 1];
-        String url = wechatPayServiceConfig.getBaseUrl().concat(CombinePayUrlEnum.PAY_TRANSACTIONS.getType().concat(newType));
+        String url = wechatPayServiceConfig.getBaseUrl().concat(CombinePayUrlEnum.PAY_TRANSACTIONS.getType().concat("jsapi"));
         log.info("请求地址 ===> {}" + url);
         String resStr = wechatHttpPost(url, paramsStr);
         Map<String, Object> resMap = JSONObject.parseObject(resStr, new TypeReference<Map<String, Object>>() {
         });
         Map<String, Object> signMap = paySignMsgApplet(resMap);
-        resMap.put("type", type);
+        resMap.put("type", "sub_jsapi");
         resMap.put("signMap", signMap);
         return resMap;
     }
@@ -168,26 +164,6 @@ public class WxProviderServiceImpl implements WxProviderService {
         return resMap;
     }
 
-
-    /**
-     * 关闭(取消)订单
-     *
-     * @param orderNo orderNo
-     */
-    public void closeOrder(String orderNo) {
-        // TODO 用于在客户下单后,不进行支付,取消订单的场景
-        log.info("根据订单号取消订单,订单号: {}", orderNo);
-        String url = String.format(CombinePayUrlEnum.CLOSE_ORDER_BY_NO.getType(), orderNo);
-        url = wechatPayServiceConfig.getBaseUrl().concat(url);
-        Map<String, String> params = new HashMap<>(2);
-        params.put("sp_mchid", wechatPayServiceConfig.getSpMchId());
-        params.put("sub_mchid", wechatPayServiceConfig.getSubAppId());
-        String paramsStr = JSON.toJSONString(params);
-        log.info("请求参数 ===> {}" + paramsStr);
-        String res = wechatHttpPost(url, paramsStr);
-        log.info(res);
-    }
-
     /**
      * 申请退款
      *
@@ -195,7 +171,7 @@ public class WxProviderServiceImpl implements WxProviderService {
      * @return orderNo
      */
     @Override
-    public String refundOrder(String orderNo,Integer refundMoney,Integer total) {
+    public String refundOrder(String orderNo, Integer refundMoney, Integer total) {
         log.info("根据订单号申请退款,订单号: {}", orderNo);
         String url = wechatPayServiceConfig.getBaseUrl().concat(CombinePayUrlEnum.DOMESTIC_REFUNDS.getType());
         Map<String, Object> params = new HashMap<>(2);
@@ -412,7 +388,7 @@ public class WxProviderServiceImpl implements WxProviderService {
     }
 
     private JSONObject getNotifyBodyJson(HttpServletRequest request) {
-        String body = HttpUtils.readData(request);
+        String body = WechatPayValidator.readData(request);
         log.info("===========微信回调参数===========\n" + body);
         log.info("微信回调参数:{}", body);
         JSONObject jsonObject = JSONObject.parseObject(body);
@@ -481,5 +457,4 @@ public class WxProviderServiceImpl implements WxProviderService {
         resMap.put("message", "成功");
         return resMap;
     }
-
 }

+ 0 - 41
yami-shop-wx/src/main/java/com/yami/shop/wx/utils/HttpUtils.java

@@ -1,41 +0,0 @@
-package com.yami.shop.wx.utils;
-
-import javax.servlet.http.HttpServletRequest;
-import java.io.BufferedReader;
-import java.io.IOException;
-
-/**
- * @author kaur
- **/
-public class HttpUtils {
-
-    /**
-     * 将通知参数转化为字符串
-     * @param request HttpServletRequest
-     * @return 字符串
-     */
-    public static String readData(HttpServletRequest request) {
-        BufferedReader br = null;
-        try {
-            StringBuilder result = new StringBuilder();
-            br = request.getReader();
-            for (String line; (line = br.readLine()) != null; ) {
-                if (result.length() > 0) {
-                    result.append("\n");
-                }
-                result.append(line);
-            }
-            return result.toString();
-        } catch (IOException e) {
-            throw new RuntimeException(e.getMessage());
-        } finally {
-            if (br != null) {
-                try {
-                    br.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-        }
-    }
-}

+ 32 - 0
yami-shop-wx/src/main/java/com/yami/shop/wx/utils/WechatPayValidator.java

@@ -9,6 +9,7 @@ import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.util.EntityUtils;
 
 import javax.servlet.http.HttpServletRequest;
+import java.io.BufferedReader;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.time.DateTimeException;
@@ -132,4 +133,35 @@ public class WechatPayValidator {
             throw new RuntimeException("回调参数,解密失败!");
         }
     }
+
+    /**
+     * 将通知参数转化为字符串
+     * @param request HttpServletRequest
+     * @return 字符串
+     */
+    public static String readData(HttpServletRequest request) {
+        BufferedReader br = null;
+        try {
+            StringBuilder result = new StringBuilder();
+            br = request.getReader();
+            for (String line; (line = br.readLine()) != null; ) {
+                if (result.length() > 0) {
+                    result.append("\n");
+                }
+                result.append(line);
+            }
+            return result.toString();
+        } catch (IOException e) {
+            throw new RuntimeException(e.getMessage());
+        } finally {
+            if (br != null) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
 }