From 61bbd595ee2bc3c67b40878894dcc07174c6eea6 Mon Sep 17 00:00:00 2001 From: 无关风月 <443237572@qq.com> Date: 星期二, 24 九月 2024 10:15:30 +0800 Subject: [PATCH] 玩湃微信商户分账 --- cloud-server-other/src/main/java/com/dsh/other/util/PayMoneyUtil.java | 233 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 218 insertions(+), 15 deletions(-) diff --git a/cloud-server-other/src/main/java/com/dsh/other/util/PayMoneyUtil.java b/cloud-server-other/src/main/java/com/dsh/other/util/PayMoneyUtil.java index f436220..a7b08c9 100644 --- a/cloud-server-other/src/main/java/com/dsh/other/util/PayMoneyUtil.java +++ b/cloud-server-other/src/main/java/com/dsh/other/util/PayMoneyUtil.java @@ -6,10 +6,9 @@ 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.domain.ExtendParams; -import com.alipay.api.domain.RoyaltyDetailInfos; -import com.alipay.api.domain.RoyaltyInfo; +import com.alipay.api.domain.*; +import com.alipay.api.msg.AlipayMsgClient; +import com.alipay.api.msg.MsgHandler; import com.alipay.api.request.*; import com.alipay.api.response.*; import lombok.Synchronized; @@ -22,6 +21,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; import javax.annotation.Resource; import javax.crypto.BadPaddingException; @@ -53,15 +53,15 @@ 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 appid = "wx41d32f362ba0f911";//微信appid - private String appletsAppid = "";//微信小程序appid + private String appSecret = "cf0ebf950f5926a69041a0e2bbe20f3e"; - private String mchId = "";//微信商户号 + private String mchId = "1501481761";//微信商户号 - private String key = "";//微信商户号 + private String key = "6f5e0c2dcabfa9c27b5da5836a362fef";//微信商户号 - private String callbackPath = "http://8.137.22.229:56666/other";//支付回调网关地址 + private String callbackPath = "https://online.daowepark.com:443/other";//支付回调网关地址 private String app_cert_path = "C:/cert/alipay/user/app_cert_path.crt";//应用公钥证书路径 @@ -69,9 +69,13 @@ private String alipay_root_cert_path = "C:/cert/alipay/user/alipay_root_cert_path.crt";//支付宝CA根证书文件路径 - private String certPath = "C:\\cert\\1523106371_20211206_cert\\apiclient_cert.p12";//微信证书 + private String certPath = "/usr/playpai/cert/weixin/apiclient_cert.p12";//微信证书 + + + public ResultUtil confirm(String smid,String code, String outTradeNo, String amount) { + AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, @@ -95,6 +99,47 @@ " }," + " \"extend_params\":{" + " \"royalty_freeze\":\"true\"" + + " }" + + "}"); + AlipayTradeSettleConfirmResponse response = null; + try { + response = alipayClient.execute(request); + } catch (AlipayApiException e) { + e.printStackTrace(); + } + if(response.isSuccess()){ + System.out.println("调用成功"); + return ResultUtil.success(); + } else { + System.out.println("调用失败"); + return ResultUtil.error("出现问题啦"); + } + } + // 属于平台的运营商 因为无需分账不冻结资金 + public ResultUtil confirm1(String smid,String code, String outTradeNo, String amount) { + AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", + aliAppid, + appPrivateKey, + "json", + "GBK", + alipay_public_key, + "RSA2"); + AlipayTradeSettleConfirmRequest request = new AlipayTradeSettleConfirmRequest(); + request.setBizContent("{" + + " \"out_request_no\":\""+code+"\"," + + " \"trade_no\":\""+outTradeNo+"\"," + + " \"settle_info\":{" + + " \"settle_detail_infos\":[" + + " {" + + " \"trans_in_type\":\"defaultSettle\"," + + " \"settle_entity_id\":\""+smid+"\"," + + " \"settle_entity_type\":\"SecondMerchant\"," + + " \"amount\":"+amount+"," + + " }" + + " ]" + + " }," + + " \"extend_params\":{" + + " \"royalty_freeze\":\"false\"" + " }" + "}"); AlipayTradeSettleConfirmResponse response = null; @@ -180,7 +225,21 @@ model.setTotalAmount(amount);//付款金额 model.setProductCode("QUICK_MSECURITY_PAY"); model.setPassbackParams(passbackParams);//自定义参数 - model.setSellerId(smid); + SettleInfo settleInfo = new SettleInfo(); + settleInfo.setSettlePeriodTime("3d"); + SettleDetailInfo settleDetailInfo = new SettleDetailInfo(); + settleDetailInfo.setTransInType("defaultSettle"); + settleDetailInfo.setAmount(amount); + List<SettleDetailInfo> settleDetailInfos = new ArrayList<>(); + settleDetailInfos.add(settleDetailInfo); + settleInfo.setSettleDetailInfos(settleDetailInfos); + model.setSettleInfo(settleInfo); + SubMerchant subMerchant = new SubMerchant(); + subMerchant.setMerchantId(smid); + model.setSubMerchant(subMerchant); + ExtendParams extendParams = new ExtendParams(); + extendParams.setRoyaltyFreeze("true");// 冻结资金 用于后续分账处理 + model.setExtendParams(extendParams); request.setBizModel(model); request.setNotifyUrl(callbackPath + notifyUrl); @@ -268,7 +327,6 @@ e.printStackTrace(); } JSONObject alipay_trade_precreate_response = JSON.parseObject(response.getBody()).getJSONObject("alipay_trade_precreate_response"); - System.err.print(alipay_trade_precreate_response.getString("qr_code")); return ResultUtil.success(alipay_trade_precreate_response.getString("qr_code")); } @@ -383,10 +441,20 @@ } String nonce_str = UUIDUtil.getRandomCode(16); Map<String, Object> map = new HashMap<>(); - map.put("appid", "APP".equals(tradeType) ? appid : appletsAppid); + map.put("appid", appid); map.put("mch_id", mchId); map.put("nonce_str", nonce_str); - map.put("body", body); + String temp = ""; + if (body.split("-").length>1){ + temp = body.split("-")[1]; + map.put("body", body.split("-")[0]); + }else{ + map.put("body", body); + } + if (StringUtils.hasLength(temp) && temp.equals("1")){ + // 添加分账标识 + map.put("profit_sharing", "Y"); + } map.put("attach", attach);//存储订单id map.put("out_trade_no", out_trade_no);//存储的订单code map.put("total_fee", i); @@ -464,7 +532,7 @@ String s1 = this.weixinSignature(map3); map3.put("sign", s1); System.err.println(map3); - map3.put("siteBookingId", attach); + map3.put("siteBooking", attach); return ResultUtil.success(map3); } return null; @@ -477,7 +545,142 @@ return ResultUtil.error(map1.get("return_msg"), new JSONObject()); } } + /** + * 发起分账 + * @param order 微信订单号 + * @return + */ + public ResultUtil fenzhang(String order,BigDecimal amount,String merchantNumber,String nonce_str) throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("mch_id", mchId); + map.put("appid", appid); + map.put("nonce_str", nonce_str); + map.put("transaction_id", order); + // 将这个字符串使用json格式拼接起来 + Map<String, Object> body = new HashMap<>(); + body.put("type", "MERCHANT_ID"); + body.put("account", merchantNumber); + body.put("amount", amount); + 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.pushHttpsRequsetXml(url, xmlString.toString(), new HashMap<>(), mchId, certPath, "PKCS12"); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("分账请求"+body1); + //将结果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 order 微信订单号 + * @return + */ + public ResultUtil fenzhangRefund(String order,BigDecimal amount,String merchantNumber, + String nonce_str, + String fenzhangRefundNo) throws Exception { + Map<String, Object> map = new HashMap<>(); + map.put("mch_id", mchId); + map.put("appid", appid); + map.put("nonce_str", nonce_str); + map.put("order_id", order); + map.put("out_return_no", fenzhangRefundNo); + map.put("return_account_type", "MERCHANT_ID"); + map.put("return_account", merchantNumber); + map.put("return_amount", amount); + map.put("description", "用户退款"); + String s = this.weixinSignature(map); + map.put("sign", s); + String url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharingreturn"; + //设置请求头 + 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.pushHttpsRequsetXml(url, xmlString.toString(), new HashMap<>(), mchId, certPath, "PKCS12"); + } catch (Exception e) { + e.printStackTrace(); + } + System.err.println("分账金额回退"+body1); + //将结果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(map1.get("return_no")); + } 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()); + } + } /** * 微信支付成功后的回调处理 -- Gitblit v1.7.1