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

微信公眾號支付(二)實現(xiàn)統(tǒng)一下單接口

 更新時間:2015年09月08日 22:52:48   投稿:mrr  
本篇文章主要給大家介紹調(diào)用微信公眾支付的統(tǒng)一下單API,通過參數(shù)封裝為xml格式并發(fā)送到微信給的接口地址就可以獲得返回內(nèi)容,需要的朋友可以參考下本文

上一篇已經(jīng)獲取到了用戶的OpenId

這篇主要是調(diào)用微信公眾支付的統(tǒng)一下單API

API地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1

看文檔,主要流程就是把20個左右的參數(shù)封裝為XML格式發(fā)送到微信給的接口地址,然后就可以獲取到返回的內(nèi)容了,如果成功里面就有支付所需要的預支付ID

請求參數(shù)就不解釋了。

其中,隨機字符串:我用的是UUID去中劃線

 public static String create_nonce_str() {
 return UUID.randomUUID().toString().replace("-","");
 }

商戶訂單號:每個訂單號只能使用一次,所以用的是系統(tǒng)的訂單號加的時間戳。

總金額:不能為

通知地址:微信支付成功或失敗回調(diào)給系統(tǒng)的地址

簽名:

import java.io.Serializable;
 public class PayInfo implements Serializable{
 private static final long serialVersionUID = L;
 private String appid;
 private String mch_id;
 private String device_info;
 private String nonce_str;
 private String sign;
 private String body;
 private String attach;
 private String out_trade_no;
 private int total_fee;
 private String spbill_create_ip;
 private String notify_url;
 private String trade_type;
 private String openid;
 //下面是get,set方法 
 }


 /**
 * 創(chuàng)建統(tǒng)一下單的xml的java對象
 * @param bizOrder 系統(tǒng)中的業(yè)務單號
 * @param ip 用戶的ip地址
 * @param openId 用戶的openId
 * @return
 */
 public PayInfo createPayInfo(BizOrder bizOrder,String ip,String openId) {
  PayInfo payInfo = new PayInfo();
  payInfo.setAppid(Constants.appid);
  payInfo.setDevice_info("WEB");
  payInfo.setMch_id(Constants.mch_id);
  payInfo.setNonce_str(CommonUtil.create_nonce_str().replace("-", ""));
  payInfo.setBody("這里是某某白米飯的body");
  payInfo.setAttach(bizOrder.getId());
  payInfo.setOut_trade_no(bizOrder.getOrderCode().concat("A").concat(DateFormatUtils.format(new Date(), "MMddHHmmss")));
  payInfo.setTotal_fee((int)bizOrder.getFeeAmount());
  payInfo.setSpbill_create_ip(ip);
  payInfo.setNotify_url(Constants.notify_url);
  payInfo.setTrade_type("JSAPI");
  payInfo.setOpenid(openId);
  return payInfo;
 }

獲取簽名:

/**
 * 獲取簽名
 * @param payInfo
 * @return
 * @throws Exception
 */
 public String getSign(PayInfo payInfo) throws Exception {
  String signTemp = "appid="+payInfo.getAppid()
   +"&attach="+payInfo.getAttach()
   +"&body="+payInfo.getBody()
   +"&device_info="+payInfo.getDevice_info()
   +"&mch_id="+payInfo.getMch_id()
   +"&nonce_str="+payInfo.getNonce_str()
   +"&notify_url="+payInfo.getNotify_url()
   +"&openid="+payInfo.getOpenid()
   +"&out_trade_no="+payInfo.getOut_trade_no()
   +"&spbill_create_ip="+payInfo.getSpbill_create_ip()
   +"&total_fee="+payInfo.getTotal_fee()
   +"&trade_type="+payInfo.getTrade_type()
   +"&key="+Constants.key; //這個key注意
 MessageDigest md = MessageDigest.getInstance("MD");
 md.reset();
 md.update(signTemp.getBytes("UTF-"));
 String sign = CommonUtil.byteToStr(md.digest()).toUpperCase();
 return sign;
 }

注意:上面的Constants.key取值在商戶號API安全的API密鑰中。

