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)
{
/// <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
)
{
/// <summary>
/// created by sunzhizhi 2006.5.21,sunzhizhi@msn.com。
/// </summary>
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 < 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();
}
}