Browse Source

微信支付h5支付生成app支付地址

xufeng 2 years ago
parent
commit
28c156cf49

+ 14 - 1
game-module/game-common/src/main/java/com/zanxiang/common/constant/Constants.java

@@ -132,7 +132,7 @@ public class Constants {
      */
     public static final String ROCK_ASYNC_MP_FANS = "ROCK_ASYNC_MP_FANS_";
 
-public static final BigDecimal NUM_100 = new BigDecimal(100);
+    public static final BigDecimal NUM_100 = new BigDecimal(100);
 
     public static final boolean ENABLE_NO = false;
     public static final boolean ENABLE_YES = true;
@@ -203,4 +203,17 @@ public static final BigDecimal NUM_100 = new BigDecimal(100);
      * 已登录用户列表
      */
     public static final String REDIS_LOGIN_USER_LIST = "Gateway:LoginUserList:";
+
+
+    /**
+     * 成功
+     */
+    public static final String SUCCESS = "success";
+
+    /**
+     * 失败
+     */
+    public static final String FAIL = "fail";
+
+
 }

+ 33 - 29
game-module/game-sdk/src/main/java/com/zanxiang/sdk/common/util/WxPayUtil.java

@@ -1,8 +1,11 @@
 package com.zanxiang.sdk.common.util;
 
+import com.zanxiang.common.constant.Constants;
 import com.zanxiang.sdk.domain.bo.WxPayConfigBO;
 
+import java.util.Map;
 import java.util.SortedMap;
+import java.util.TreeMap;
 
 /**
  * 相关配置参数
@@ -39,35 +42,36 @@ public class WxPayUtil {
      * @param urlCode
      * @return
      */
