C#支付宝扫码支付代码完整版
来源:本站原创|时间:2020-01-10|栏目:C#教程|点击: 次
本文实例为大家分享了C#支付宝扫码支付示的具体代码,供大家参考,具体内容如下
支付宝工具类
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Web; namespace ZK_ECommerce.Shop.pay.alipay { public class AliPay { public static string GetMD5(string s, string _input_charset) { /// <summary> /// 与ASP兼容的MD5加密算法 /// </summary> MD5 md5 = new MD5CryptoServiceProvider(); byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s)); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < t.Length; i++) { sb.Append(t[i].ToString("x").PadLeft(2, '0')); } return sb.ToString(); } public static string[] BubbleSort(string[] r) { /// <summary> /// 冒泡排序法 /// </summary> int i, j; //交换标志 string temp; bool exchange; for (i = 0; i < r.Length; i++) //最多做R.Length-1趟排序 { exchange = false; //本趟排序开始前,交换标志应为假 for (j = r.Length - 2; j >= i; j--) { if (System.String.CompareOrdinal(r[j + 1], r[j]) < 0) //交换条件 { temp = r[j + 1]; r[j + 1] = r[j]; r[j] = temp; exchange = true; //发生了交换,故将交换标志置为真 } } if (!exchange) //本趟排序未发生交换,提前终止算法 { break; } } return r; } public string CreatUrl( string gateway, string service, string partner, string sign_type, string out_trade_no, string subject, string body, string payment_type, string total_fee, string show_url, string seller_email, string key, string return_url, string _input_charset, string notify_url, string extra_common_param = "" ) { return CreatUrl( gateway, service, partner, sign_type, out_trade_no, subject, body, payment_type, total_fee, show_url, seller_email, key, return_url, _input_charset, notify_url, "", extra_common_param); } public string CreatUrl( string gateway, string service, string partner, string sign_type, string out_trade_no, string subject, string body, string payment_type, string total_fee, string show_url, string seller_email, string key, string return_url, string _input_charset, string notify_url, string token, string extra_common_param = "" ) { /// <summary> ///2015年11月17日14:08:00 /// </summary> int i; string[] Oristr; //构造数组; if (!string.IsNullOrEmpty(token)) { if (extra_common_param == "COD2MOTOPAY") { Oristr = new[]{ "service="+service, "partner=" + partner, "subject=" + subject, "body=" + body, "out_trade_no=" + out_trade_no, "total_fee=" + total_fee, "show_url=" + show_url, "payment_type=" + payment_type, "seller_email=" + seller_email, "notify_url=" + notify_url, "_input_charset="+_input_charset, "return_url=" + return_url, "token="+token, "extra_common_param="+extra_common_param }; } else { Oristr = new[]{ "service="+service, "partner=" + partner, "subject=" + subject, "body=" + body, "out_trade_no=" + out_trade_no, "total_fee=" +total_fee, "show_url=" + show_url, "payment_type=" + payment_type, "seller_email=" + seller_email, "notify_url=" + notify_url, "_input_charset="+_input_charset, "return_url=" + return_url, "token="+token }; } } else { if (extra_common_param == "COD2MOTOPAY") { Oristr = new[]{ "service="+service, "partner=" + partner, "subject=" + subject, "body=" + body, "out_trade_no=" + out_trade_no, "total_fee="+total_fee, "show_url=" + show_url, "payment_type=" + payment_type, "seller_email=" + seller_email, "notify_url=" + notify_url, "_input_charset="+_input_charset, "return_url=" + return_url, "extra_common_param="+extra_common_param }; } else { Oristr = new[]{ "service="+service, "partner=" + partner, "subject=" + subject, "body=" + body, "out_trade_no=" + out_trade_no, "total_fee=" + total_fee, "show_url=" + show_url, "payment_type=" + payment_type, "seller_email=" + seller_email, "notify_url=" + notify_url, "_input_charset="+_input_charset, "return_url=" + return_url }; } } //进行排序; string[] Sortedstr = BubbleSort(Oristr); //构造待md5摘要字符串 ; StringBuilder prestr = new StringBuilder(); for (i = 0; i < Sortedstr.Length; i++) { if (i == Sortedstr.Length - 1) { prestr.Append(Sortedstr[i]); } else { prestr.Append(Sortedstr[i] + "&"); } } prestr.Append(key); //生成Md5摘要; string sign = GetMD5(prestr.ToString(), _input_charset); //构造支付Url; char[] delimiterChars = { '=' }; StringBuilder parameter = new StringBuilder(); parameter.Append(gateway); for (i = 0; i < Sortedstr.Length; i++) { parameter.Append(Sortedstr[i].Split(delimiterChars)[0] + "=" + HttpUtility.UrlEncode(Sortedstr[i].Split(delimiterChars)[1]) + "&"); } parameter.Append("sign=" + sign + "&sign_type=" + sign_type); //返回支付Url; return parameter.ToString(); } public string CreatUrl_MissPayments( string gateway, string service, string partner, string sign_type, string out_trade_no, string key, string _input_charset ) { /// <summary> /// created by sunzhizhi 2006.5.21,sunzhizhi@msn.com。 /// </summary> int i; //构造数组; string[] Oristr ={ "service="+service, "partner=" + partner, "out_trade_no=" + out_trade_no, "_input_charset="+_input_charset }; //进行排序; string[] Sortedstr = BubbleSort(Oristr); //构造待md5摘要字符串 ; StringBuilder prestr = new StringBuilder(); for (i = 0; i < Sortedstr.Length; i++) { if (i == Sortedstr.Length - 1) { prestr.Append(Sortedstr[i]); } else { prestr.Append(Sortedstr[i] + "&"); } } prestr.Append(key); //生成Md5摘要; string sign = GetMD5(prestr.ToString(), _input_charset); //构造支付Url; char[] delimiterChars = { '=' }; StringBuilder parameter = new StringBuilder(); parameter.Append(gateway); for (i = 0; i < Sortedstr.Length; i++) { parameter.Append(Sortedstr[i].Split(delimiterChars)[0] + "=" + HttpUtility.UrlEncode(Sortedstr[i].Split(delimiterChars)[1]) + "&"); } parameter.Append("sign=" + sign + "&sign_type=" + sign_type); //返回支付Url; return parameter.ToString(); } public static string GetTranSign(string TranData) { if (string.IsNullOrWhiteSpace(TranData)) return null; string sign = GetMD5(TranData + "alskdjfaow;fjel;asdjf", "utf-8"); return sign; } } }
页面处理
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Security.Cryptography; using System.Text; using System.Web; using System.Web.Mvc; using ZK_ECommerce.Shop.pay.alipay; namespace Alipay.Controllers { public class ApliayDemoController : Controller { /// <summary> /// 支付页面 /// </summary> /// <param name="ordercode"></param> /// <returns></returns> public ActionResult Index(string ordercode) { string out_trade_no = ordercode; string gateway = "https://mapi.alipay.com/gateway.do?"; //支付接口 string service = "create_direct_pay_by_user"; string partner = ""; //partner合作伙伴ID保留字段 string sign_type = "MD5"; string payment_type = "1"; //支付类型 string show_url = "http://www.letao.com"; string seller_email = ""; //卖家账号 string key = ""; //partner账户的支付宝安全校验码 string return_url = "http://localhost:1396/ApliayDemo/ApliyReturn"; //服务器通知返回接口 string notify_url = "http://localhost:1396/ApliayDemo/AlipayNotify"; //服务器通知返回接口 string _input_charset = "utf-8"; string extra_common_param = "COD2MOTOPAY";//表示快捷支付 string token = string.Empty; if (string.IsNullOrEmpty(token)) token = ""; string subject = "康康体检套餐"; // subject商品名称 string body = "测试商品"; //商品描述 string total_fee = "0.01"; AliPay ap = new AliPay(); string aliay_url = ap.CreatUrl(gateway, service, partner, sign_type, out_trade_no, subject, body, payment_type, total_fee, show_url, seller_email, key, return_url, _input_charset, notify_url, token, extra_common_param); return Redirect(aliay_url); } /// <summary> /// 返回通知页面 /// </summary> /// <returns></returns> public ActionResult ApliyReturn() { string alipayNotifyURL = "https://mapi.alipay.com/gateway.do?"; string key = ""; //partner 的对应交易安全校验码(必须填写) string _input_charset = "utf-8"; string partner = ""; //partner合作伙伴id(必须填写) alipayNotifyURL = alipayNotifyURL + "service=notify_verify" + "&partner=" + partner + "¬ify_id=" + Request.QueryString["notify_id"]; //获取支付宝ATN返回结果,true是正确的订单信息,false 是无效的 string responseTxt = Get_Http(alipayNotifyURL, 120000); int i; NameValueCollection coll; coll = Request.QueryString; String[] requestarr = coll.AllKeys; //进行排序; string[] Sortedstr = BubbleSort(requestarr); //构造待md5摘要字符串 ; StringBuilder prestr = new StringBuilder(); for (i = 0; i < Sortedstr.Length; i++) { if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type") { if (i == Sortedstr.Length - 1) { prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]]); } else { prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]] + "&"); } } } prestr.Append(key); string mysign = GetMD5(prestr.ToString(), _input_charset); string sign = Request.QueryString["sign"]; string trade_status = Request.QueryString["trade_status"]; if (mysign == sign && responseTxt == "true" && trade_status == "TRADE_SUCCESS") //验证支付发过来的消息,签名是否正确 { string order_amount = Request.QueryString["total_fee"].ToString().Trim(); string order_code = Request.QueryString["out_trade_no"].ToString().Trim(); //updateOrder(order_code);//更新订单状态 Response.Redirect(""); //跳转到支付成功页面 } else { Response.Write("支付失败。"); Response.Write("<br>Result:responseTxt=" + responseTxt); Response.Write("<br>Result:mysign=" + mysign); Response.Write("<br>Result:sign=" + sign); } return View(); } /// <summary> /// 支付回调页面 /// </summary> /// <returns></returns> public ActionResult AlipayNotify() { string alipayNotifyURL = "https://mapi.alipay.com/gateway.do?"; string partner = ""; //partner合作伙伴id(必须填写) string key = ""; //partner 的对应交易安全校验码(必须填写) alipayNotifyURL = alipayNotifyURL + "service=notify_verify" + "&partner=" + partner + "¬ify_id=" + Request.Form["notify_id"]; //获取支付宝ATN返回结果,true是正确的订单信息,false 是无效的 string responseTxt = Get_Http(alipayNotifyURL, 120000); int i; NameValueCollection coll; //Load Form variables into NameValueCollection variable. coll = Request.Form; // Get names of all forms into a string array. String[] requestarr = coll.AllKeys; //进行排序; string[] Sortedstr = BubbleSort(requestarr); //构造待md5摘要字符串 ; StringBuilder prestr = new StringBuilder(); for (i = 0; i < Sortedstr.Length; i++) { if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type") { if (i == Sortedstr.Length - 1) { prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]]); } else { prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]] + "&"); } } } prestr.Append(key); string mysign = GetMD5(prestr.ToString()); string sign = Request.QueryString["sign"]; string trade_status = Request.QueryString["trade_status"]; if (mysign == sign && responseTxt == "true" && trade_status == "TRADE_SUCCESS") //验证支付发过来的消息,签名是否正确 { string order_amount = Request.QueryString["total_fee"].ToString().Trim(); string order_code = Request.QueryString["out_trade_no"].ToString().Trim(); // updateOrder(order_code);//更新订单状态 Response.Redirect(""); //跳转到支付成功页面 } else { Response.Write("支付失败。"); Response.Write("<br>Result:responseTxt=" + responseTxt); Response.Write("<br>Result:mysign=" + mysign); Response.Write("<br>Result:sign=" + sign); } return View(); } public static string GetMD5(string s, string _input_charset) { /// <summary> /// 与ASP兼容的MD5加密算法 /// </summary> MD5 md5 = new MD5CryptoServiceProvider(); byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s)); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < t.Length; i++) { sb.Append(t[i].ToString("x").PadLeft(2, '0')); } return sb.ToString(); } public static string GetMD5(string s) { /// <summary> /// 与ASP兼容的MD5加密算法 /// </summary> MD5 md5 = new MD5CryptoServiceProvider(); byte[] t = md5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(s)); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < t.Length; i++) { sb.Append(t[i].ToString("x").PadLeft(2, '0')); } return sb.ToString(); } public static string[] BubbleSort(string[] r) { /// <summary> /// 冒泡排序法 /// </summary> int i, j; //交换标志 string temp; bool exchange; for (i = 0; i < r.Length; i++) //最多做R.Length-1趟排序 { exchange = false; //本趟排序开始前,交换标志应为假 for (j = r.Length - 2; j >= i; j--) { if (System.String.CompareOrdinal(r[j + 1], r[j]) < 0) //交换条件 { temp = r[j + 1]; r[j + 1] = r[j]; r[j] = temp; exchange = true; //发生了交换,故将交换标志置为真 } } if (!exchange) //本趟排序未发生交换,提前终止算法 { break; } } return r; } //获取远程服务器ATN结果 public String Get_Http(String a_strUrl, int timeout) { string strResult; try { HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(a_strUrl); myReq.Timeout = timeout; HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse(); Stream myStream = HttpWResp.GetResponseStream(); StreamReader sr = new StreamReader(myStream, Encoding.Default); StringBuilder strBuilder = new StringBuilder(); while (-1 != sr.Peek()) { strBuilder.Append(sr.ReadLine()); } strResult = strBuilder.ToString(); } catch (Exception exp) { strResult = "错误:" + exp.Message; } return strResult; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
您可能感兴趣的文章
- 01-10C#一个简单的定时小程序实现代码
- 01-10C#获取网页源代码的方法
- 01-1010个C#程序员经常用到的实用代码片段
- 01-10C#导出网站功能实例代码讲解
- 01-10C#代码实现PDF文档操作类
- 01-10C#超实用代码段合集
- 01-10分享WCF聊天程序--WCFChat实现代码
- 01-10Silverlight将图片转换为byte的实现代码
- 01-10使用C#代码获取存储过程返回值
- 01-10C#代码实现对AES加密解密
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10delphi制作wav文件的方法
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10C#中split用法实例总结
- 04-02jquery与jsp,用jquery
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-11ajax实现页面的局部加载
- 01-10SublimeText编译C开发环境设置
- 01-10使用C语言求解扑克牌的顺子及n个骰子