一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java微信公眾號企業(yè)付款開發(fā)

java微信公眾號企業(yè)付款開發(fā)

2021-06-02 14:06shaomiaojava Java教程

這篇文章主要為大家詳細介紹了java微信公眾號企業(yè)付款開發(fā),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文為大家分享了java微信公眾號企業(yè)付款的開發(fā)代碼,供大家參考,具體內容如下

詳情參照微信開發(fā)者文檔 企業(yè)付款文檔

java微信公眾號企業(yè)付款開發(fā)

java代碼 定義所傳遞的參數(shù)

?
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();
 // 隨機數(shù)
 string nonce_str = uuidgenerator.getuuid();
 // 轉賬金額(分為單位)1-200
 int jine = 100;
 // 企業(yè)付款信息
 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 靜態(tài)方法

?
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;
 
 }

企業(yè)付款的調用公共方法

java代碼
post提交 xml參數(shù)
解析回調的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
/**
 * 企業(yè)付款
 *
 * @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);
  // 企業(yè)付款描述信息
  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();
  // 得到根元素的所有子節(jié)點
  list<element> elementlist = root.elements();
  string errors = "";
  for (element e : elementlist)
  {
 
   // result_code業(yè)務
   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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 特黄特黄一级片 | 91麻豆制片厂 | 欧美一二区视频 | 冰雪奇缘1完整版免费观看 变形金刚第一部 | japan孕妇孕交 | 国产精品久久久久久久久久久搜索 | 视频在线观看高清免费 | caoporn超碰最新地址进入 | 日韩欧美天堂 | 五月天国产精品 | 19+韩国女主播激情vip视频在线 | 亚洲精品国产综合久久一线 | 国产人妖xxxxx免费看 | 四虎在线最新地址公告 | 国产精品九九久久一区hh | 毛片 ftp | 国产精品毛片高清在线完整版 | 91香蕉视频在线观看 | 99视频在线国产 | 猛男壮男受bl爽哭了高h | 国产欧美综合精品一区二区 | 亚洲精品成人a | 国产拍拍拍 | 美女靠逼动漫 | 国产情侣自拍网 | 青青草99热这里都是精品 | 美女张开大腿让男人桶 | 免费国产高清精品一区在线 | 校园刺激全黄H全肉细节文 校草让我脱了内裤给全班看 | 色综合图区 | 国产精品福利在线观看免费不卡 | 四虎精品在线视频 | 国精品午夜dy8888狼人 | 国产精品3p视频 | 欧美丝袜foot job| 9久热这里只有精品免费 | 大学生情侣在线 | 午夜人妻理论片天堂影院 | ts视频在线观看 | 亚洲精品午夜级久久久久 | 欧美肥b |