java微信公众号企业付款开发

时间:2022-06-18 04:19:56

本文为大家分享了java微信公众号企业付款的开发代码,供大家参考,具体内容如下

详情参照微信开发者文档 企业付款文档

java微信公众号企业付款开发

java代码 定义所传递的参数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@requestmapping(value = "zhifu", method = requestmethod.get)
public @responsebody string getweixinopenid(string code,
  httpservletrequest request)
{
 // 订单号 自定义 生成32位uuid
 string partner_trade_no = uuidgenerator.getuuid();
 // 随机数
 string nonce_str = uuidgenerator.getuuid();
 // 转账金额(分为单位)1-200
 int jine = 100;
 // 企业付款信息
 string desc = "转账";
 // ip地址
 string spbill_create_ip = "xx.xx.xx";
 // re_user_name
 string re_user_name = "xx";
 
 string check_name = checkname.no_check.tostring();
 string zfpath = "d:/apiclient_cert.p12";
 try
 {
  // 获取openid
  string openid = wechatutil.getbyopenid(appid, secret, code);
  // 付款
  boolean flag = wechatutil.enterprisepayment(openid, appid, mchid,
    nonce_str, partner_trade_no, re_user_name, jine, desc,
    spbill_create_ip, check_name, key, zfpath);
  // 成功
  if (flag)
  {
   return "success";
  }
 
 }
 catch (exception e)
 {
  system.err.println(e.getstacktrace());
 }
 return "fail";
}

获取关注本公众号用户唯一标示  获取openid

java代码 获取openid 静态方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
  * 获取openid
  *
  * @description
  * @param appid
  * @param secret
  * @param code
  * @return
  * @author shaomiao
  */
 public static string getbyopenid(string appid, string secret, string code)
 {
  string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
    + appid + "&secret=" + secret + "&code=" + code
    + "&grant_type=authorization_code";
  string jsonstring = wechatutil.getjsonstring(url);
  jsonobject json1 = jsonobject.parseobject(jsonstring);
  string openid = json1.get("openid").tostring();
  return openid;
 
 }

企业付款的调用公共方法

java代码
post提交 xml参数
解析回调的xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
 * 企业付款
 *
 * @description
 * @param openid
 * @param appid
 * @param mchid 商户id
 * @param nonce_str
 * @param partner_trade_no
 * @param re_user_name
 * @param jine
 * @param desc
 * @param spbill_create_ip
 * @param check_name
 * @return
 * @author jobs
 * @throws ioexception
 * @throws clientprotocolexception
 */
public static boolean enterprisepayment(string openid, string appid,
  string mchid, string nonce_str, string partner_trade_no,
  string re_user_name, int jine, string desc, string spbill_create_ip,
  string check_name, string key, string zfpath) throws exception
{
 boolean getsuccess = true;
 if (null != openid)
 {
  // zf
  map<string, string> params_map = new linkedhashmap<string, string>();
  stringbuffer param = new stringbuffer();
  // appid
  param.append("mch_appid=" + appid);
  // 商户id
  param.append("&mchid=" + mchid);
  // 随机字符串
  // param.append("&nonce_str="
  // + zifwutil.string2md5(new date().gettime() + ""));
  param.append("&nonce_str=" + nonce_str);
  // 订单号自定义
  param.append("&partner_trade_no=" + partner_trade_no);
 
  param.append("&openid=" + openid);
  // 校验用户姓名选项
  /**
   * no_check:不校验真实姓名
   * force_check:强校验真实姓名(未实名认证的用户会校验失败,无法转账)
   * option_check:针对已实名认证的用户才校验真实姓名(未实名认证用户不校验,可以转账成功)
   */
  param.append("&check_name=" + check_name);
  // 收款用户姓名
  param.append("&re_user_name=" + re_user_name);
  // 金额
  param.append("&amount=" + jine);
  // 企业付款描述信息
  param.append("&desc=" + desc);
  // ip地址
  param.append("&spbill_create_ip=" + spbill_create_ip);
 
  string[] params = param.tostring().split("&");
  arrays.sort(params);
  param = new stringbuffer();
  for (string p : params)
  {
   string[] value = p.split("=");
   params_map.put(value[0], value[1]);
   param.append(value[0] + "=" + value[1] + "&");
  }
 
  // 签名最后
  string sign = zifwutil.string2md5(param.tostring() + "key=" + key)
    .touppercase();
  params_map.put("sign", sign);
  string reqstr = zifwutil.toxml(params_map);
  // zhengshu
  closeablehttpclient httpclient = certificatevalidation(zfpath,
    mchid);
 
  httppost httppost = new httppost(
    "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers");
  stringentity myentity = new stringentity(reqstr, "utf-8");
  httppost.setentity(myentity);
  system.out.println("executing request" + httppost.getrequestline());
 
  closeablehttpresponse response = httpclient.execute(httppost);
  system.out.println(response.getstatusline());
 
  httpentity resentity = response.getentity();
  inputstreamreader reader = new inputstreamreader(
    resentity.getcontent(), "utf-8");
  char[] buff = new char[1024];
  int length = 0;
  stringbuffer strhuxml = new stringbuffer();
  while ((length = reader.read(buff)) != -1)
  {
   strhuxml.append(new string(buff, 0, length));
   system.out.println(new string(buff, 0, length));
  }
  // httpclient.close();
  httpclient.getconnectionmanager().shutdown();
 
  // string ret = zifwutil.post(
  // "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers",
  // reqstr);
 
  // 解析传过来的xml
  document document = documenthelper.parsetext(strhuxml.tostring());
  // 得到xml根元素
  element root = document.getrootelement();
  // 得到根元素的所有子节点
  list<element> elementlist = root.elements();
  string errors = "";
  for (element e : elementlist)
  {
 
   // result_code业务
   if ("return_code".equals(e.getname())
     && !"success".equals(e.gettext()))
   {
    getsuccess = false;
   }
   if ("result_code".equals(e.getname())
     && !"success".equals(e.gettext()))
   {
    getsuccess = false;
   }
  }
 
 }
 return getsuccess;
}

