欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信APP支付Java代碼

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

本文實(shí)例為大家分享了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";//自己設(shè)置
 private static final int mch_id = 1252196606;//自己設(shè)置
 private static final String api_key = "b8b9c2bbe92d57cc38fde49745056167";//自己設(shè)置
 private static final String notify_url = "http://www.xxx.com/weixin_notify_url.jsp";//自己設(shè)置
 private static final String trade_type = "APP";//

 public static void main(String[] args) {
 postToWeChat("5455545", "test", 0.01);
 postToWeChat("5455545", "中文", 0.01);//會(huì)失敗
 }

 /**
 * 提交到微信
 * 
 * @param out_trade_no
 *   自己系統(tǒng)的訂單號(hào)
 * @param body
 *   標(biāo)題
 * @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加密簽名加密類自己解決就不放上來(lái)了
 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;
 }

 /**
 * 表示生成字符串的長(zhǎng)度
 * 
 * @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();
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • idea代碼模板設(shè)置方式

    idea代碼模板設(shè)置方式

    這篇文章主要介紹了idea代碼模板設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 100行java寫的微信跳一跳輔助程序

    100行java寫的微信跳一跳輔助程序

    本篇文章給大家分享了用java寫的一個(gè)微信跳一跳輔助腳本程序,有興趣的朋友參考學(xué)習(xí)下。
    2018-01-01
  • JAVA字符串類型switch的底層原理詳析

    JAVA字符串類型switch的底層原理詳析

    這篇文章主要給大家介紹了關(guān)于JAVA字符串類型switch的底層原理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用JAVA具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • SpringBoot 整合jdbc和mybatis的方法

    SpringBoot 整合jdbc和mybatis的方法

    該文章主要為記錄如何在SpringBoot項(xiàng)目中整合JDBC和MyBatis,在整合中我會(huì)使用簡(jiǎn)單的用法和測(cè)試用例,感興趣的朋友跟隨小編一起看看吧
    2019-11-11
  • 淺談一下JVM垃圾回收算法

    淺談一下JVM垃圾回收算法

    這篇文章主要介紹了一下JVM垃圾回收算法,Java有著自己一套的內(nèi)存管理機(jī)制,不需要開發(fā)者去手動(dòng)釋放內(nèi)存,開發(fā)者只需要寫好代碼即可,運(yùn)行過(guò)程中產(chǎn)生的垃圾都由JVM回收,需要的朋友可以參考下
    2023-04-04
  • SpringBoot配置Actuator組件,實(shí)現(xiàn)系統(tǒng)監(jiān)控

    SpringBoot配置Actuator組件,實(shí)現(xiàn)系統(tǒng)監(jiān)控

    在生產(chǎn)環(huán)境中,需要實(shí)時(shí)或定期監(jiān)控服務(wù)的可用性。Spring Boot的actuator(健康監(jiān)控)功能提供了很多監(jiān)控所需的接口,可以對(duì)應(yīng)用系統(tǒng)進(jìn)行配置查看、相關(guān)功能統(tǒng)計(jì)等。
    2021-06-06
  • Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能對(duì)比

    Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能對(duì)比

    本文主要介紹了Fluent Mybatis,原生Mybatis,Mybatis Plus三者功能對(duì)比,分享給大家,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • java中使用xls格式化xml的實(shí)例

    java中使用xls格式化xml的實(shí)例

    這篇文章主要介紹了java中調(diào)用xls格式化xml的實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • java web返回中文亂碼問(wèn)題及解決

    java web返回中文亂碼問(wèn)題及解決

    這篇文章主要介紹了java web返回中文亂碼問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Jmeter參數(shù)化實(shí)現(xiàn)方法及應(yīng)用實(shí)例

    Jmeter參數(shù)化實(shí)現(xiàn)方法及應(yīng)用實(shí)例

    這篇文章主要介紹了Jmeter參數(shù)化實(shí)現(xiàn)方法及應(yīng)用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08

最新評(píng)論