最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

微信APP支付Java代碼

 更新時間:2016年07月07日 11:42:32   投稿:lijiao  
這篇文章主要為大家詳細介紹了微信APP支付Java代碼,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java微信APP支付代碼,供大家參考,具體內(nèi)容如下

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
 *   自己系統(tǒng)的訂單號
 * @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();
 }
}

以上就是本文的全部內(nèi)容,希望對大家學習java程序設計有所幫助。

相關文章

  • Java字符串轉成二進制碼的方法

    Java字符串轉成二進制碼的方法

    這篇文章主要為大家詳細介紹了Java字符串轉成二進制碼的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一
    2017-05-05
  • MyBatis動態(tài)SQL實現(xiàn)配置過程解析

    MyBatis動態(tài)SQL實現(xiàn)配置過程解析

    這篇文章主要介紹了MyBatis動態(tài)SQL實現(xiàn)配置過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-03-03
  • JDK8接口的默認與靜態(tài)方法-接口與抽象類的區(qū)別詳解

    JDK8接口的默認與靜態(tài)方法-接口與抽象類的區(qū)別詳解

    這篇文章主要介紹了JDK8接口的默認與靜態(tài)方法-接口與抽象類的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,,需要的朋友可以參考下
    2019-06-06
  • MyBatisPlus代碼生成器的使用示例

    MyBatisPlus代碼生成器的使用示例

    本文主要介紹了MyBatisPlus代碼生成器的使用示例,通過 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各個模塊的代碼,感興趣的可以了解一下
    2021-12-12
  • Springboot整合Gson報錯問題解決過程

    Springboot整合Gson報錯問題解決過程

    這篇文章主要介紹了Springboot整合Gson報錯問題解決過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • 使用Spring AOP實現(xiàn)MySQL數(shù)據(jù)庫讀寫分離案例分析(附demo)

    使用Spring AOP實現(xiàn)MySQL數(shù)據(jù)庫讀寫分離案例分析(附demo)

    分布式環(huán)境下數(shù)據(jù)庫的讀寫分離策略是解決數(shù)據(jù)庫讀寫性能瓶頸的一個關鍵解決方案,這篇文章主要介紹了使用Spring AOP實現(xiàn)MySQL數(shù)據(jù)庫讀寫分離案例分析(附demo),有興趣的可以了解一下。
    2017-01-01
  • Java導出Excel統(tǒng)計報表合并單元格的方法詳解

    Java導出Excel統(tǒng)計報表合并單元格的方法詳解

    我們在日常編程過程中,總是會碰見導出相關表格信息的需求,所以就讓我們一起來學習一下,這篇文章主要給大家介紹了關于Java導出Excel統(tǒng)計報表合并單元格的相關資料,需要的朋友可以參考下
    2021-10-10
  • Mybatis-Plus字段策略FieldStrategy的使用

    Mybatis-Plus字段策略FieldStrategy的使用

    本文主要介紹了Mybatis-Plus字段策略FieldStrategy的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • Java獲取調(diào)用當前方法的類名或方法名(棧堆信息)的四種方式舉例

    Java獲取調(diào)用當前方法的類名或方法名(棧堆信息)的四種方式舉例

    在Java編程中我們經(jīng)常需要在運行時獲取當前執(zhí)行的方法名稱,這在日志記錄、性能監(jiān)控、調(diào)試等方面非常有用,這篇文章主要給大家介紹了關于Java獲取調(diào)用當前方法的類名或方法名(棧堆信息)的四種方式,需要的朋友可以參考下
    2024-09-09
  • 如何在springBoot下搭建日志框架

    如何在springBoot下搭建日志框架

    這篇文章主要介紹了如何在springBoot下搭建日志框架,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06

最新評論

阳江市| 内江市| 闸北区| 哈尔滨市| 德清县| 美姑县| 共和县| 五台县| 汪清县| 黄骅市| 闸北区| 阿坝县| 普洱| 大渡口区| 桂林市| 绿春县| 麟游县| 年辖:市辖区| 焦作市| 图们市| 五大连池市| 通化县| 绥滨县| 清水县| 县级市| 余庆县| 濉溪县| 青岛市| 桃园县| 泗洪县| 时尚| 潢川县| 临夏市| 德清县| 当涂县| 龙泉市| 扬州市| 潮安县| 宁津县| 梧州市| 永嘉县|