asp.net 微信支付 例項程式碼
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="jkmobile.wx_pay.index" %> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no" /> <meta charset="utf-8" /> <meta name="viewport"> <title>專家諮詢支付頁面</title> <link href="../Content/css.css" rel="stylesheet" /> <link href="../Content/bootstrap.css" rel="stylesheet" /> <script src="../Scripts/jquery-1.10.2.js"></script> <script src="../Scripts/bootstrap.js"></script> <script src="../Scripts/layer_mobile/layer.js"></script> <style> .enter { width: 30px; } .enter2 { height: 40px; } .font_yahei { font-family: 'Microsoft YaHei'; } h3 { text-align: center; } .content_backgroundColor { background-color: #F5F3F3; } </style> </head> <body> <div class="container body-content"> <div class="row baseGreen"> <h3>專家諮詢支付頁面</h3> </div> <div class="row"> <div style=" height:20px;"> </div> </div> <div class="row"> <div class="col-xs-2 col-sm-2"> </div> <div class="col-xs-10 col-sm-10"> <h4> 產品名稱:專家諮詢</h4> </div> </div> <div class="row" style="margin-bottom:30px;"> <div class="col-xs-2 col-sm-2"> </div> <div class="col-xs-10 col-sm-10"> <h4> 合計金額:<%=str_order_money %>(元)</h4> </div> </div> <div class="row"> <div class="col-xs-12 col-sm-12" style="text-align:right ;"> <button type="button" class="btn btn-warning btn-block btn-lg" onclick="fCharge()">付款</button> </div> </div> <div class="row"> <div style=" height:10px;"> </div> </div> <div class="row"> <div class="col-xs-12 col-sm-12"> <button type="button" class="btn btn-info btn-block" onclick="fBackHome()">返回首頁</button> </div> </div> <div class="row"> <div style=" height:20px;"> </div> </div> </div> <input id="order_money" type="hidden" value="<%=order_money %>" /> <input id="openid" type="hidden" value="<%=openid %>" /> <input id="order_id" type="hidden" value="<%=order_id %>" /> <input id="order_number" type="hidden" value="<%=order_number %>" /> <script type="text/javascript"> //獲取url的引數 function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } //初始化微信支付環境 function fCharge() { //fPostCharge(); if (typeof WeixinJSBridge == "undefined") { if (document.addEventListener) { document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false); } else if (document.attachEvent) { document.attachEvent('WeixinJSBridgeReady', onBridgeReady); document.attachEvent('onWeixinJSBridgeReady', onBridgeReady); } } else { fPostCharge(); } } //提交充值資料 function fPostCharge() { var vChargeVal = $("#order_money").val(); vChargeVal = parseFloat(vChargeVal); if (vChargeVal > 0) { layer.open({ type: 2, content: '正在啟動微信支付...' }); $.ajax({ type: "post", data: { step: "wx_pay", order_id: $("#order_id").val(), order_number: $("#order_number").val(), openid: $("#openid").val(), totalfee: vChargeVal }, url: "/wx_pay/wx_pay.ashx", success: function (data) { layer.closeAll();//記得關閉 var json = eval("(" + data + ")");//轉換後的JSON物件 onBridgeReady(json); }, error: function () { layer.closeAll(); layer.open({ content: '呼叫微信支付模組失敗,請稍後再試!', btn: '確定' }); } }) } else { layer.open({ content: '付款金額不能小於等於0!', btn: '確定' }); } } //呼叫微信支付模組 function onBridgeReady(json) { WeixinJSBridge.invoke( 'getBrandWCPayRequest', { "appId": json.appId, //公眾號名稱,由商戶傳入 "timeStamp": json.timeStamp, //時間戳,自1970年以來的秒數 "nonceStr": json.nonceStr, //隨機串 "package": json.packageValue, "signType": "MD5", //微信簽名方式: "paySign": json.paySign //微信簽名 }, function (res) { if (res.err_msg == "get_brand_wcpay_request:ok") { layer.closeAll(); //自定義結果頁面 window.location.href = "http://*********"; } else { location.href = "/wx_pay/pay_error.aspx"; } // 使用以上方式判斷前端返回,微信團隊鄭重提示:res.err_msg將在使用者支付成功後返回 ok,但並不保證它絕對可靠。 }); } function fBackHome() { //自定義結果頁面 location.href = "http://******"; } </script> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace jkmobile.wx_pay { public partial class index : System.Web.UI.Page { public string order_id = "", order_number = "", openid = "", str_order_money = "0.00"; public int order_money = 0; protected void Page_Load(object sender, EventArgs e) { Mobile_Healthy.Log.PublicLog.WriteLog("請求支付地址及引數:", "1", Request.Url.ToString()); try { if (Request.QueryString["openid"] != null) { openid = Request.QueryString["openid"]; } if (Request.QueryString["returnValue"] != null && !string.IsNullOrEmpty(Request.QueryString["returnValue"])) { string[] array_value = Request.QueryString["returnValue"].ToString().Split('|'); if (array_value.Length > 0) { order_id = array_value[0]; str_order_money = (Convert.ToDecimal(array_value[1]) / 100).ToString("#0.00"); order_money = Convert.ToInt32(array_value[1]); order_number = array_value[2]; } } } catch (Exception ex){ Mobile_Healthy.Log.PublicLog.WriteLog("請求支付異常詳情:","4", ex.ToString()); } } } }
wx_pay.ashx.cs程式碼
using jkmobile.TenPayLibV3; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Web; namespace jkmobile.wx_pay { /// <summary> /// wx_pay 的摘要說明 /// </summary> public class wx_pay : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request.Params["step"] != null) { JsApiPay jsApiPay = new JsApiPay(); if (context.Request.Params["step"] == "wx_pay") { #region 購買專家諮詢 object objResult = ""; //金額單位分 string strFee = context.Request.Params["totalfee"]; string order_id = context.Request.Params["order_id"]; string order_number = context.Request.Params["order_number"]; //若傳遞了相關引數,則調統一下單介面,獲得後續相關介面的入口引數 jsApiPay.openid = context.Request.Params["openid"]; jsApiPay.total_fee = int.Parse(strFee); //JSAPI支付預處理 try { string strBody = "健康專案-專家諮詢購買";//商品描述 WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(strBody, order_id, jsApiPay.openid, order_number, WxPayConfig.NOTIFY_URL); WxPayData wxJsApiParam = jsApiPay.GetJsApiParameters();//獲取H5調起JS API引數 ModelForOrder aOrder = new ModelForOrder() { appId = wxJsApiParam.GetValue("appId").ToString(), nonceStr = wxJsApiParam.GetValue("nonceStr").ToString(), packageValue = wxJsApiParam.GetValue("package").ToString(), paySign = wxJsApiParam.GetValue("paySign").ToString(), timeStamp = wxJsApiParam.GetValue("timeStamp").ToString(), msg = "成功下單,正在喚起微信支付." }; objResult = aOrder; } catch (Exception ex) { ModelForOrder aOrder = new ModelForOrder() { appId = "", nonceStr = "", packageValue = "", paySign = "", timeStamp = "", msg = "下單失敗,請重試,多次失敗,請聯絡管理員." }; objResult = aOrder; } string strJson = JsonConvert.SerializeObject(objResult); context.Response.Write(strJson); #endregion } } context.Response.Flush(); context.Response.End(); } public bool IsReusable { get { return false; } } } }
/**
* 呼叫統一下單,獲得下單結果
* @return 統一下單結果
* @失敗時拋異常WxPayException
*/
/// <summary>
/// 呼叫統一下單,獲得下單結果
/// </summary>
/// <param name="strBody">商品描述</param>
/// <param name="out_trade_no">商戶訂單號</param>
/// <param name="openid2">微信id</param>
/// <param name="order_number">產品購買明細id</param>
/// /// <param name="notify_url">通知地址</param>
/// <returns></returns>
public WxPayData GetUnifiedOrderResult(string strBody, string out_trade_no, string openid2, string order_number,string notify_url)
{
try
{
Mobile_Healthy.Log.PublicLog.WriteLog("統一下單回撥地址:", "1", JsonConvert.SerializeObject(notify_url));
}
catch { }
//統一下單
WxPayData data = new WxPayData();
data.SetValue("body", strBody);
data.SetValue("attach", openid2 + "|" + order_number);
data.SetValue("out_trade_no", out_trade_no);
data.SetValue("total_fee", total_fee);
data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));
data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));
data.SetValue("goods_tag", "test");
data.SetValue("trade_type", "JSAPI");
data.SetValue("notify_url", notify_url);
data.SetValue("openid", openid2);
WxPayData result = WxPayApi.UnifiedOrder(data);
if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "")
{
Log.Error(this.GetType().ToString(), "UnifiedOrder response error!");
throw new WxPayException("UnifiedOrder response error!");
}
unifiedOrderResult = result;
return result;
}
/** * * 統一下單 * @param WxPaydata inputObj 提交給統一下單API的引數 * @param int timeOut 超時時間 * @throws WxPayException * @return 成功時返回,其他拋異常 */
/// <summary> /// 統一下單 /// </summary> /// <param name="inputObj"></param> /// <param name="timeOut"></param> /// <returns></returns> public static WxPayData UnifiedOrder(WxPayData inputObj, int timeOut = 6) { string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //檢測必填引數 if (!inputObj.IsSet("out_trade_no")) { throw new WxPayException("缺少統一支付介面必填引數out_trade_no!"); } else if (!inputObj.IsSet("body")) { throw new WxPayException("缺少統一支付介面必填引數body!"); } else if (!inputObj.IsSet("total_fee")) { throw new WxPayException("缺少統一支付介面必填引數total_fee!"); } else if (!inputObj.IsSet("trade_type")) { throw new WxPayException("缺少統一支付介面必填引數trade_type!"); }
//關聯引數 if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid")) { throw new WxPayException("統一支付介面中,缺少必填引數openid!trade_type為JSAPI時,openid為必填引數!"); } if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id")) { throw new WxPayException("統一支付介面中,缺少必填引數product_id!trade_type為JSAPI時,product_id為必填引數!"); }
//非同步通知url未設定,則使用配置檔案中的url //if (inputObj.IsSet("notify_url")) //{ // inputObj.SetValue("notify_url", WxPayConfig.NOTIFY_URL);//非同步通知url //}
inputObj.SetValue("appid", WxPayConfig.APPID);//公眾賬號ID inputObj.SetValue("mch_id", WxPayConfig.MCHID);//商戶號 inputObj.SetValue("spbill_create_ip", WxPayConfig.IP);//終端ip inputObj.SetValue("nonce_str", GenerateNonceStr());//隨機字串
//簽名 inputObj.SetValue("sign", inputObj.MakeSign()); string xml = inputObj.ToXml();
var start = DateTime.Now;
Log.Debug("WxPayApi", "UnfiedOrder request : " + xml); string response = HttpService.Post(xml, url, false, timeOut); Log.Debug("WxPayApi", "UnfiedOrder response : " + response);
var end = DateTime.Now; int timeCost = (int)((end - start).TotalMilliseconds);
WxPayData result = new WxPayData(); result.FromXml(response);
ReportCostTime(url, timeCost, result);//測速上報
return result; }
/** * * 測速上報 * @param string interface_url 介面URL * @param int timeCost 介面耗時 * @param WxPayData inputObj引數陣列 */ private static void ReportCostTime(string interface_url, int timeCost, WxPayData inputObj) { //如果不需要進行上報 if(WxPayConfig.REPORT_LEVENL == 0) { return; }
//如果僅失敗上報 if(WxPayConfig.REPORT_LEVENL == 1 && inputObj.IsSet("return_code") && inputObj.GetValue("return_code").ToString() == "SUCCESS" && inputObj.IsSet("result_code") && inputObj.GetValue("result_code").ToString() == "SUCCESS") { return; } //上報邏輯 WxPayData data = new WxPayData(); data.SetValue("interface_url",interface_url); data.SetValue("execute_time_",timeCost); //返回狀態碼 if(inputObj.IsSet("return_code")) { data.SetValue("return_code",inputObj.GetValue("return_code")); } //返回資訊 if(inputObj.IsSet("return_msg")) { data.SetValue("return_msg",inputObj.GetValue("return_msg")); } //業務結果 if(inputObj.IsSet("result_code")) { data.SetValue("result_code",inputObj.GetValue("result_code")); } //錯誤程式碼 if(inputObj.IsSet("err_code")) { data.SetValue("err_code",inputObj.GetValue("err_code")); } //錯誤程式碼描述 if(inputObj.IsSet("err_code_des")) { data.SetValue("err_code_des",inputObj.GetValue("err_code_des")); } //商戶訂單號 if(inputObj.IsSet("out_trade_no")) { data.SetValue("out_trade_no",inputObj.GetValue("out_trade_no")); } //裝置號 if(inputObj.IsSet("device_info")) { data.SetValue("device_info",inputObj.GetValue("device_info")); } try { Report(data); } catch (WxPayException ex) { //不做任何處理 } }
/** * * 從統一下單成功返回的資料中獲取微信瀏覽器調起jsapi支付所需的引數, * 微信瀏覽器調起JSAPI時的輸入引數格式如下: * { * "appId" : "wx2421b1c4370ec43b", //公眾號名稱,由商戶傳入 * "timeStamp":" 1395712654", //時間戳,自1970年以來的秒數 * "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //隨機串 * "package" : "prepay_id=u802345jgfjsdfgsdg888", * "signType" : "MD5", //微信簽名方式: * "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信簽名 * } * @return string 微信瀏覽器調起JSAPI時的輸入引數,json格式可以直接做引數用 * 更詳細的說明請參考網頁端調起支付API:http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7 * */ public WxPayData GetJsApiParameters() { Log.Debug(this.GetType().ToString(), "JsApiPay::GetJsApiParam is processing...");
WxPayData jsApiParam = new WxPayData(); jsApiParam.SetValue("appId", unifiedOrderResult.GetValue("appid")); jsApiParam.SetValue("timeStamp", WxPayApi.GenerateTimeStamp()); jsApiParam.SetValue("nonceStr", WxPayApi.GenerateNonceStr()); jsApiParam.SetValue("package", "prepay_id=" + unifiedOrderResult.GetValue("prepay_id")); jsApiParam.SetValue("signType", "MD5"); jsApiParam.SetValue("paySign", jsApiParam.MakeSign());
string parameters = jsApiParam.ToJson(); Log.Debug(this.GetType().ToString(), "Get jsApiParam : " + parameters); return jsApiParam; }
WxPayData.cs 微信支付協議介面資料類,所有的API介面通訊都依賴這個資料結構,
using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.Security.Cryptography;
using System.Text;
using LitJson;
namespace jkmobile.TenPayLibV3
{
/// <summary>
/// 微信支付協議介面資料類,所有的API介面通訊都依賴這個資料結構,
/// 在呼叫介面之前先填充各個欄位的值,然後進行介面通訊,
/// 這樣設計的好處是可擴充套件性強,使用者可隨意對協議進行更改而不用重新設計資料結構,
/// 還可以隨意組合出不同的協議資料包,不用為每個協議設計一個數據包結構
/// </summary>
public class WxPayData
{
public WxPayData()
{
}
//採用排序的Dictionary的好處是方便對資料包進行簽名,不用再簽名之前再做一次排序
private SortedDictionary<string, object> m_values = new SortedDictionary<string, object>();
/**
* 設定某個欄位的值
* @param key 欄位名
* @param value 欄位值
*/
public void SetValue(string key, object value)
{
m_values[key] = value;
}
/**
* 根據欄位名獲取某個欄位的值
* @param key 欄位名
* @return key對應的欄位值
*/
public object GetValue(string key)
{
object o = null;
m_values.TryGetValue(key, out o);
return o;
}
/**
* 判斷某個欄位是否已設定
* @param key 欄位名
* @return 若欄位key已被設定,則返回true,否則返回false
*/
public bool IsSet(string key)
{
object o = null;
m_values.TryGetValue(key, out o);
if (null != o)
return true;
else
return false;
}
/**
* @將Dictionary轉成xml
* @return 經轉換得到的xml串
* @throws WxPayException
**/
public string ToXml()
{
//資料為空時不能轉化為xml格式
if (0 == m_values.Count)
{
Log.Error(this.GetType().ToString(), "WxPayData資料為空!");
throw new WxPayException("WxPayData資料為空!");
}
string xml = "<xml>";
foreach (KeyValuePair<string, object> pair in m_values)
{
//欄位值不能為null,會影響後續流程
if (pair.Value == null)
{
Log.Error(this.GetType().ToString(), "WxPayData內部含有值為null的欄位!");
throw new WxPayException("WxPayData內部含有值為null的欄位!");
}
if (pair.Value.GetType() == typeof(int))
{
xml += "<" + pair.Key + ">" + pair.Value + "</" + pair.Key + ">";
}
else if (pair.Value.GetType() == typeof(string))
{
xml += "<" + pair.Key + ">" + "<![CDATA[" + pair.Value + "]]></" + pair.Key + ">";
}
else//除了string和int型別不能含有其他資料型別
{
Log.Error(this.GetType().ToString(), "WxPayData欄位資料型別錯誤!");
throw new WxPayException("WxPayData欄位資料型別錯誤!");
}
}
xml += "</xml>";
return xml;
}
/**
* @將xml轉為WxPayData物件並返回物件內部的資料
* @param string 待轉換的xml串
* @return 經轉換得到的Dictionary
* @throws WxPayException
*/
public SortedDictionary<string, object> FromXml(string xml)
{
if (string.IsNullOrEmpty(xml))
{
Log.Error(this.GetType().ToString(), "將空的xml串轉換為WxPayData不合法!");
throw new WxPayException("將空的xml串轉換為WxPayData不合法!");
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
XmlNode xmlNode = xmlDoc.FirstChild;//獲取到根節點<xml>
XmlNodeList nodes = xmlNode.ChildNodes;
foreach (XmlNode xn in nodes)
{
XmlElement xe = (XmlElement)xn;
m_values[xe.Name] = xe.InnerText;//獲取xml的鍵值對到WxPayData內部的資料中
}
try
{
//2015-06-29 錯誤是沒有簽名
if (m_values["return_code"].ToString() != "SUCCESS")
{
return m_values;
}
CheckSign();//驗證簽名,不通過會拋異常
}
catch (WxPayException ex)
{
throw new WxPayException(ex.Message);
}
return m_values;
}
/**
* @Dictionary格式轉化成url引數格式
* @ return url格式串, 該串不包含sign欄位值
*/
public string ToUrl()
{
string buff = "";
foreach (KeyValuePair<string, object> pair in m_values)
{
if (pair.Value == null)
{
Log.Error(this.GetType().ToString(), "WxPayData內部含有值為null的欄位!");
throw new WxPayException("WxPayData內部含有值為null的欄位!");
}
if (pair.Key != "sign" && pair.Value.ToString() != "")
{
buff += pair.Key + "=" + pair.Value + "&";
}
}
buff = buff.Trim('&');
return buff;
}
/**
* @Dictionary格式化成Json
* @return json串資料
*/
public string ToJson()
{
string jsonStr = JsonMapper.ToJson(m_values);
return jsonStr;
}
/**
* @values格式化成能在Web頁面上顯示的結果(因為web頁面上不能直接輸出xml格式的字串)
*/
public string ToPrintStr()
{
string str = "";
foreach (KeyValuePair<string, object> pair in m_values)
{
if (pair.Value == null)
{
Log.Error(this.GetType().ToString(), "WxPayData內部含有值為null的欄位!");
throw new WxPayException("WxPayData內部含有值為null的欄位!");
}
str += string.Format("{0}={1}<br>", pair.Key, pair.Value.ToString());
}
Log.Debug(this.GetType().ToString(), "Print in Web Page : " + str);
return str;
}
/**
* @生成簽名,詳見簽名生成演算法
* @return 簽名, sign欄位不參加簽名
*/
public string MakeSign()
{
//轉url格式
string str = ToUrl();
//在string後加入API KEY
str += "&key=" + WxPayConfig.KEY;
//MD5加密
var md5 = MD5.Create();
var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
var sb = new StringBuilder();
foreach (byte b in bs)
{
sb.Append(b.ToString("x2"));
}
//所有字元轉為大寫
return sb.ToString().ToUpper();
}
/**
*
* 檢測簽名是否正確
* 正確返回true,錯誤拋異常
*/
public bool CheckSign()
{
//如果沒有設定簽名,則跳過檢測
if (!IsSet("sign"))
{
Log.Error(this.GetType().ToString(), "WxPayData簽名存在但不合法!");
throw new WxPayException("WxPayData簽名存在但不合法!");
}
//如果設定了簽名但是簽名為空,則拋異常
else if (GetValue("sign") == null || GetValue("sign").ToString() == "")
{
Log.Error(this.GetType().ToString(), "WxPayData簽名存在但不合法!");
throw new WxPayException("WxPayData簽名存在但不合法!");
}
//獲取接收到的簽名
string return_sign = GetValue("sign").ToString();
//在本地計算新的簽名
string cal_sign = MakeSign();
if (cal_sign == return_sign)
{
return true;
}
Log.Error(this.GetType().ToString(), "WxPayData簽名驗證錯誤!");
throw new WxPayException("WxPayData簽名驗證錯誤!");
}
/**
* @獲取Dictionary
*/
public SortedDictionary<string, object> GetValues()
{
return m_values;
}
}
}