微信签名验证证书

验证证书公共方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * 验证证书公共方法
 *
 * @description
 * @param zfpath 证书的路径
 * @param mchid 商户id
 * @return
 * @throws exception
 * @author jobs
 */
// shanghuid
// 验证证书
@suppresswarnings("deprecation")
public static closeablehttpclient certificatevalidation(string zfpath,
  string mchid) throws exception
{
 // 指定读取证书格式为pkcs12
 keystore keystore = keystore.getinstance("pkcs12");
 // 证书地址
 fileinputstream instream = new fileinputstream(new file(zfpath));
 try
 {
  keystore.load(instream, mchid.tochararray());
 }
 finally
 {
  instream.close();
 }
 
 // trust own ca and all self-signed certs
 sslcontext sslcontext = sslcontexts.custom()
   .loadkeymaterial(keystore, mchid.tochararray()).build();
 // allow tlsv1 protocol only
 sslconnectionsocketfactory sslsf = new sslconnectionsocketfactory(
   sslcontext, new string[] { "tlsv1" }, null,
   sslconnectionsocketfactory.browser_compatible_hostname_verifier);
 closeablehttpclient httpclient = httpclients.custom()
   .setsslsocketfactory(sslsf).build();
 return httpclient;
}

微信公共方法  字符串转xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
  * 微信支付拼接xml
  *
  * @param params
  * @return
  */
 public static string toxml(map<string, string> params)
 {
  string xml = "<xml>";
  for (string key : params.keyset())
  {
   if ("body".equals(key) || "attach".equals(key)
     || "sign".equals(key))
   {
    xml += "<" + key + "><![cdata[" + params.get(key) + "]]></"
      + key + ">";
   }
   else
   {
    xml += "<" + key + ">" + params.get(key) + "</" + key + ">";
   }
  }
  xml += "</xml>";
  return xml;
 }

微信公共方法  字符串md5

加密
用来加密签名

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/***
  * md5加码 生成32位md5码
  */
 public static string string2md5(string instr)
 {
  stringbuffer buf = new stringbuffer();
  try
  {
   messagedigest md = messagedigest.getinstance("md5");
   md.update(instr.getbytes("utf-8"));
   byte b[] = md.digest();
 
   int i;
 
   for (int offset = 0; offset < b.length; offset++)
   {
    i = b[offset];
    if (i < 0)
     i += 256;
    if (i < 16)
     buf.append("0");
    buf.append(integer.tohexstring(i));
   }
  }
  catch (exception e)
  {
   e.printstacktrace();
  }
  return buf.tostring();
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/shaomiaojava/article/details/50562550