一些工具方法:獲取ip地址,將字節(jié)數(shù)組轉(zhuǎn)換為十六進制字符串,將字節(jié)轉(zhuǎn)換為十六進制字符串

 /**
 * 將字節(jié)數(shù)組轉(zhuǎn)換為十六進制字符串
 * 
 * @param byteArray
 * @return
 */
 public static String byteToStr(byte[] byteArray) {
  String strDigest = "";
  for (int i = ; i < byteArray.length; i++) {
  strDigest += byteToHexStr(byteArray[i]);
  }
  return strDigest;
 }
 /**
 * 將字節(jié)轉(zhuǎn)換為十六進制字符串
 * 
 * @param btyes
 * @return
 */
 public static String byteToHexStr(byte bytes) {
  char[] Digit = { '', '', '', '', '', '', '', '', '', '', 'A', 'B', 'C', 'D', 'E', 'F' };
  char[] tempArr = new char[];
  tempArr[] = Digit[(bytes >>> ) & XF];
  tempArr[] = Digit[bytes & XF];
  String s = new String(tempArr);
  return s;
 }
 /**
 * 獲取ip地址
 * @param request
 * @return
 */
 public static String getIpAddr(HttpServletRequest request) { 
  InetAddress addr = null; 
  try { 
  addr = InetAddress.getLocalHost(); 
  } catch (UnknownHostException e) { 
  return request.getRemoteAddr(); 
  } 
  byte[] ipAddr = addr.getAddress(); 
  String ipAddrStr = ""; 
  for (int i = ; i < ipAddr.length; i++) { 
  if (i > ) { 
   ipAddrStr += "."; 
  } 
  ipAddrStr += ipAddr[i] & xFF; 
  } 
  return ipAddrStr; 
 } 

這樣就獲取了簽名,把簽名與PayInfo中的其他數(shù)據(jù)轉(zhuǎn)成XML格式,當做參數(shù)傳遞給統(tǒng)一下單地址。

 PayInfo pi = pu.createPayInfo(bo,"...","");
 String sign = pu.getSign(pi);
 pi.setSign(sign);

對象轉(zhuǎn)XML

 /**
 * 擴展xstream使其支持CDATA
 */
 private static XStream xstream = new XStream(new XppDriver() {
  public HierarchicalStreamWriter createWriter(Writer out) {
  return new PrettyPrintWriter(out) {
   //增加CDATA標記
   boolean cdata = true;
   @SuppressWarnings("rawtypes")
   public void startNode(String name, Class clazz) {
   super.startNode(name, clazz);
   }
   protected void writeText(QuickWriter writer, String text) {
   if (cdata) {
    writer.write("<![CDATA[");
    writer.write(text);
    writer.write("]]>");
   } else {
    writer.write(text);
   }
   }
  };
  }
 });
 public static String payInfoToXML(PayInfo pi) {
  xstream.alias("xml", pi.getClass());
  return xstream.toXML(pi);
 }

xml轉(zhuǎn)Map

 @SuppressWarnings("unchecked")
 public static Map<String, String> parseXml(String xml) throws Exception {
  Map<String, String> map = new HashMap<String, String>();
  Document document = DocumentHelper.parseText(xml);
  Element root = document.getRootElement();
  List<Element> elementList = root.elements();
  for (Element e : elementList)
  map.put(e.getName(), e.getText());
  return map;
 }

下面就是調(diào)用統(tǒng)一下單的URL了

log.info(MessageUtil.payInfoToXML(pi).replace("__", "_"));
   Map<String, String> map = CommonUtil.httpsRequestToXML("https://api.mch.weixin.qq.com/pay/unifiedorder", "POST", MessageUtil.payInfoToXML(pi).replace("__", "_").replace("<![CDATA[", "").replace("]]>", ""));
 log.info(map);

 public static Map<String, String> httpsRequestToXML(String requestUrl, String requestMethod, String outputStr) {
  Map<String, String> result = new HashMap<>();
  try {
  StringBuffer buffer = httpsRequest(requestUrl, requestMethod, outputStr);
  result = MessageUtil.parseXml(buffer.toString());
  } catch (ConnectException ce) {
  log.error("連接超時:"+ce.getMessage());
  } catch (Exception e) {
  log.error("https請求異常:"+ece.getMessage());
  }
  return result;
 }

httpsRequest()這個方法在第一篇中

上面獲取到的Map如果成功的話,里面就會有

