---
title: "支付宝接口小实现例子"
author: "tcl1122"
date: "2010-11-07T15:35:41Z"
tags: ["ASP", "Exchange", "J#", "算法"]
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2010-11-07T15:37:38Z"
id: 804976
url: "https://tcl1122.iteye.com/blog/804976"
---

以下是摘抄的例子，望对你我都有点启发， \[code="java"] protected void Page\_Load(object sender, EventArgs e) { if (!IsPostBack) { //lbl\_Num.Text = Request\["OrderID"].ToString(); //lbl\_Prices.Text = Request\["Money"].ToString(); //按时构造订单号； System.DateTime currentTime = new System.DateTime(); currentTime = System.DateTime.Now; string out\_trade\_no = currentTime.ToString("g"); out\_trade\_no = out\_trade\_no.Replace("-", ""); out\_trade\_no = out\_trade\_no.Replace(":", ""); out\_trade\_no = out\_trade\_no.Replace(" ", ""); lbl\_Num.Text = out\_trade\_no; } } //进入支付宝支付 protected void Button1\_Click(object sender, EventArgs e) { string out\_trade\_no = "2009052610003"; //业务参数赋值； string gateway = "https://www.alipay.com/cooperate/gateway.do?"; //'支付接口 string service = "create\_direct\_pay\_by\_user"; string partner = "2086102833883695"; //partner 合作伙伴ID 保留字段 string sign\_type = "MD5"; string subject = "####购物网商品"; //subject 商品名称 string body = "####购物网"; //body 商品描述 string payment\_type = "1"; //支付类型 string total\_fee = "0.01"; //总金额 0.01～50000.00 string show\_url = "www.163.3com"; string seller\_email = "566666666434@163.com"; //卖家账号 string key = "7xlym97z460sz8psuidrslw7k3o986cz"; //partner账户的支付宝安全校验码 string return\_url = "http://www.163.com/Alipay\_Return.aspx"; //服务器通知返回接口 string notify\_url = "http://www.163.com/Alipay\_Notify.aspx"; //服务器通知返回接口 string \_input\_charset = "utf-8"; 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 ); Response.Redirect(aliay\_url); } //构建AliPay类 public class AliPay { public static string GetMD5(string s, string \_input\_charset) { /// /// 与ASP兼容的MD5加密算法 /// MD5 md5 = new MD5CryptoServiceProvider(); byte\[] t = md5.ComputeHash(Encoding.GetEncoding(\_input\_charset).GetBytes(s)); StringBuilder sb = new StringBuilder(32); for (int i = 0; i &lt; t.Length; i++) { sb.Append(t\[i].ToString("x").PadLeft(2, '0')); } return sb.ToString(); } public static string\[] BubbleSort(string\[] r) { /// /// 冒泡排序法 /// int i, j; //交换标志 string temp; bool exchange; for (i = 0; i &lt; r.Length; i++) //最多做R.Length-1趟排序 { exchange = false; //本趟排序开始前，交换标志应为假 for (j = r.Length - 2; j &gt;= i; j--) { if (System.String.CompareOrdinal(r\[j + 1], r\[j]) &lt; 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 ) { /// /// created by sunzhizhi 2006.5.21,sunzhizhi@msn.com。 /// int i; //构造数组； string\[] Oristr ={ "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 &lt; 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 &lt; 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(); } } \[/code] 以下是另外一位朋友的代码： \[code="c#"] protected void BtnAlipay\_Click(object sender, EventArgs e) { //业务参数赋值； string gateway = "https://www.alipay.com/cooperate/gateway.do?"; //支付接口 string service = "create\_direct\_pay\_by\_user"; //服务名称，这个是识别是何接口实现何功能的标识，请勿修改 string seller\_email = ""; //商家签约时的支付宝帐号，即收款的支付宝帐号 string sign\_type = "MD5"; //加密类型,签名方式“不用改” string key = ""; //安全校验码，与partner是一组，获取方式是：用签约时支付宝帐号登陆支付宝网站www.alipay.com，在商家服务我的商家里即可查到。 string partner = ""; //商户ID，合作身份者ID，合作伙伴ID string \_input\_charset = "utf-8"; //编码类型，完全根据客户自身的项目的编码格式而定，千万不要填错。否则极其容易造成MD5加密错误。 string show\_url = "http://www.alipay.com/"; //展示地址，即在支付页面时，商品名称旁边的“详情”的链接地址。 string out\_trade\_no = TxtOrderno.Text.Trim(); //客户自己的订单号，订单号必须在自身订单系统中保持唯一性 string subject = TxtSubject.Text.Trim(); //商品名称，也可称为订单名称，该接口并不是单一的只能买一样东西，可把一次支付当作一次下订单 string body = TxtBody.Text.Trim(); //商品描述，即备注 string total\_fee = TxtTotal\_fee.Text.Trim(); //商品价格，也可称为订单的总金额 //服务器通知url（Alipay\_Notify.aspx文件所在路经），必须是完整的路径地址 string notify\_url = "http://localhost:8978/direct\_vs2005\_utf/Alipay\_Notify.aspx"; //服务器返回url（Alipay\_Return.aspx文件所在路经），必须是完整的路径地址 string return\_url = "http://localhost:8978/direct\_vs2005\_utf/Alipay\_Return.aspx"; //构造数组； //以下数组即是参与加密的参数，若参数的值不允许为空，若该参数为空，则不要成为该数组的元素 string\[] para ={ "service="+service, "partner=" + partner, "seller\_email=" + seller\_email, "out\_trade\_no=" + out\_trade\_no, "subject=" + subject, "body=" + body, "total\_fee=" + total\_fee, "show\_url=" + show\_url, "payment\_type=1", "notify\_url=" + notify\_url, "return\_url=" + return\_url, "\_input\_charset="+\_input\_charset }; //支付URL生成 string aliay\_url = AliPay.CreatUrl( //gateway,//GET方式传递参数时请去掉注释 para, \_input\_charset, sign\_type, key ); //以下是GET方式传递参数 //Response.Redirect(aliay\_url); //以下是POST方式传递参数 Response.Write("

"); Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); Response.Write("

"); Response.Write(""); } \[/code]