From a540a342416302805adfb1599599bbbbaa191030 Mon Sep 17 00:00:00 2001 From: liujie <1793218484@qq.com> Date: 星期五, 08 八月 2025 23:42:13 +0800 Subject: [PATCH] 赛事模块 --- cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/PayMoneyUtil.java | 496 ++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 382 insertions(+), 114 deletions(-) diff --git a/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/PayMoneyUtil.java b/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/PayMoneyUtil.java index 7d89f83..75fbf3f 100644 --- a/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/PayMoneyUtil.java +++ b/cloud-server-management/src/main/java/com/dsh/guns/modular/system/util/PayMoneyUtil.java @@ -2,16 +2,12 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import com.alipay.api.AlipayApiException; -import com.alipay.api.AlipayClient; -import com.alipay.api.CertAlipayRequest; -import com.alipay.api.DefaultAlipayClient; -import com.alipay.api.domain.AlipayTradeAppPayModel; -import com.alipay.api.msg.AlipayMsgClient; -import com.alipay.api.msg.MsgHandler; import com.alipay.api.request.*; import com.alipay.api.response.*; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.dsh.course.util.UUIDUtil; +import com.dsh.guns.modular.system.controller.util.HttpClientUtil; +import com.dsh.guns.modular.system.controller.util.MD5AndKL; import com.dsh.guns.modular.system.model.OperatorAuthAlipay; import com.dsh.guns.modular.system.model.OperatorUser; import com.dsh.guns.modular.system.service.IOperatorAuthService; @@ -22,11 +18,18 @@ import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.stereotype.Component; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; import javax.servlet.http.HttpServletRequest; import java.io.*; import java.util.*; + +import static com.dsh.guns.modular.system.controller.util.MD5AndKL.byteArrayToHexString; + /** * 第三方支付工具类 */ @@ -40,15 +43,17 @@ private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmu8n/4yTHWbn7VOrNc9OsLtDL1bEQ8gC1dHkj8Wy5z0mkaOsjJRIG/28ze12M0V8jdCKuuDr5Z1OPKiqf+XO3ypguEh+mYUVMBM/cZodDFQfTY1TKLWjvQCuaqlA+QUTCK6f7T7stsgyQ1o9Jj0rXZDz6PM4QHSTzjrLIBaeqM5WIBvH+fy/X+QG5Utd+/UT0kc0JyvuKhZ65yVUd/C9VcwJJAPliRsAQNrqYterwAJ9zvw9tF11wj9W0XgJ8Ccu4x3gR1vrlLRJJo/OA97RmxPQ+5hSacWQZCUd1dwiBq+YCrKVHGTj14izRHXrLc0yBlRXo7tBOIqcy3IsvKVthQIDAQAB";//支付宝支付公钥 - private String appid = "";//微信appid + private String smid = "2088330203191220";//平台支付宝商户号... - private String appletsAppid = "";//微信小程序appid + private String appid = "wx41d32f362ba0f911";//微信appid - private String mchId = "";//微信商户号 + private String appSecret = "cf0ebf950f5926a69041a0e2bbe20f3e"; - private String key = "";//微信商户号 + private String mchId = "1501481761";//微信商户号 - private String callbackPath = "http://8.137.22.229:56666/account";//支付回调网关地址 + private String key = "6f5e0c2dcabfa9c27b5da5836a362fef";//微信商户号 + + private String callbackPath = "https://online.daowepark.com:443/account";//支付回调网关地址 private String app_cert_path = "C:/cert/alipay/user/app_cert_path.crt";//应用公钥证书路径 @@ -66,77 +71,340 @@ private IOperatorUserService operatorUserService; /** - * 处理直付通审核通过和拒绝消息 + * 添加分账接收方 + * @param number 商户号或者openId + * @param name 商户全称或者个人姓名 + * @return */ - public void getMessage() { - System.err.println("进入"); - final AlipayMsgClient alipayMsgClient = AlipayMsgClient.getInstance(aliAppid); + public ResultUtil addReceiver(String number,String name) throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("mch_id", mchId); + map.put("appid", appid); + String nonce_str = UUIDUtil.getRandomCode(16); + map.put("nonce_str", nonce_str); + // 将这个字符串使用json格式拼接起来 + Map<String, Object> body = new HashMap<>(); + body.put("type", "MERCHANT_ID"); + body.put("account", number); + body.put("name", name); + body.put("relation_type", "PARTNER"); + JSONObject jsonObject = new JSONObject(body); + String jsonString = jsonObject.toString(); + map.put("receiver",jsonString ); + String s = this.weixinSignature(map); + map.put("sign", s); + String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver"; + //设置请求头 + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_XML); + StringBuffer xmlString = new StringBuffer(); + Set<String> strings = map.keySet(); + String[] keys = {}; + keys = strings.toArray(keys); + Arrays.sort(keys); + xmlString.append("<xml>"); + for (int l = 0; l < keys.length; l++) { + xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">"); + } + xmlString.append("</xml>"); + + Map<String, String> map1 = null; + String body1 = null; try { - alipayMsgClient.setConnector(serverHost); + body1 = HttpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData(); } catch (Exception e) { e.printStackTrace(); } - alipayMsgClient.setSecurityConfig(signType, appPrivateKey, alipay_public_key); + //将结果xml解析成map + body1 = body1.replaceAll("<!\\[CDATA\\[", ""); + body1 = body1.replaceAll("]]>", ""); try { - alipayMsgClient.connect(); - } catch (InterruptedException e) { + map1 = this.xmlToMap(body1, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (DocumentException e) { e.printStackTrace(); } - alipayMsgClient.setMessageHandler( new MsgHandler() { - /** - * 客户端接收到消息后回调此方法 - * @param msgApi 接收到的消息的消息api名 - * @param msgId 接收到的消息的消息id - * @param bizContent 接收到的消息的内容,json格式 - */ - public void onMessage (String msgApi, String msgId, String bizContent) { - // 直付通进件审核通过 - if (StringUtils.equals(msgApi,"ant.merchant.expand.indirect.zft.passed")){ - // 修改运营商状态 将返回的商户号填入运营商 - System.out.println( "receive message. msgApi:" + msgApi + " msgId:" + msgId + " bizContent:" + bizContent); - JSONObject json = JSONObject.parseObject(bizContent); - // 商户号 - String smid = json.getString("smid"); - // 审核备注信息 - String memo = json.getString("memo"); - // 订单id - String orderId = json.getString("order_id"); - // 通过订单id查询 - OperatorAuthAlipay auth = operatorAuthService.getOne(new QueryWrapper<OperatorAuthAlipay>() - .eq("orderNo",orderId)); - auth.setSmid(smid); - auth.setAuditState(2); - auth.setRefuseReason(memo); - operatorAuthService.updateById(auth); - Integer operatorId = auth.getOperatorId(); - OperatorUser operatorId1 = operatorUserService.getOne(new QueryWrapper<OperatorUser>() - .eq("operatorId", operatorId)); - operatorId1.setAlipayNum(smid); - operatorId1.setAlipayAudit(2); - operatorUserService.updateById(operatorId1); + String return_code = map1.get("return_code"); + if ("SUCCESS".equals(return_code)) { + String result_code = map1.get("result_code"); + if ("SUCCESS".equals(result_code)) { + return ResultUtil.success(); + } else { +// System.err.println(map1.get("err_code_des")); + return ResultUtil.error(map1.get("err_code_des")); + } + } else { +// System.err.println(map1.get("return_msg") + appid + "----" + mchId); + return ResultUtil.error(map1.get("return_msg"), new JSONObject()); + } + } + /** + * 发起分账 + * @param order 微信订单号 + * @return + */ + public ResultUtil fenzhang(String order) throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("mch_id", mchId); + map.put("appid", appid); + String nonce_str = UUIDUtil.getRandomCode(16); + map.put("nonce_str", nonce_str); + map.put("transaction_id", order); + // 将这个字符串使用json格式拼接起来 + Map<String, Object> body = new HashMap<>(); + body.put("type", "MERCHANT_ID"); + // todo 分账接收方 计算分账金额 + body.put("account", "1233"); + body.put("amount", "0"); + body.put("description", "订单分账"); + JSONObject jsonObject = new JSONObject(body); + String jsonString = jsonObject.toString(); + map.put("receiver",jsonString); + String s = this.weixinSignature(map); + map.put("sign", s); + String url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing"; + //设置请求头 + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_XML); + StringBuffer xmlString = new StringBuffer(); + Set<String> strings = map.keySet(); + String[] keys = {}; + keys = strings.toArray(keys); + Arrays.sort(keys); + xmlString.append("<xml>"); + for (int l = 0; l < keys.length; l++) { + xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">"); + } + xmlString.append("</xml>"); + + Map<String, String> map1 = null; + String body1 = null; + try { + body1 = HttpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData(); + } catch (Exception e) { + e.printStackTrace(); + } + //将结果xml解析成map + body1 = body1.replaceAll("<!\\[CDATA\\[", ""); + body1 = body1.replaceAll("]]>", ""); + try { + map1 = this.xmlToMap(body1, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (DocumentException e) { + e.printStackTrace(); + } + String return_code = map1.get("return_code"); + if ("SUCCESS".equals(return_code)) { + String result_code = map1.get("result_code"); + if ("SUCCESS".equals(result_code)) { + return ResultUtil.success(); + } else { +// System.err.println(map1.get("err_code_des")); + return ResultUtil.error(map1.get("err_code_des")); + } + } else { +// System.err.println(map1.get("return_msg") + appid + "----" + mchId); + return ResultUtil.error(map1.get("return_msg"), new JSONObject()); + } + } + /** + * 删除分账接收方 + * @param number 商户号或者openId + * @param name 商户全称或者个人姓名 + * @return + */ + public ResultUtil deleteReceiver(String number,String name) throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("mch_id", mchId); + map.put("appid", appid); + String nonce_str = UUIDUtil.getRandomCode(16); + map.put("nonce_str", nonce_str); + // 将这个字符串使用json格式拼接起来 + Map<String, Object> body = new HashMap<>(); + body.put("type", "MERCHANT_ID"); + body.put("account", number); + body.put("name", name); + body.put("relation_type", "PARTNER"); + JSONObject jsonObject = new JSONObject(body); + String jsonString = jsonObject.toString(); + map.put("receiver",jsonString ); + String s = this.weixinSignature(map); + map.put("sign", s); + String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver"; + //设置请求头 + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_XML); + StringBuffer xmlString = new StringBuffer(); + Set<String> strings = map.keySet(); + String[] keys = {}; + keys = strings.toArray(keys); + Arrays.sort(keys); + xmlString.append("<xml>"); + for (int l = 0; l < keys.length; l++) { + xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">"); + } + xmlString.append("</xml>"); + + Map<String, String> map1 = null; + String body1 = null; + try { + body1 = HttpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData(); + } catch (Exception e) { + e.printStackTrace(); + } + //将结果xml解析成map + body1 = body1.replaceAll("<!\\[CDATA\\[", ""); + body1 = body1.replaceAll("]]>", ""); + try { + map1 = this.xmlToMap(body1, "UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (DocumentException e) { + e.printStackTrace(); + } + String return_code = map1.get("return_code"); + if ("SUCCESS".equals(return_code)) { + String result_code = map1.get("result_code"); + if ("SUCCESS".equals(result_code)) { + return ResultUtil.success(); + } else { +// System.err.println(map1.get("err_code_des")); + return ResultUtil.error(map1.get("err_code_des")); + } + } else { +// System.err.println(map1.get("return_msg") + appid + "----" + mchId); + return ResultUtil.error(map1.get("return_msg"), new JSONObject()); + } + } + /** + * 微信下单的签名算法 + * + * @param map + * @return + */ + private String weixinSignature(Map<String, Object> map) { + try { + Set<Map.Entry<String, Object>> entries = map.entrySet(); + List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(entries); + // 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序) + Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() { + public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) { + return (o1.getKey()).toString().compareTo(o2.getKey()); } - // 直付通进件审核失败 - if (StringUtils.equals(msgApi,"ant.merchant.expand.indirect.zft.rejected")){ - JSONObject json = JSONObject.parseObject(bizContent); - // 商户号 - String orderId = json.getString("order_id"); - // 官方拒绝理由 - String reason = json.getString("reason"); - // 通过订单id查询 - OperatorAuthAlipay auth = operatorAuthService.getOne(new QueryWrapper<OperatorAuthAlipay>() - .eq("orderNo",orderId)); - auth.setAuditState(3); - auth.setRefuseReason(reason); - operatorAuthService.updateById(auth); - Integer operatorId = auth.getOperatorId(); - OperatorUser operatorId1 = operatorUserService.getOne(new QueryWrapper<OperatorUser>() - .eq("operatorId", operatorId)); - operatorId1.setAlipayAudit(3); - operatorUserService.updateById(operatorId1); + }); + // 构造签名键值对的格式 + StringBuilder sb = new StringBuilder(); + for (Map.Entry<String, Object> item : infoIds) { + if (item.getKey() != null || item.getKey() != "") { + String key = item.getKey(); + Object val = item.getValue(); + if (!(val == "" || val == null)) { + sb.append(key + "=" + val + "&"); + } } } - }); + sb.append("key=" + key); + String sign = sha256_HMAC(sb.toString(), key).toUpperCase(); + return sign; + } catch (Exception e) { + e.printStackTrace(); + } + return null; } + /** + * sha256_HMAC加密 + * @param message 消息 + * @param secret 秘钥 + * @return 加密后字符串 + */ + public String sha256_HMAC(String message, String secret) { + String hash = ""; + try { + Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); + SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256"); + sha256_HMAC.init(secret_key); + byte[] bytes = sha256_HMAC.doFinal(message.getBytes()); + hash = byteArrayToHexString(bytes); + } catch (Exception e) { + System.out.println("Error HmacSHA256 ===========" + e.getMessage()); + } + return hash; + } + + /** + * 处理直付通审核通过和拒绝消息 + */ +// public void getMessage() { +// System.err.println("进入"); +// final AlipayMsgClient alipayMsgClient = AlipayMsgClient.getInstance(aliAppid); +// try { +// alipayMsgClient.setConnector(serverHost); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// alipayMsgClient.setSecurityConfig(signType, appPrivateKey, alipay_public_key); +// try { +// alipayMsgClient.connect(); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// alipayMsgClient.setMessageHandler( new MsgHandler() { +// /** +// * 客户端接收到消息后回调此方法 +// * @param msgApi 接收到的消息的消息api名 +// * @param msgId 接收到的消息的消息id +// * @param bizContent 接收到的消息的内容,json格式 +// */ +// public void onMessage (String msgApi, String msgId, String bizContent) { +// // 直付通进件审核通过 +// if (StringUtils.equals(msgApi,"ant.merchant.expand.indirect.zft.passed")){ +// // 修改运营商状态 将返回的商户号填入运营商 +// System.out.println( "receive message. msgApi:" + msgApi + " msgId:" + msgId + " bizContent:" + bizContent); +// JSONObject json = JSONObject.parseObject(bizContent); +// // 商户号 +// String smid = json.getString("smid"); +// // 审核备注信息 +// String memo = json.getString("memo"); +// // 订单id +// String orderId = json.getString("order_id"); +// // 通过订单id查询 +// OperatorAuthAlipay auth = operatorAuthService.getOne(new QueryWrapper<OperatorAuthAlipay>() +// .eq("orderNo",orderId)); +// auth.setSmid(smid); +// auth.setAuditState(2); +// auth.setRefuseReason(memo); +// operatorAuthService.updateById(auth); +// Integer operatorId = auth.getOperatorId(); +// OperatorUser operatorId1 = operatorUserService.getOne(new QueryWrapper<OperatorUser>() +// .eq("operatorId", operatorId)); +// operatorId1.setAlipayNum(smid); +// operatorId1.setAlipayAudit(2); +// operatorUserService.updateById(operatorId1); +// } +// // 直付通进件审核失败 +// if (StringUtils.equals(msgApi,"ant.merchant.expand.indirect.zft.rejected")){ +// JSONObject json = JSONObject.parseObject(bizContent); +// // 商户号 +// String orderId = json.getString("order_id"); +// // 官方拒绝理由 +// String reason = json.getString("reason"); +// // 通过订单id查询 +// OperatorAuthAlipay auth = operatorAuthService.getOne(new QueryWrapper<OperatorAuthAlipay>() +// .eq("orderNo",orderId)); +// auth.setAuditState(3); +// auth.setRefuseReason(reason); +// operatorAuthService.updateById(auth); +// Integer operatorId = auth.getOperatorId(); +// OperatorUser operatorId1 = operatorUserService.getOne(new QueryWrapper<OperatorUser>() +// .eq("operatorId", operatorId)); +// operatorId1.setAlipayAudit(3); +// operatorUserService.updateById(operatorId1); +// } +// } +// }); +// } /** * 支付宝支付 */ @@ -193,19 +461,19 @@ // } - //实例化客户端 - AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipay_public_key, "RSA2"); - //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay - AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest(); - //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。 - AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); - model.setBody(body);//对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。 - model.setSubject(subject);//商品的标题/交易标题/订单标题/订单关键字等。 - model.setOutTradeNo(outTradeNo);//商户网站唯一订单号 - model.setTimeoutExpress("30m"); - model.setTotalAmount(amount);//付款金额 - model.setProductCode("QUICK_MSECURITY_PAY"); - model.setPassbackParams(passbackParams);//自定义参数 +// //实例化客户端 +// AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipay_public_key, "RSA2"); +// //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay +// AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest(); +// //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。 +// AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); +// model.setBody(body);//对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。 +// model.setSubject(subject);//商品的标题/交易标题/订单标题/订单关键字等。 +// model.setOutTradeNo(outTradeNo);//商户网站唯一订单号 +// model.setTimeoutExpress("30m"); +// model.setTotalAmount(amount);//付款金额 +// model.setProductCode("QUICK_MSECURITY_PAY"); +// model.setPassbackParams(passbackParams);//自定义参数 //分账 @@ -246,42 +514,42 @@ // model.setRoyaltyInfo(royaltyInfo); // System.err.println("=================="+royaltyInfo); // - request.setBizModel(model); - request.setNotifyUrl(callbackPath + notifyUrl); - try { - //这里和普通的接口调用不同,使用的是sdkExecute - AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request); - Map<String, String> map = new HashMap<>(); - map.put("orderString", response.getBody()); - map.put("returnId", passbackParams); - System.out.println(map);//就是orderString 可以直接给客户端请求,无需再做处理。 - return ResultUtil.success(map); - } catch (AlipayApiException e) { - e.printStackTrace(); - } +// request.setBizModel(model); +// request.setNotifyUrl(callbackPath + notifyUrl); +// try { +// //这里和普通的接口调用不同,使用的是sdkExecute +// AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request); +// Map<String, String> map = new HashMap<>(); +// map.put("orderString", response.getBody()); +// map.put("returnId", passbackParams); +// System.out.println(map);//就是orderString 可以直接给客户端请求,无需再做处理。 +// return ResultUtil.success(map); +// } catch (AlipayApiException e) { +// e.printStackTrace(); +// } return null; } - /** - * 支付宝查询订单支付状态 - * - * @param out_trade_no - * @return - * @throws Exception - */ - public AlipayTradeQueryResponse queryALIOrder(String out_trade_no) throws Exception { - AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipay_public_key, "RSA2"); - AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); - request.setBizContent("{" + - " \"out_trade_no\":\"" + out_trade_no + "\"" + - "}"); - AlipayTradeQueryResponse response = alipayClient.execute(request); - return response; - - } +// /** +// * 支付宝查询订单支付状态 +// * +// * @param out_trade_no +// * @return +// * @throws Exception +// */ +// public AlipayTradeQueryResponse queryALIOrder(String out_trade_no) throws Exception { +// AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipay_public_key, "RSA2"); +// AlipayTradeQueryRequest request = new AlipayTradeQueryRequest(); +// request.setBizContent("{" + +// " \"out_trade_no\":\"" + out_trade_no + "\"" + +// "}"); +// AlipayTradeQueryResponse response = alipayClient.execute(request); +// return response; +// +// } -- Gitblit v1.7.1