String return_code = map.get("return_code");
 if(StringUtils.isNotBlank(return_code) && return_code.equals("SUCCESS")){
   String return_msg = map.get("return_msg");
 if(StringUtils.isNotBlank(return_msg) && !return_msg.equals("OK")) {
   return "統(tǒng)一下單錯誤!";
 }
 }else{
   return "統(tǒng)一下單錯誤!";
 }
 String prepay_Id = map.get("prepay_id");

這個prepay_id就是預支付的ID。后面支付需要它。

相關(guān)文章

  • idea不使用maven如何將項目打包

    idea不使用maven如何將項目打包

    使用IDEA 2021版本,不借助Maven進行打WAR包的步驟是:首先點擊Project Structure,然后點擊Artifacts,接著選擇需要的打包類型,設置好包的名稱,最后進行打包,這種方法適用于不使用Maven進行項目管理的情況
    2024-09-09
  • 基于java Springboot實現(xiàn)教務管理系統(tǒng)詳解

    基于java Springboot實現(xiàn)教務管理系統(tǒng)詳解

    這篇文章主要介紹了Java 實現(xiàn)簡易教務管理系統(tǒng)的代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • 使用Java獲取Json中的數(shù)據(jù)簡單示例

    使用Java獲取Json中的數(shù)據(jù)簡單示例

    開發(fā)過程中經(jīng)常會遇到json數(shù)據(jù)的處理,而單獨對json數(shù)據(jù)進行增刪改并不方便,下面這篇文章主要給大家介紹了關(guān)于使用Java獲取Json中的數(shù)據(jù),文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-04-04
  • Java 遞歸重難點分析詳解與練習

    Java 遞歸重難點分析詳解與練習

    一說起遞歸,我想每個人都不陌生。舉個從小就聽過的例子:從前有座山,山里有座廟,廟里有個和尚,和尚在講故事,從前有座山,山里有座廟,廟里有個和尚,和尚在講故事,從前有座山,要理解遞歸,就得先了解什么是遞歸,實際上這句話就是一個遞歸
    2021-11-11
  • VsCode搭建Java開發(fā)環(huán)境的方法

    VsCode搭建Java開發(fā)環(huán)境的方法

    這篇文章主要介紹了VsCode搭建Java開發(fā)環(huán)境的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 5種Java中數(shù)組的拷貝方法總結(jié)分享

    5種Java中數(shù)組的拷貝方法總結(jié)分享

    這篇文章主要介紹了5種Java中數(shù)組的拷貝方法總結(jié)分享,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-07-07
  • 設計模式之原型模式_動力節(jié)點Java學院整理

    設計模式之原型模式_動力節(jié)點Java學院整理

    這篇文章主要介紹了設計模式之原型模式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 關(guān)于Java的對象序列化流和反序列化流詳細解讀

    關(guān)于Java的對象序列化流和反序列化流詳細解讀

    這篇文章主要介紹了關(guān)于Java的對象序列化流和反序列化流,對象序列化:就是將對象保存到磁盤中,或者在網(wǎng)絡中傳輸對象,反之,自己序列還可以從文件中讀取回來,重構(gòu)對象,對它進行反序列化,需要的朋友可以參考下
    2023-05-05
  • Springboot啟動報錯時實現(xiàn)異常定位

    Springboot啟動報錯時實現(xiàn)異常定位

    這篇文章主要介紹了Springboot啟動報錯時實現(xiàn)異常定位,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • Java算法練習題,每天進步一點點(1)

    Java算法練習題,每天進步一點點(1)

    方法下面小編就為大家?guī)硪黄狫ava算法的一道練習題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07

最新評論

察雅县| 海丰县| 玉树县| 永善县| 昆明市| 原平市| 永吉县| 五莲县| 东海县| 卢龙县| 什邡市| 彰武县| 合江县| 江达县| 衢州市| 宁南县| 镇沅| 婺源县| 灵寿县| 淅川县| 赤城县| 彝良县| 鹤岗市| 韶关市| 贵州省| 油尖旺区| 七台河市| 中超| 桓台县| 平塘县| 武胜县| 九龙县| 宜川县| 镇安县| 崇信县| 沙雅县| 乐至县| 桂东县| 三门峡市| 庆云县| 法库县|