C#支付宝扫码支付代码完整版
程序员文章站
2023-12-15 08:44:10
本文实例为大家分享了c#支付宝扫码支付示的具体代码,供大家参考,具体内容如下
支付宝工具类
using system;
using system.col...
本文实例为大家分享了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; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。