提交 | 用户 | age
|
b7ab9f
|
1 |
package com.hx.mp.util; |
E |
2 |
|
|
3 |
import net.sf.json.JSONObject; |
|
4 |
import org.apache.http.client.methods.CloseableHttpResponse; |
|
5 |
import org.apache.http.util.EntityUtils; |
|
6 |
|
|
7 |
import java.util.Map; |
|
8 |
import java.util.SortedMap; |
|
9 |
import java.util.TreeMap; |
|
10 |
import java.util.UUID; |
|
11 |
|
|
12 |
/** |
|
13 |
* 微信小程序支付相关工具类 |
|
14 |
*/ |
|
15 |
public class WxMpPayUtil { |
|
16 |
|
|
17 |
/**小程序红包发送链接*/ |
|
18 |
private static final String URL_SEND_RED_PACK = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendminiprogramhb"; |
|
19 |
|
|
20 |
/** |
|
21 |
* 发送小程序红包 |
|
22 |
* @param orderNo 订单号 |
|
23 |
* @param mchId 商户号 |
|
24 |
* @param mchKey 商户密钥 |
|
25 |
* @param appId 小程序appId |
|
26 |
* @param sendName 发送者名称 |
|
27 |
* @param openId 接收者openId |
|
28 |
* @param payFee 红包金额,以分为单位 |
|
29 |
* @param wishing 祝福语 |
|
30 |
* @param actName 活动名称 |
|
31 |
* @param remark 备注 |
|
32 |
* @param certPath 支付证书路径 |
|
33 |
* @return |
|
34 |
*/ |
|
35 |
public static JSONObject sendMiniProgramReadPack(String orderNo, String mchId, String mchKey, String appId, String sendName, String openId, |
|
36 |
int payFee, String wishing, String actName, String remark, String certPath) |
|
37 |
{ |
|
38 |
SortedMap<Object, Object> parameters = new TreeMap<Object, Object>(); |
|
39 |
parameters.put("nonce_str", UUID.randomUUID().toString().substring(0, 30)); |
|
40 |
parameters.put("mch_billno", orderNo);//商户订单号 |
|
41 |
parameters.put("mchid", mchId);//商户号 |
|
42 |
parameters.put("wxappid", appId);//公众账号appId |
|
43 |
parameters.put("send_name", sendName);//红包发送者名称 |
|
44 |
parameters.put("re_openid", openId);//用户openId |
|
45 |
parameters.put("total_amount", payFee);//红包总金额,以分为单位 |
|
46 |
parameters.put("total_num", 1);//红包数量,固定为1 |
|
47 |
parameters.put("wishing", wishing);//红包祝福语,128字符 |
|
48 |
parameters.put("act_name", actName);//活动名称 |
|
49 |
parameters.put("remark", remark);//备注 |
|
50 |
parameters.put("notify_way", "MINI_PROGRAM_JSAPI");//通知用户形式,固定 |
|
51 |
|
|
52 |
String sign = WXSignUtils.createSign("UTF-8", parameters, mchKey); |
|
53 |
|
|
54 |
parameters.put("sign", sign); |
|
55 |
String xmlInfo = HttpXmlUtils.transferXml(parameters); |
|
56 |
|
|
57 |
JSONObject returnObj = new JSONObject(); |
|
58 |
|
|
59 |
try { |
|
60 |
CloseableHttpResponse response = HttpUtil.Post(URL_SEND_RED_PACK, xmlInfo, true, certPath, mchId); |
|
61 |
String transfersXml = EntityUtils.toString(response.getEntity(), "utf-8"); |
|
62 |
Map<String, String> transferMap = HttpXmlUtils.parseRefundXml(transfersXml); |
|
63 |
boolean bl = false; |
|
64 |
if (transferMap.size() > 0) { |
|
65 |
if (transferMap.get("return_code").equals("SUCCESS")) { |
|
66 |
// 通讯成功 |
|
67 |
if (transferMap.get("result_code").equals("SUCCESS")) { |
|
68 |
// 成功需要进行的逻辑操作 |
|
69 |
returnObj.put("status", "suc"); |
|
70 |
} else { |
|
71 |
bl = true; |
|
72 |
returnObj.put("status", "fail"); |
|
73 |
returnObj.put("errMsg", transferMap.get("err_code") + "|" + transferMap.get("err_code_des")); |
|
74 |
} |
|
75 |
} else { |
|
76 |
bl = true; |
|
77 |
// 通讯不成功 |
|
78 |
returnObj.put("status", "fail"); |
|
79 |
returnObj.put("errMsg", transferMap.get("return_msg")); |
|
80 |
} |
|
81 |
} else { |
|
82 |
bl = true; |
|
83 |
returnObj.put("status", "fail"); |
|
84 |
returnObj.put("errMsg", "返回为空"); |
|
85 |
} |
|
86 |
if (bl) { |
|
87 |
System.out.println("企业付款失败:" + transfersXml); |
|
88 |
} |
|
89 |
} catch (Exception e) { |
|
90 |
e.printStackTrace(); |
|
91 |
returnObj.put("status", "fail"); |
|
92 |
returnObj.put("errMsg", e.getMessage()); |
|
93 |
} |
|
94 |
|
|
95 |
return returnObj; |
|
96 |
} |
|
97 |
|
|
98 |
} |