Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android端微信支付V3版本地簽名統一下單詳解

android端微信支付V3版本地簽名統一下單詳解

編輯:關於Android編程

滿滿的都是坑,因為服務器偷懶讓客服端寫統一下單,服務器只給了通知的url。微信的支付demo並沒有統一下單的代碼。

讀此文前先閱讀: https://pay.weixin.qq.com/wiki/doc/api/app/app.PHP?chapter=9_1

一步步的來  先根據統一下單的參數介紹工具:

1. 獲取到當前的ip:

<span >public String getLocalIpAddress() { 
    try { 
      for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { 
        NetworkInterface intf = en.nextElement(); 
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { 
          InetAddress inetAddress = enumIpAddr.nextElement(); 
          if (!inetAddress.isLoopbackAddress()) { 
            return inetAddress.getHostAddress().toString(); 
          } 
        } 
      } 
    } catch (SocketException ex) { 
    } 
    return null; 
  } 
 
  private String getWifiIp() { 
    //獲取wifi服務 
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
    //判斷wifi是否開啟 
    if (!wifiManager.isWifiEnabled()) { 
      wifiManager.setWifiEnabled(true); 
    } 
    WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
    int ipAddress = wifiInfo.getIpAddress(); 
    String ip = intToIp(ipAddress); 
    return ip; 
  } 
 
  private String intToIp(int i) { 
 
    return (i & 0xFF) + "." + 
        ((i >> 8) & 0xFF) + "." + 
        ((i >> 16) & 0xFF) + "." + 
        (i >> 24 & 0xFF); 
  } 
</span> 

2.隨機訂單號生成 test 你們可根據自己生成隨機數:

<span >private String genOutTradNo() { 
    Random random = new Random(); 
    return MD5.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes()); 
  }</span> 

3.簽名工具:

<span >private String genAppSign(List<NameValuePair> params) { 
    StringBuilder sb = new StringBuilder(); 
 
    for (int i = 0; i < params.size(); i++) { 
      sb.append(params.get(i).getName()); 
      sb.append('='); 
      sb.append(params.get(i).getValue()); 
      sb.append('&'); 
    } 
    sb.append("key="); 
    sb.append(Constants.API_KEY); 
 
    this.sb.append("sign str\n"+sb.toString()+"\n\n"); 
    String appSign = MD5.getMessageDigest(sb.toString().getBytes()); 
    Log.e("orion",appSign); 
    return appSign; 
  }</span> 

差不多了  現在我們需要生成傳遞的參數   參數要求是xml 格式的:

<span >private String genProductArgs() { 
    StringBuffer xml = new StringBuffer(); 
    String ip = getWifiIp(); 
    if (ip == "" && ip == "") { 
      ip = getLocalIpAddress(); 
    } 
    try { 
      String nonceStr = genNonceStr(); 
      xml.append("</xml>"); 
      List<NameValuePair> packageParams = new LinkedList<NameValuePair>(); 
      packageParams.add(new BasicNameValuePair("appid", Constants.APP_ID)); 
      packageParams.add(new BasicNameValuePair("body", "APP pay test")); 
      packageParams.add(new BasicNameValuePair("mch_id", Constants.MCH_ID)); 
      packageParams.add(new BasicNameValuePair("nonce_str", nonceStr)); 
      packageParams.add(new BasicNameValuePair("notify_url",ConfigUtil.NOTIFY_URL)); 
      packageParams.add(new BasicNameValuePair("out_trade_no",genOutTradNo())); 
      packageParams.add(new BasicNameValuePair("spbill_create_ip",ip)); 
      packageParams.add(new BasicNameValuePair("total_fee", "1")); 
      packageParams.add(new BasicNameValuePair("trade_type", "APP")); 
      String sign = genPackageSign(packageParams); 
      packageParams.add(new BasicNameValuePair("sign", sign)); 
      String xmlstring =toXml(packageParams); 
      return xmlstring; 
 
    } catch (Exception e) { 
      Log.e("TAG", "fail, ex = " + e.getMessage()); 
      return null; 
    } 
  }</span> 

其中  toxml:

<span >private String toXml(List<NameValuePair> params) { 
    StringBuilder sb = new StringBuilder(); 
    sb.append("<xml>"); 
    for (int i = 0; i < params.size(); i++) { 
      sb.append("<"+params.get(i).getName()+">"); 
 
 
      sb.append(params.get(i).getValue()); 
      sb.append("</"+params.get(i).getName()+">"); 
    } 
    sb.append("</xml>"); 
 
    Log.e("orion",sb.toString()); 
    return sb.toString(); 
  }</span> 

