微信公衆號支付入坑-01

微信支付入坑系列-01

要使用微信支付,須要如下幾個步驟:
1.開通微信支付功能,開通後,就能夠拿到商戶id和商戶api密鑰,有了這兩個必須的東西,就能夠調用微信統一支付接口,拿到微信預支付prepayid了.有了prepayid,就能夠在網頁中調用jsapi了。
2.重點是怎麼拿到prepayid呢?很簡單,調用post請求道https://api.mch.weixin.qq.com/pay/unifiedorder地址就能夠了,很簡單吧。咋一看確實簡單,但這裏面隱藏了好幾個坑。
3.要拿到peipayid,須要傳遞一組xml數據,對應java攻城獅來講,通常都會寫個model,填充數據,而後將model轉換爲xml字符串,而後跟隨post請求一塊兒發送出去。
4.在一組xml數據中,有一個簽名,初次遇到的人確定都會蒙。這個簽名要怎麼籤呢,要把你全部要傳遞給服務器的數據進行處理。記住:是全部要傳遞給服務器(騰訊)的數據,除了sign這個數據外的全部數據。sign也要被傳送到服務器端去。

通常是這樣的:
               private String appid;// 公衆帳號ID
               private String mch_id;// 商戶號
               private String device_info;// 設備號
               private String nonce_str;// 隨機字符串
               private String sign;// 簽名
               private String body;// 商品描述
               private String detail;// 商品詳情
               private String attach;// 附加數據
               ... ...
          將這些字段先進行排序,字典排序,直接調用Arrays.sort(String[]);這樣就排序完了,而後再拼接成一個字符串,appid=123&mch_id=123......以此類推,最後在加上商品api密鑰key=123.完了以後,進行md5轉換

5.我遇到的下一個坑,也很簡單,但折騰了我好久。我將建立好的model轉換爲xml,使用的是如下代碼:XStream xStream = new XStream();
        xStream.alias("xml", object.getClass());
         轉換結果以下:
         
 <appid>xxxxxxxxxxxxx</appid>
  <mch__id>xxxxxxxxxxxx</mch__id>
  <nonce__str>1add1a30ac87aa2db72f57a2375d8fec</nonce__str>
  <sign>C939DCA5210FEDF5C9651412C966EA01</sign>
  <body>test</body>
  <out__trade__no>1ad41a30ac87aa2db72f57a2375d8fec</out__trade__no>
  <total__fee>1</total__fee>
  <spbill__create__ip>xxx.xx.xx.xx</spbill__create__ip>
  <notify__url>xxxxxxxxxxxxxxxx</notify__url>
  <trade__type>JSAPI</trade__type>
  <openid>xxxxxxxxxxx</openid>
</xml>
     乍一看,這是沒有問題的,其實應該是這樣的:
     <xml><appid><![CDATA[xxxxxxxxxxx]]></appid><mch_id><![CDATA[xxxxxxxxxx]]></mch_id><nonce_str><![CDATA[1add1a30ac87aa2db72f57a2375d8fec]]></nonce_str></xml>
       也就是說:只要是字符串,就得用<![CDATA[   ]]>包裹起來,整型數據不用包裹,沒有其餘型號的數據了
相關文章
相關標籤/搜索