本文為大家分享了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