得到傳遞的參數,根據文檔指示,我們需要用post去吊連接URL地址:https://api.mch.weixin.qq.com/pay/unifiedorder:

<span >  private class GetPrepayIdTask extends AsyncTask<Void, Void, Map<String,String>> { 
 
    private ProgressDialog dialog; 
 
 
    @Override 
    protected void onPreExecute() { 
      dialog = ProgressDialog.show(PayActivity.this, getString(R.string.app_tip), getString(R.string.getting_prepayid)); 
    } 
 
    @Override 
    protected void onPostExecute(Map<String,String> result) { 
      if (dialog != null) { 
        dialog.dismiss(); 
      } 
      sb.append("prepay_id\n"+result.get("prepay_id")+"\n\n"); 
 
      resultunifiedorder=result; 
 
    } 
 
    @Override 
    protected void onCancelled() { 
      super.onCancelled(); 
    } 
 
    @Override 
    protected Map<String,String> doInBackground(Void... params) { 
 
      String url = String.format("https://api.mch.weixin.qq.com/pay/unifiedorder"); 
      String entity = genProductArgs(); 
 
      Log.e("orion",entity); 
 
      byte[] buf = Util.httpPost(url, entity); 
 
      String content = new String(buf); 
      Log.e("orion", content); 
      Map<String,String> xml=decodeXml(content); 
 
      return xml; 
    } 
  }</span> 

 其中decodexml就是:

 <span >public Map<String,String> decodeXml(String content) { 
 
    try { 
      Map<String, String> xml = new HashMap<String, String>(); 
      XmlPullParser parser = Xml.newPullParser(); 
      parser.setInput(new StringReader(content)); 
      int event = parser.getEventType(); 
      while (event != XmlPullParser.END_DOCUMENT) { 
 
        String nodeName=parser.getName(); 
        switch (event) { 
          case XmlPullParser.START_DOCUMENT: 
 
            break; 
          case XmlPullParser.START_TAG: 
 
            if("xml".equals(nodeName)==false){ 
              //實例化student對象 
              xml.put(nodeName,parser.nextText()); 
            } 
            break; 
          case XmlPullParser.END_TAG: 
            break; 
        } 
        event = parser.next(); 
      } 
 
      return xml; 
    } catch (Exception e) { 
      Log.e("orion",e.toString()); 
    } 
    return null; 
 
  }</span> 

 下單完成,第二部就是給微信支付傳遞調起微信支付的參數(具體參數看文檔說明):

 <span >private void genPayReq() { 
 
    req.appId = Constants.APP_ID; 
    req.partnerId = Constants.MCH_ID; 
    req.prepayId = resultunifiedorder.get("prepay_id"); 
    req.packageValue = "prepay_id="+resultunifiedorder.get("prepay_id"); 
    req.nonceStr = genNonceStr(); 
    req.timeStamp = String.valueOf(genTimeStamp()); 
    List<NameValuePair> signParams = new LinkedList<NameValuePair>(); 
    signParams.add(new BasicNameValuePair("appid", req.appId)); 
    signParams.add(new BasicNameValuePair("noncestr", req.nonceStr)); 
    signParams.add(new BasicNameValuePair("package", req.packageValue)); 
    signParams.add(new BasicNameValuePair("partnerid", req.partnerId)); 
    signParams.add(new BasicNameValuePair("prepayid", req.prepayId)); 
    signParams.add(new BasicNameValuePair("timestamp", req.timeStamp)); 
    req.sign = genAppSign(signParams); 
    sb.append("sign\n"+req.sign+"\n\n"); 
 
    Log.e("orion", signParams.toString()); 
 
  }</span> 

 第三部,調微信支付:

 <span >private void sendPayReq() { 
    msgApi.registerApp(Constants.APP_ID); 
    msgApi.sendReq(req); 
  }</span> 

 其中:

 <span >Constants.APP_ID</span> 

是appid 在開發者平台獲取

<span >    req.partnerId = Constants.MCH_ID;</span> 

商戶id

<span >ConfigUtil.NOTIFY_URL</span> 

支付後的回調通知地址。

簽名兩次,然後用的api——key是商戶平台api安全裡面自定義的。

對了 還有個獲取時間工具

<span >private long genTimeStamp() { 
    return System.currentTimeMillis() / 1000; 
  }</span> 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved