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 小程序appId
|
* @param sendName 发送者名称
|
* @param openId 接收者openId
|
* @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;
|
}
|
|
}
|