模型层实体类:
public class JsEntities { /// <summary> /// 公众号id /// </summary> public string appId { get; set; } /// <summary> /// 时间戳 /// </summary> public string timeStamp { get; set; } /// <summary> /// 随机字符串 /// </summary> public string nonceStr { get; set; } /// <summary> /// 订单详情扩展字符串 /// </summary> public string package { get; set; } /// <summary> /// 签名类型 /// </summary> public string signType { get; set; } /// <summary> /// 签名 /// </summary> public string paySign { get; set; } }
登录后复制
public class UnifyEntities { /// <summary> /// 公众账号ID /// </summary> public string appid { get; set; } /// <summary> /// 微信支付分配的商户号 /// </summary> public string mch_id { get; set; } /// <summary> /// 微信支付分配的终端设备号 /// </summary> public string device_info { get; set; } /// <summary> /// 随机字符串,不长于32位 /// </summary> public string nonce_str { get; set; } /// <summary> /// 签名 /// </summary> public string sign { get; set; } /// <summary> /// 商品描述最大长度127 /// </summary> public string body { get; set; } /// <summary> /// 附加数据,原样返回 /// </summary> public string attach { get; set; } /// <summary> /// 商户系统内部的订单号,32 个字符内、可包含字母,确保在商户系统唯一,详细说明 /// </summary> public string out_trade_no { get; set; } /// <summary> /// 订单总金额,单位为分,不能带小数点 /// </summary> public string total_fee { get; set; } /// <summary> /// 终端IP /// </summary> public string spbill_create_ip { get; set; } /// <summary> /// 交易起始时间 /// </summary> public string time_start { get; set; } /// <summary> /// 交易结束时间 /// </summary> public string time_expire { get; set; } /// <summary> /// 接收微信支付成功通知 /// </summary> public string notify_url { get; set; } /// <summary> /// JSAPI、NATIVE、APP /// </summary> public string trade_type { get; set; } /// <summary> /// 用户在商户appid下的唯一标识,trade_type为JSAPI 时,此参数必传 /// </summary> public string openid { get; set; } /// <summary> /// 只在 trade_type 为 NATIVE 时需要填写。此id为二维码中包含的商品ID,商户自行维护。 /// </summary> public string product_id { get; set; } }
登录后复制
public class UnifyReceive { /// <summary> /// SUCCESS/FAIL此字段是通信标识,非交易标识,交易是否成功需要查看result_code来判断 /// </summary> public string return_code { get; set; } /// <summary> /// 返回信息,如非空,为错误原因 /// </summary> public string return_msg { get; set; } /// <summary> /// 微信分配的公众账号ID /// </summary> public string appid { get; set; } /// <summary> /// 微信支付分配的商户号 /// </summary> public string mch_id { get; set; } /// <summary> /// 随机字符串,不长于32位 /// </summary> public string nonce_str { get; set; } /// <summary> /// 签名 /// </summary> public string sign { get; set; } /// <summary> /// 业务结果 /// </summary> public string result_code { get; set; } /// <summary> /// 预支付ID /// </summary> public string prepay_id { get; set; } /// <summary> /// 交易类型 /// </summary> public string trade_type { get; set; } /// <summary> /// 二维码链接 /// </summary> public string code_url { get; set; } public UnifyReceive(string xml) { XElement doc = XElement.Parse(xml); return_code = doc.Element("return_code").Value; return_msg = doc.Element("return_msg").Value; if (return_code == "SUCCESS") { appid = doc.Element("appid").Value; mch_id = doc.Element("mch_id").Value; nonce_str = doc.Element("nonce_str").Value; sign = doc.Element("sign").Value; result_code = doc.Element("result_code").Value; if (result_code == "SUCCESS") { trade_type = doc.Element("trade_type").Value; prepay_id = doc.Element("prepay_id").Value; if (trade_type == "NATIVE") { code_url = doc.Element("code_url").Value; } trade_type = doc.Element("trade_type").Value; } } } }
登录后复制
TestJs.aspx内容:
<meta> <title></title> <script></script> <script></script> <script> $(function () { $("#submit").click(function () { var OID ="<%=openid%>"; alert(OID); $.get("WxPay/WxPay.ashx?action=jspayparam", { body: $("#body").val(), total_fee: $("#price").val(), out_trade_no: $("#order").val(), trade_type: "JSAPI", msgid: "<%=openid%>" }, function (data) { WxPay.Pay(data.appId, data.timeStamp, data.nonceStr, data.package, data.signType, data.paySign, function () { alert("支付成功"); }); }, "json"); }); }) </script>
登录后复制
商品描述: 商品价格: 订单号:
JsPay.aspx.cs代码:
public partial class JsPay : System.Web.UI.Page { public string openid = ""; protected void Page_Load(object sender, EventArgs e) { string code = Request["code"]; if (string.IsNullOrEmpty(code)) { //如果code没获取成功,重新拉取一遍 GetAuthUrl("wxxxxxxxxxxxxxxxxxxxxxxx", "http://www.china101.net/JsPay.aspx"); } openid = GetOpenID("wxxxxxxxxxxxxxxxxxxxxxxx", "dsdssdsdsdsdsdsdsdsdsd", JKRequest.GetQueryString("code"), () => { }); } public string GetOpenID(string appid, string secret, string code, Action CallBack) { try { string retdata = Utils.HttpGet(string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, secret, code)); .LogHelper.WriteFile(retdata); JObject jobj = (JObject)JsonConvert.DeserializeObject(retdata); string openid = jobj.Value<string>("openid"); return openid; } catch (Exception) { CallBack(); return ""; } // return GetUserInfo(access_token, openid); } /// <summary> /// 获取鉴权地址 /// </summary> /// <param> /// <param> /// <returns></returns> public void GetAuthUrl(string appid, string redirect_url) { Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=123#wechat_redirect", appid, Utils.UrlEncode(redirect_url))); } }</string>
登录后复制
WxPay.ashx代码:
/// <summary> /// WxPay 的摘要说明 /// </summary> public class WxPay : IHttpHandler { /// <summary> /// 公众账号ID /// </summary> private string appid = "wxxxxxxxxxxxxxxxx"; /// <summary> /// 商户号 /// </summary> private string mch_id = "12333333333"; /// <summary> /// 通知url /// </summary> private string notify_url = "http://www.china101.net/Notify2.aspx"; /// <summary> /// 密钥 /// </summary> private string key = "chinapagexxxxxxxxxxxxx"; public void ProcessRequest(HttpContext context) { string action = JKRequest.GetQueryString("action"); switch (action) { case "unifysign": GetUnifySign(context); break; case "jspayparam": GetJsPayParam(context); break; case "nativedynamic": GetPayQr(context); break; } } #region 获取js支付参数 void GetJsPayParam(HttpContext context) { JsEntities jsEntities = new JsEntities() { appId = appid, nonceStr = .Utils.GetRandom(), package = string.Format("prepay_id={0}", GetPrepayId(context)), signType = "MD5", timeStamp = .Utils.ConvertDateTimeInt(DateTime.Now).ToString() }; string url, sign; string xmlStr = .Utils.GetUnifyRequestXml<jsentities>(jsEntities, key, out url, out sign); LogHelper.WriteFile(xmlStr); jsEntities.paySign = sign; context.Response.Write(JsonConvert.SerializeObject(jsEntities)); } #endregion #region 获取预支付ID //-------------------------------------------------------------------------- string GetPrepayId(HttpContext context) { string xml; GetUnifySign(context, out xml); LogHelper.WriteFile("GetPrepayId---71--" + xml); UnifyReceive unifyReceive = new UnifyReceive(.Utils.HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder", xml)); LogHelper.WriteFile("unifyReceive---73--" + unifyReceive.prepay_id); return unifyReceive.prepay_id; } #endregion #region 获取统一签名 void GetUnifySign(HttpContext context) { string xml; context.Response.Write(GetUnifySign(context, out xml)); } #endregion #region 获取统一签名 string GetUnifySign(HttpContext context, out string xml) { string url, sign; xml = WxPayHelper.Utils.GetUnifyUrlXml<unifyentities>(GetUnifyEntities(context), key, out url, out sign); return sign; } #endregion #region 获取二维码 void GetPayQr(HttpContext context) { string url = GetPayUrl(context); WxPayHelper.Utils.GetQrCode(url); } #endregion #region 获取二维码链接 string GetPayUrl(HttpContext context) { string xml; GetUnifySign(context, out xml); WxPayHelper.Utils.WriteTxt(xml); UnifyReceive unifyReceive = new UnifyReceive(WxPayHelper.Utils.HttpPost("https://api.mch.weixin.qq.com/pay/unifiedorder", xml)); return unifyReceive.code_url; } #endregion #region 获取统一支付接口参数对象 UnifyEntities GetUnifyEntities(HttpContext context) { string msgid = JKRequest.GetQueryString("msgid"); LogHelper.WriteFile("115---------"+msgid); UnifyEntities unify = new UnifyEntities { appid = appid, body = JKRequest.GetQueryString("body"), mch_id = mch_id, nonce_str = WxPayHelper.Utils.GetRandom(), out_trade_no = JKRequest.GetQueryString("out_trade_no"), notify_url = notify_url, spbill_create_ip = JKRequest.GetIP(), trade_type = JKRequest.GetQueryString("trade_type"), total_fee = JKRequest.GetQueryString("total_fee") }; if (unify.trade_type == "NATIVE") { unify.product_id = msgid; } else { unify.openid = msgid; } return unify; } #endregion public bool IsReusable { get { return false; } } }</unifyentities></jsentities>
登录后复制
Utils.cs代码:
public class Utils { #region MD5加密 public static string MD5(string pwd) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] data = System.Text.Encoding.Default.GetBytes(pwd); byte[] md5data = md5.ComputeHash(data); md5.Clear(); string str = ""; for (int i = 0; i /// 获取文件的md5 /// /// <param>文件路径,url路径 /// <returns>md5字符串</returns> string GetMD5HashFromFile(string filepath) { try { WebClient wc = new WebClient(); var file = wc.OpenRead(new Uri(filepath)); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i /// 判断对象是否为Int32类型的数字 /// /// <param> /// <returns></returns> public static bool IsNumeric(object expression) { if (expression != null) return IsNumeric(expression.ToString()); return false; } /// <summary> /// 判断对象是否为Int32类型的数字 /// </summary> /// <param> /// <returns></returns> public static bool IsNumeric(string expression) { if (expression != null) { string str = expression; if (str.Length > 0 && str.Length /// 是否为Double类型 /// /// <param> /// <returns></returns> public static bool IsDouble(object expression) { if (expression != null) return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(.w*)?$"); return false; } /// <summary> /// 检测是否符合email格式 /// </summary> /// <param>要判断的email字符串 /// <returns>判断结果</returns> public static bool IsValidEmail(string strEmail) { return Regex.IsMatch(strEmail, @"^[w.]+([-]w+)*@[A-Za-z0-9-_]+[.][A-Za-z0-9-_]"); } public static bool IsValidDoEmail(string strEmail) { return Regex.IsMatch(strEmail, @"^@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$"); } /// <summary> /// 检测是否是正确的Url /// </summary> /// <param>要验证的Url /// <returns>判断结果</returns> public static bool IsURL(string strUrl) { return Regex.IsMatch(strUrl, @"^(http|https)://([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9-]+.)*[a-zA-Z0-9-]+.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(:[0-9]+)*(/($|[a-zA-Z0-9.,?'\+&%$#=~_-]+))*$"); } /// <summary> /// 将字符串转换为数组 /// </summary> /// <param>字符串 /// <returns>字符串数组</returns> public static string[] GetStrArray(string str) { return str.Split(new char[',']); } /// <summary> /// 将数组转换为字符串 /// </summary> /// <param>List /// <param>分隔符 /// <returns>String</returns> public static string GetArrayStr(List<string> list, string speater) { StringBuilder sb = new StringBuilder(); for (int i = 0; i /// object型转换为bool型 /// /// <param>要转换的字符串 /// <param>缺省值 /// <returns>转换后的bool类型结果</returns> public static bool StrToBool(object expression, bool defValue) { if (expression != null) return StrToBool(expression, defValue); return defValue; } /// <summary> /// string型转换为bool型 /// </summary> /// <param>要转换的字符串 /// <param>缺省值 /// <returns>转换后的bool类型结果</returns> public static bool StrToBool(string expression, bool defValue) { if (expression != null) { if (string.Compare(expression, "true", true) == 0) return true; else if (string.Compare(expression, "false", true) == 0) return false; } return defValue; } /// <summary> /// 将对象转换为Int32类型 /// </summary> /// <param>要转换的字符串 /// <param>缺省值 /// <returns>转换后的int类型结果</returns> public static int ObjToInt(object expression, int defValue) { if (expression != null) return StrToInt(expression.ToString(), defValue); return defValue; } /// <summary> /// 将字符串转换为Int32类型 /// </summary> /// <param>要转换的字符串 /// <param>缺省值 /// <returns>转换后的int类型结果</returns> public static int StrToInt(string expression, int defValue) { if (string.IsNullOrEmpty(expression) || expression.Trim().Length >= 11 || !Regex.IsMatch(expression.Trim(), @"^([-]|[0-9])[0-9]*(.w*)?$")) return defValue; int rv; if (Int32.TryParse(expression, out rv)) return rv; return Convert.ToInt32(StrToFloat(expression, defValue)); } /// <summary> /// Object型转换为decimal型 /// </summary> /// <param>要转换的字符串 /// <param>缺省值 /// <returns>转换后的decimal类型结果</returns> public static decimal ObjToDecimal(object expression, decimal defValue) { if (expression != null) return StrToDecimal(expression.ToString(), defValue); return defValue; } /// <summary> /// string型转换为decimal型 /// </summary> /// <param>要转换的字符串 /// <param>缺省值 /// <returns>转换后的decimal类型结果</returns> public static decimal StrToDecimal(string expression, decimal defValue) { if ((expression == null) || (expression.Length > 10)) return defValue; decimal intValue = defValue; if (expression != null) { bool IsDecimal = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(.w*)?$"); if (IsDecimal) decimal.TryParse(expression, out intValue); } return intValue; } /// <summary> /// Object型转换为float型 /// </summary> /// <param>要转换的字符串 /// <param>缺省值 /// <returns>转换后的int类型结果</returns> public static float ObjToFloat(object expression, float defValue) { if (expression != null) return StrToFloat(expression.ToString(), defValue); return defValue; } /// <summary> /// string型转换为float型 /// </summary> /// <param>要转换的字符串 /// <param>缺省值 /// <returns>转换后的int类型结果</returns> public static float StrToFloat(string expression, float defValue) { if ((expression == null) || (expression.Length > 10)) return defValue; float intValue = defValue; if (expression != null) { bool IsFloat = Regex.IsMatch(expression, @"^([-]|[0-9])[0-9]*(.w*)?$"); if (IsFloat) float.TryParse(expression, out intValue); } return intValue; } /// <summary> /// 将对象转换为日期时间类型 /// </summary> /// <param>要转换的字符串 /// <param>缺省值 /// <returns>转换后的int类型结果</returns> public static DateTime StrToDateTime(string str, DateTime defValue) { if (!string.IsNullOrEmpty(str)) { DateTime dateTime; if (DateTime.TryParse(str, out dateTime)) return dateTime; } return defValue; } /// <summary> /// 将对象转换为日期时间类型 /// </summary> /// <param>要转换的字符串 /// <returns>转换后的int类型结果</returns> public static DateTime StrToDateTime(string str) { return StrToDateTime(str, DateTime.Now); } /// <summary> /// 将对象转换为日期时间类型 /// </summary> /// <param>要转换的对象 /// <returns>转换后的int类型结果</returns> public static DateTime ObjectToDateTime(object obj) { return StrToDateTime(obj.ToString()); } /// <summary> /// 将对象转换为日期时间类型 /// </summary> /// <param>要转换的对象 /// <param>缺省值 /// <returns>转换后的int类型结果</returns> public static DateTime ObjectToDateTime(object obj, DateTime defValue) { return StrToDateTime(obj.ToString(), defValue); } /// <summary> /// 将对象转换为字符串 /// </summary> /// <param>要转换的对象 /// <returns>转换后的string类型结果</returns> public static string ObjectToStr(object obj) { if (obj == null) return ""; return obj.ToString().Trim(); } /// <summary> /// 判断是否邮箱 /// </summary> /// <param> /// <returns></returns> public static bool IsEmail(string expression) { return Regex.IsMatch(expression, @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$"); } /// <summary> /// 判断是否手机 /// </summary> /// <param> /// <returns></returns> public static bool IsMobile(string expression) { return Regex.IsMatch(expression, @"^1[3|4|5|6|7|8|9][0-9]{9}$"); } public static bool IsPhone(string telphone) { Regex regex = new Regex(@"^(d{3,4}-)?d{6,8}$"); return regex.IsMatch(telphone); } #endregion #region 分割字符串 /// <summary> /// 分割字符串 /// </summary> public static string[] SplitString(string strContent, string strSplit) { if (!string.IsNullOrEmpty(strContent)) { if (strContent.IndexOf(strSplit) /// 分割字符串 /// /// <returns></returns> public static string[] SplitString(string strContent, string strSplit, int count) { string[] result = new string[count]; string[] splited = SplitString(strContent, strSplit); for (int i = 0; i /// 删除最后结尾的一个逗号 /// public static string DelLastComma(string str) { if (str.Length /// 删除最后结尾的指定字符后的字符 /// public static string DelLastChar(string str, string strchar) { if (string.IsNullOrEmpty(str)) return ""; if (str.LastIndexOf(strchar) >= 0 && str.LastIndexOf(strchar) == str.Length - 1) { return str.Substring(0, str.LastIndexOf(strchar)); } return str; } #endregion #region 生成指定长度的字符串 /// <summary> /// 生成指定长度的字符串,即生成strLong个str字符串 /// </summary> /// <param>生成的长度 /// <param>以str生成字符串 /// <returns></returns> public static string StringOfChar(int strLong, string str) { string ReturnStr = ""; for (int i = 0; i /// 生成日期随机码 /// /// <returns></returns> public static string GetRamCode() { #region return DateTime.Now.ToString("yyyyMMddHHmmssffff"); #endregion } #endregion #region 生成随机字母或数字 /// <summary> /// 生成随机数字 /// </summary> /// <param>生成长度 /// <returns></returns> public static string Number(int Length) { return Number(Length, false); } /// <summary> /// 生成随机数字 /// </summary> /// <param>生成长度 /// <param>是否要在生成前将当前线程阻止以避免重复 /// <returns></returns> public static string Number(int Length, bool Sleep) { if (Sleep) System.Threading.Thread.Sleep(3); string result = ""; System.Random random = new Random(); for (int i = 0; i /// 生成随机字母字符串(数字字母混和) /// /// <param>待生成的位数 public static string GetCheckCode(int codeCount) { string str = string.Empty; int rep = 0; long num2 = DateTime.Now.Ticks + rep; rep++; Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep))); for (int i = 0; i /// 根据日期和随机码生成订单号 /// /// <returns></returns> public static string GetOrderNumber() { string num = DateTime.Now.ToString("yyMMddHHmmss");//yyyyMMddHHmmssms return num + Number(2).ToString(); } private static int Next(int numSeeds, int length) { byte[] buffer = new byte[length]; System.Security.Cryptography.RNGCryptoServiceProvider Gen = new System.Security.Cryptography.RNGCryptoServiceProvider(); Gen.GetBytes(buffer); uint randomResult = 0x0;//这里用uint作为生成的随机数 for (int i = 0; i /// 截取字符长度 /// /// <param>字符 /// <param>长度 /// <returns></returns> public static string CutString(string inputString, int len) { if (string.IsNullOrEmpty(inputString)) return ""; inputString = DropHTML(inputString); ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; string tempString = ""; byte[] s = ascii.GetBytes(inputString); for (int i = 0; i len) break; } //如果截过则加上半个省略号 byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString); if (mybyte.Length > len) tempString += "…"; return tempString; } #endregion #region 清除HTML标记 public static string DropHTML(string Htmlstring) { if (string.IsNullOrEmpty(Htmlstring)) return ""; //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"<script>]*?>.*?</script>", "", RegexOptions.IgnoreCase); //删除HTML Htmlstring = Regex.Replace(Htmlstring, @"]*)>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"([ ])[s]+", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"</string>
登录后复制
以上就是用ASP.NET实现JS-SDK支付的实例详解的详细内容,更多请关注GTHOST其它相关文章!