-//    public String shortUrl(String urlCode) {
-//        try {
-//            String key = wxPay.getApiKey();
-//            SortedMap<Object, Object> packageParams = new TreeMap<>();
-//            commonParams(packageParams);
-//            packageParams.put("long_url", urlCode);// URL链接
-//            String sign = PayCommonUtil.createSign("UTF-8", packageParams, key);
-//            packageParams.put("sign", sign);// 签名
-//            String requestXML = PayCommonUtil.getRequestXml(packageParams);
-//            String resXml = HttpUtil.postData(WxPayUrl.SHORT_URL, requestXML);
-//            Map map = XMLUtil.doXMLParse(resXml);
-//            String returnCode = (String) map.get("return_code");
-//            if (Constants.SUCCESS.equalsIgnoreCase(returnCode)) {
-//                String resultCode = (String) map.get("return_code");
-//                if (Constants.SUCCESS.equalsIgnoreCase(resultCode)) {
-//                    urlCode = (String) map.get("short_url");
-//                    return urlCode;
-//                } else {
-//                    return Constants.FAIL;
-//                }
-//            } else {
-//                return Constants.FAIL;
-//            }
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//            return Constants.FAIL;
-//        }
-//    }
-//    public String doRefund(String url, String data) throws Exception {
+    public String shortUrl(String urlCode) {
+        try {
+            String key = wxPay.getApiKey();
+            SortedMap<Object, Object> packageParams = new TreeMap<>();
+            commonParams(packageParams);
+            packageParams.put("long_url", urlCode);// URL链接
+            String sign = PayCommonUtil.createSign("UTF-8", packageParams, key);
+            packageParams.put("sign", sign);// 签名
+            String requestXML = PayCommonUtil.getRequestXml(packageParams);
+            String resXml = HttpUtil.postData(WxPayUrl.SHORT_URL, requestXML);
+            Map map = XMLUtil.doXMLParse(resXml);
+            String returnCode = (String) map.get("return_code");
+            if (Constants.SUCCESS.equalsIgnoreCase(returnCode)) {
+                String resultCode = (String) map.get("return_code");
+                if (Constants.SUCCESS.equalsIgnoreCase(resultCode)) {
+                    urlCode = (String) map.get("short_url");
+                    return urlCode;
+                } else {
+                    return Constants.FAIL;
+                }
+            } else {
+                return Constants.FAIL;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return Constants.FAIL;
+        }
+    }
+
+    //    public String doRefund(String url, String data) throws Exception {
 //        /**
 //         * 注意PKCS12证书 是从微信商户平台-》账户设置-》 API安全 中下载的
 //         */

+ 2 - 2
game-module/game-sdk/src/main/java/com/zanxiang/sdk/controller/PayController.java

@@ -47,7 +47,7 @@ public class PayController {
 
     @ApiOperation(value = "支付参数生成")
     @PostMapping(value = "/create")
-    public ResultMap create(@Validated @RequestBody ProductPayParam product, @ValidLogin UserData userData) {
+    public ResultMap create(@Validated @RequestBody ProductPayParam product, @ValidLogin UserData userData, HttpServletRequest request) {
         String userId = String.valueOf(userData.getUserId());
 //        String userId = "1";
         String cpId = "1";
@@ -76,7 +76,7 @@ public class PayController {
         bo.setPayDevice(userData.getDeviceType());
         bo.setDeviceSystem(userData.getDeviceSystem());
         //todo : 工具类做了修改
-        bo.setSpbillCreateIp(IpUtils.getHostIp());
+        bo.setSpbillCreateIp(IpUtils.getRealIp(request));
         bo.setOutTradeNo(product.getOrderId());
         bo.setPayWay(product.getPayWay());
         return payService.payCreate(bo);

+ 21 - 13
game-module/game-sdk/src/main/java/com/zanxiang/sdk/service/Impl/pay/WxpayServiceImpl.java

@@ -1,6 +1,7 @@
 package com.zanxiang.sdk.service.Impl.pay;
 
 import com.alibaba.fastjson.JSONObject;
+import com.zanxiang.common.constant.Constants;
 import com.zanxiang.common.domain.ResultMap;
 import com.zanxiang.common.enums.ResEnum;
 import com.zanxiang.common.utils.RandomStringUtil;
@@ -21,10 +22,7 @@ import weixin.popular.api.SnsAPI;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.SortedMap;
-import java.util.TreeMap;
+import java.util.*;
 
 /**
  * @author xufeng
@@ -76,8 +74,8 @@ public class WxpayServiceImpl extends PayService implements OrderPayService {
                 return this.mobile(product, "NATIVE");
             case 2:
             case 3:
-//                return this.mobile(product, "MWEB");
-                return this.pc(product);
+                return this.mobile(product, "MWEB");
+//                return this.pc(product);
             case 4:
                 return this.mp(product);
             default:
@@ -131,7 +129,7 @@ public class WxpayServiceImpl extends PayService implements OrderPayService {
             // 处理业务开始
             // ------------------------------
             String resXml;
-            if ("SUCCESS".equals(packageParams.get("result_code"))) {
+            if (Constants.SUCCESS.equalsIgnoreCase(packageParams.get("result_code").toString())) {
                 // 这里是支付成功
                 String orderNo = (String) packageParams.get("out_trade_no");
                 logger.info("微信订单号{}付款成功", orderNo);
@@ -240,7 +238,7 @@ public class WxpayServiceImpl extends PayService implements OrderPayService {
             totalFee = wxPayUtil.subZeroAndDot(this.totalFee);
             packageParams.put("total_fee", totalFee);// 总金额
             packageParams.put("spbill_create_ip", product.getSpbillCreateIp());// 发起人IP地址
-            packageParams.put("notify_url", notifyUrl);// 回调地址
+            packageParams.put("notify_url", "https://localhost");// 回调地址 notifyUrl
             packageParams.put("trade_type", trade_type);// 交易类型
             packageParams.put("attach", JSONObject.toJSONString(this.attach));
 
@@ -262,16 +260,26 @@ public class WxpayServiceImpl extends PayService implements OrderPayService {
             Map map = XMLUtil.doXMLParse(resXml);
             logger.info("订单号:{} 微信响应 map:{}", product.getOutTradeNo(), map);
             String returnCode = (String) map.get("return_code");
-            if ("SUCCESS".equals(returnCode)) {
+            if (Constants.SUCCESS.equalsIgnoreCase(returnCode)) {
                 String resultCode = (String) map.get("result_code");
-                if ("SUCCESS".equals(resultCode)) {
+                if (Constants.SUCCESS.equalsIgnoreCase(resultCode)) {
                     logger.info("订单号:{}生成微信支付码成功", product.getOutTradeNo());
                     String urlCode = trade_type.equals("MWEB") ? (String) map.get("mweb_url") : (String) map.get("code_url");
                     String imgName = this.outTradeNo + ".png";
                     String imgPath = filePath + imgName;
-                    ZxingUtil.createQRCodeImage(urlCode, imgPath);
-                    System.out.println(imgPath);
-                    return ResultMap.ok(product.getPayDevice(), urlCode);
+                    HashMap<String, String> result = new HashMap<>();
+                    if (trade_type.equals("MWEB")) {
+                        try {
+                            String shortUrl = wxPayUtil.shortUrl(urlCode);
+                            if (!Constants.FAIL.equalsIgnoreCase(shortUrl)) {
+                                result.put("AppLink", shortUrl);
+                            }
+                        } catch (Exception e) {
+                            logger.error("订单号:{} 生成微信app支付链接失败:{}", product.getOutTradeNo(), e);
+                        }
+                    }
+                    result.put("fromData", urlCode);
+                    return ResultMap.ok(product.getPayDevice(), result);
                 } else {
                     String errCodeDes = (String) map.get("err_code_des");
                     logger.error("订单号:{}生成微信支付码(系统)失败:{}", product.getOutTradeNo(), errCodeDes);