From b7ab9f397ef2322fa0e79ab51fc16cbd77399835 Mon Sep 17 00:00:00 2001 From: E1ED922C1E9526DD63272D7EC5C6CB77 <E1ED922C1E9526DD63272D7EC5C6CB77@i-search.com.cn> Date: 星期五, 16 四月 2021 11:53:32 +0800 Subject: [PATCH] 添加小程序红包接口 --- src/main/java/com/hx/mp/util/WxMpPayUtil.java | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 98 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/hx/mp/util/WxMpPayUtil.java b/src/main/java/com/hx/mp/util/WxMpPayUtil.java new file mode 100644 index 0000000..736c589 --- /dev/null +++ b/src/main/java/com/hx/mp/util/WxMpPayUtil.java @@ -0,0 +1,98 @@ +package com.hx.mp.util; + +import net.sf.json.JSONObject; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.util.EntityUtils; + +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.UUID; + +/** + * 寰俊灏忕▼搴忔敮浠樼浉鍏冲伐鍏风被 + */ +public class WxMpPayUtil { + + /**灏忕▼搴忕孩鍖呭彂閫侀摼鎺�*/ + private static final String URL_SEND_RED_PACK = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendminiprogramhb"; + + /** + * 鍙戦�佸皬绋嬪簭绾㈠寘 + * @param orderNo 璁㈠崟鍙� + * @param mchId 鍟嗘埛鍙� + * @param mchKey 鍟嗘埛瀵嗛挜 + * @param appId 灏忕▼搴廰ppId + * @param sendName 鍙戦�佽�呭悕绉� + * @param openId 鎺ユ敹鑰卭penId + * @param payFee 绾㈠寘閲戦锛屼互鍒嗕负鍗曚綅 + * @param wishing 绁濈璇� + * @param actName 娲诲姩鍚嶇О + * @param remark 澶囨敞 + * @param certPath 鏀粯璇佷功璺緞 + * @return + */ + public static JSONObject sendMiniProgramReadPack(String orderNo, String mchId, String mchKey, String appId, String sendName, String openId, + int payFee, String wishing, String actName, String remark, String certPath) + { + SortedMap<Object, Object> parameters = new TreeMap<Object, Object>(); + parameters.put("nonce_str", UUID.randomUUID().toString().substring(0, 30)); + parameters.put("mch_billno", orderNo);//鍟嗘埛璁㈠崟鍙� + parameters.put("mchid", mchId);//鍟嗘埛鍙� + parameters.put("wxappid", appId);//鍏紬璐﹀彿appId + parameters.put("send_name", sendName);//绾㈠寘鍙戦�佽�呭悕绉� + parameters.put("re_openid", openId);//鐢ㄦ埛openId + parameters.put("total_amount", payFee);//绾㈠寘鎬婚噾棰濓紝浠ュ垎涓哄崟浣� + parameters.put("total_num", 1);//绾㈠寘鏁伴噺锛屽浐瀹氫负1 + parameters.put("wishing", wishing);//绾㈠寘绁濈璇紝128瀛楃 + parameters.put("act_name", actName);//娲诲姩鍚嶇О + parameters.put("remark", remark);//澶囨敞 + parameters.put("notify_way", "MINI_PROGRAM_JSAPI");//閫氱煡鐢ㄦ埛褰㈠紡锛屽浐瀹� + + String sign = WXSignUtils.createSign("UTF-8", parameters, mchKey); + + parameters.put("sign", sign); + String xmlInfo = HttpXmlUtils.transferXml(parameters); + + JSONObject returnObj = new JSONObject(); + + try { + CloseableHttpResponse response = HttpUtil.Post(URL_SEND_RED_PACK, xmlInfo, true, certPath, mchId); + String transfersXml = EntityUtils.toString(response.getEntity(), "utf-8"); + Map<String, String> transferMap = HttpXmlUtils.parseRefundXml(transfersXml); + boolean bl = false; + if (transferMap.size() > 0) { + if (transferMap.get("return_code").equals("SUCCESS")) { + // 閫氳鎴愬姛 + if (transferMap.get("result_code").equals("SUCCESS")) { + // 鎴愬姛闇�瑕佽繘琛岀殑閫昏緫鎿嶄綔 + returnObj.put("status", "suc"); + } else { + bl = true; + returnObj.put("status", "fail"); + returnObj.put("errMsg", transferMap.get("err_code") + "|" + transferMap.get("err_code_des")); + } + } else { + bl = true; + // 閫氳涓嶆垚鍔� + returnObj.put("status", "fail"); + returnObj.put("errMsg", transferMap.get("return_msg")); + } + } else { + bl = true; + returnObj.put("status", "fail"); + returnObj.put("errMsg", "杩斿洖涓虹┖"); + } + if (bl) { + System.out.println("浼佷笟浠樻澶辫触锛�" + transfersXml); + } + } catch (Exception e) { + e.printStackTrace(); + returnObj.put("status", "fail"); + returnObj.put("errMsg", e.getMessage()); + } + + return returnObj; + } + +} -- Gitblit v1.8.0