小弟在公眾號后台無意中點了更新(微信支付接口升級)PS:想都沒有想,心里還樂滋滋的免費的干嘛不升級。。。后果來了。面臨着支付不能用了,代碼需要重新更新。
/** * JS_API支付demo * ==================================================== * 在微信瀏覽器里面打開H5網頁中執行JS調起支付。接口輸入輸出數據格式為JSON。 * 成功調起支付需要三個步驟: * 步驟1:網頁授權獲取用戶openid (難點,因為這里有個大坑) * 步驟2:使用統一支付接口,獲取prepay_id(其實就是一個預支付id) * 步驟3:使用jsapi調起支付 */
官方demo下載地址(暫只有PHP)https://mp.weixin.qq.com/paymch/readtemplate?t=mp/business/course3_tmpl&lang=zh_CN%816%A49
源代碼下載: http://pan.baidu.com/s/1qWkb7B2
溫馨提示:
1、支付授權目錄一定要先配置好
2、測試白名單
3、一定要在當前公眾號里面發起支付
步驟一:網頁授權獲取用戶openid (難點,因為這里有個大坑)
官方文檔:https://res.wx.qq.com/paymchres/zh_CN/htmledition/download/bussiness-course2/wxm-payment-oauth1eec8d.pdf
1)獲取 code
2)通過 code 換取網頁授權 accesstoken, 此 access_token 與基礎支持的access_token 不同。
3)通過 accesstoken 換取用戶基本信息
注意:這里必須要配置授權目錄(就是這個坑。。。)
如圖:
步驟二:獲取prepay_id、配置支付的json參數
主要核心代碼如下:
//設置package訂單參數 SortedDictionary<string, string> dic = new SortedDictionary<string, string>(); string total_fee = (Charge_Amt * 100).ToString("f0"); string wx_timeStamp = ""; string wx_nonceStr = Interface_WxPay.getNoncestr(); dic.Add("appid", Interface_WxPay.APPID); dic.Add("mch_id", Interface_WxPay.PARTNER);//財付通帳號商家 dic.Add("device_info", "1000");//可為空 dic.Add("nonce_str", wx_nonceStr); dic.Add("trade_type", "JSAPI"); dic.Add("attach", "att1"); dic.Add("openid", openid); dic.Add("out_trade_no", Bill_No); //商家訂單號 dic.Add("total_fee", total_fee); //商品金額,以分為單位(money * 100).ToString() dic.Add("notify_url", TENPAY_NOTIFY.ToLower());//接收財付通通知的URL dic.Add("body", Body);//商品描述 dic.Add("spbill_create_ip", Context.Request.UserHostAddress); //用戶的公網ip,不是商戶服務器IP string get_sign = BuildRequest(dic, PARTNER_KEY); string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; string _req_data = "<xml>"; _req_data += "<appid>" + Interface_WxPay.APPID + "</appid>"; _req_data += "<attach><![CDATA[att1]]></attach>"; _req_data += "<body><![CDATA[" + Body + "]]></body> "; _req_data += "<device_info><![CDATA[1000]]></device_info> "; _req_data += "<mch_id><![CDATA[" + Interface_WxPay.PARTNER + "]]></mch_id> "; _req_data += "<openid><![CDATA[" + openid + "]]></openid> "; _req_data += "<nonce_str><![CDATA[" + wx_nonceStr + "]]></nonce_str> "; _req_data += "<notify_url><![CDATA[" + TENPAY_NOTIFY.ToLower() + "]]></notify_url> "; _req_data += "<out_trade_no><![CDATA[" + Bill_No + "]]></out_trade_no> "; _req_data += "<spbill_create_ip><![CDATA[" + Context.Request.UserHostAddress + "]]></spbill_create_ip> "; _req_data += "<total_fee><![CDATA[" + total_fee + "]]></total_fee> "; _req_data += "<trade_type><![CDATA[JSAPI]]></trade_type> "; _req_data += "<sign><![CDATA[" + get_sign + "]]></sign> "; _req_data += "</xml>"; //這里的方法只是一個htmlhelper(可以改為自己的) ReturnValue retValue = StreamReaderUtils.StreamReader(url, Encoding.UTF8.GetBytes(_req_data), System.Text.Encoding.UTF8, true); //設置支付參數 XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(retValue.Message); XmlNode Event = xmldoc.SelectSingleNode("/xml/prepay_id"); string return_json = ""; if (Event != null) { string _prepay_id = Event.InnerText; SortedDictionary<string, string> pay_dic = new SortedDictionary<string, string>(); wx_timeStamp = Interface_WxPay.getTimestamp(); wx_nonceStr = Interface_WxPay.getNoncestr(); string _package = "prepay_id=" + _prepay_id; pay_dic.Add("appId", Interface_WxPay.APPID); pay_dic.Add("timeStamp", wx_timeStamp); pay_dic.Add("nonceStr", wx_nonceStr); pay_dic.Add("package", _package); pay_dic.Add("signType", "MD5"); string get_PaySign = BuildRequest(pay_dic, PARTNER_KEY); return_json = JsonUtils.SerializeToJson(new { appId = Interface_Weixin.strAPPID, timeStamp = wx_timeStamp, nonceStr = wx_nonceStr, package = _package, paySign = get_PaySign, signType = "MD5" }); } return return_json;
步驟三:JSAPI網頁支付(demo有代碼、不再累贅)
步驟四:支付回調(花費了一點時間才成功)
代碼如下:

string wxNotifyXml = ""; byte[] bytes = Request.BinaryRead(Request.ContentLength); wxNotifyXml = System.Text.Encoding.UTF8.GetString(bytes); if (wxNotifyXml.Length == 0) { return; } XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(wxNotifyXml); string ResultCode = xmldoc.SelectSingleNode("/xml/result_code").InnerText; string ReturnCode = xmldoc.SelectSingleNode("/xml/return_code").InnerText; if (ReturnCode == "SUCCESS" && ResultCode == "SUCCESS") { //驗證成功 //取結果參數做業務處理 string out_trade_no = xmldoc.SelectSingleNode("/xml/out_trade_no").InnerText; //財付通訂單號 string trade_no = xmldoc.SelectSingleNode("/xml/transaction_id").InnerText; //金額,以分為單位 string total_fee = xmldoc.SelectSingleNode("/xml/total_fee").InnerText; /******************************** * * 自己業務處理 * *********************************/ }
今天上班最后一天了,也是情人節。祝大家情人節快樂。。。。此處應有掌聲
文采不夠好,請諒解!
DEMO下載: http://pan.baidu.com/s/1qWkb7B2