当前位置: 移动技术网 > IT编程>开发语言>Java > 微信APP支付Java代码

微信APP支付Java代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了java微信app支付代码,供大家参考,具体内容如下

import java.io.ioexception;
import java.io.unsupportedencodingexception;

import java.util.random;

import org.apache.http.parseexception;
import org.apache.http.client.clientprotocolexception;
import org.apache.http.client.methods.httppost;
import org.apache.http.entity.stringentity;
import org.apache.http.impl.client.defaulthttpclient;
import org.apache.http.util.entityutils;
import org.json.jsonarray;
import org.json.jsonml;
import org.json.jsonobject;

public class test {
 private static final string appid = "wx0378bf81abfe3d26";//自己设置
 private static final int mch_id = 1252196606;//自己设置
 private static final string api_key = "b8b9c2bbe92d57cc38fde49745056167";//自己设置
 private static final string notify_url = "http://www.xxx.com/weixin_notify_url.jsp";//自己设置
 private static final string trade_type = "app";//

 public static void main(string[] args) {
 posttowechat("5455545", "test", 0.01);
 posttowechat("5455545", "中文", 0.01);//会失败
 }

 /**
 * 提交到微信
 * 
 * @param out_trade_no
 *   自己系统的订单号
 * @param body
 *   标题
 * @param money
 *   金额
 * @return
 */
 private static jsonobject posttowechat(string out_trade_no, string body, double money) {
 stringbuilder xml = new stringbuilder();
 string nonce_str = getrandomstring(32);
 string ip = "127.0.0.1";// 客户端ip自己处理
 jsonobject jso = new jsonobject();
 string prepay_id = "", sign = "";
 try {
 string weixinmoney = new java.text.decimalformat("#").format(money * 100);// 微信是以分为单位的所以要乘以100
 xml.append("appid=").append(appid).append("&body=").append(new string(body.getbytes("utf-8"), "utf-8"));
 xml.append("&mch_id=").append(mch_id).append("&nonce_str=").append(nonce_str);
 xml.append("¬ify_url=").append(notify_url).append("&out_trade_no=").append(out_trade_no).append("&spbill_create_ip=").append(ip);
 xml.append("&total_fee=").append(weixinmoney).append("&trade_type=").append(trade_type).append("&key=").append(api_key);
 sign = new util().md5purity(xml.tostring()).touppercase();// md5加密签名加密类自己解决就不放上来了
 xml.delete(0, xml.length());
 xml.append("<xml>");
 xml.append(" <appid>").append(appid).append("</appid>");
 xml.append(" <body>").append(body).append("</body>");
 xml.append(" <mch_id>").append(mch_id).append("</mch_id>");
 xml.append(" <nonce_str>").append(nonce_str).append("</nonce_str>");
 xml.append(" <notify_url>").append(notify_url).append("</notify_url>");
 xml.append(" <out_trade_no>").append(out_trade_no).append("</out_trade_no>");
 xml.append(" <spbill_create_ip>").append(ip).append("</spbill_create_ip>");
 xml.append(" <total_fee>").append(weixinmoney).append("</total_fee>");
 xml.append(" <trade_type>").append(trade_type).append("</trade_type>");
 xml.append(" <sign>").append(sign).append("</sign>");
 xml.append("</xml>");
 httppost post = new httppost("https://api.mch.weixin.qq.com/pay/unifiedorder");
 stringentity entity = new stringentity(xml.tostring(), "utf-8");
 entity.setcontentencoding("utf-8");
 entity.setcontenttype("text/xml");
 post.setentity(entity);
 jsonarray childnodes = jsonml.tojsonobject(entityutils.tostring(new defaulthttpclient().execute(post).getentity(), "utf-8")).getjsonarray(
  "childnodes");
 system.out.println(childnodes);
 int len = childnodes.length() - 1;
 for (int i = len; i > -1; i--) {
 jsonobject js = childnodes.getjsonobject(i);
 if (js.get("tagname").equals("prepay_id")) {
  prepay_id = js.getjsonarray("childnodes").getstring(0);
  break;
 }
 }
 } catch (unsupportedencodingexception e) {
 e.printstacktrace();
 } catch (parseexception e) {
 e.printstacktrace();
 } catch (clientprotocolexception e) {
 e.printstacktrace();
 } catch (ioexception e) {
 e.printstacktrace();
 }
 jso.put("sign", sign);
 jso.put("appid", appid);
 jso.put("noncestr", nonce_str);
 jso.put("package", "sign=wxpay");
 jso.put("partnerid", mch_id);
 jso.put("prepayid", prepay_id);
 jso.put("timestamp", system.currenttimemillis());
 return jso;
 }

 /**
 * 表示生成字符串的长度
 * 
 * @param length
 * @return
 */
 private static string getrandomstring(int length) {
 string base = "abcdefghijklmnopqrstuvwxyz0123456789";
 random random = new random();
 stringbuffer sb = new stringbuffer();
 for (int i = 0; i < length; i++) {
 int number = random.nextint(base.length());
 sb.append(base.charat(number));
 }
 return sb.tostring();
 }
}

以上就是本文的全部内容,希望对大家学习java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网