栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微信APP支付Java代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微信APP支付Java代码

本文实例为大家分享了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);//会失败
 }

 
 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.append(" ").append(appid).append("");
 xml.append(" ").append(body).append("");
 xml.append(" ").append(mch_id).append("");
 xml.append(" ").append(nonce_str).append("");
 xml.append(" ").append(notify_url).append("");
 xml.append(" ").append(out_trade_no).append("");
 xml.append(" ").append(ip).append("");
 xml.append(" ").append(weixinMoney).append("");
 xml.append(" ").append(trade_type).append("");
 xml.append(" ").append(sign).append("");
 xml.append("");
 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;
 }

 
 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程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/149650.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号