asp.net网站集成支付宝接口,支付成功后收不到通知
这是Alipay_Return.aspx.csnamespace
{
public partial class Alipay_Return : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "支付结果";
string alipayNotifyURL = "https://www.
string key = "自己的key"; //partner 的对应交易安全校验码(必须填写)
string partner = "自己的partner"; //partner合作伙伴id(必须填写)
string _input_charset = "utf-8";//编码类型,完全根据客户自身的项目的编码格式而定,千万不要填错。否则极其容易造成MD5加密错误。
alipayNotifyURL = alipayNotifyURL + "&partner=" + partner + "¬ify_id=" + Request.QueryString["notify_id"];
//获取支付宝ATN返回结果,true是正确的订单信息,false 是无效的
string responseTxt = AliPay.Get_Http(alipayNotifyURL, 120000);
int i;
NameValueCollection coll;
//Load Form variables into NameValueCollection variable.
coll = Request.QueryString;
// Get names of all forms into a string array.
String[] requestarr = coll.AllKeys;
//进行排序;
string[] Sortedstr = AliPay.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);
//生成Md5摘要;
string mysign = AliPay.GetMD5(prestr.ToString(), _input_charset);
//*******加密签名程序结束*******
string sign = Request.QueryString["sign"];
if (mysign == sign && responseTxt == "true")
{
//更新自己数据库的订单语句,请自己填写一下
string strOrderNO = Request.QueryString["out_trade_no"].ToString();//订单号
string OrderNo = strOrderNO.Substring(strOrderNO.IndexOf('m') + 1);
int orderNo = Convert.ToInt32(OrderNo);
UserInfoCom userInfoCom = UserInfoComManager.GetUserInfoByUserIDCom(Convert.ToInt32(orderNo));
if (userInfoCom.Active == 1)
{
Response.Write("success");
return;
}
}
}
}
这是Alipay_Notify.aspx.cs
namespace
{
public partial class Alipay_Notify : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string alipayNotifyURL = "http://notify.
string alipayNotifyURL = "https://www.
string partner = "自己的ID"; //partner合作伙伴id(必须填写)
string key = "自己的Partner"; //partner 的对应交易安全校验码(必须填写)
string _input_charset = "utf-8";//编码类型,完全根据客户自身的项目的编码格式而定,千万不要填错。否则极其容易造成MD5加密错误。
alipayNotifyURL = alipayNotifyURL + "&partner=" + partner + "¬ify_id=" + Request.Form["notify_id"];
////获取支付宝ATN返回结果,true是正确的订单信息,false 是无效的
string responseTxt = AliPay.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 = AliPay.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.Form[Sortedstr[i]]);
}
else
{
prestr.Append(Sortedstr[i] + "=" + Request.Form[Sortedstr[i]] + "&");
}
}
}
prestr.Append(key);
string mysign = AliPay.GetMD5(prestr.ToString(), _input_charset);
//*******加密签名程序结束*******
string sign = Request.Form["sign"];
if (mysign == sign && responseTxt == "true")
{
//更新自己数据库的订单语句,请自己填写一下
string strOrderNO = Request.Form["out_trade_no"];//订单号
string OrderNo = strOrderNO.Substring(strOrderNO.IndexOf('m') + 1);
int orderNo = Convert.ToInt32(OrderNo);
UserInfoCom userInfoCom = UserInfoComManager.GetUserInfoByUserIDCom(Convert.ToInt32(orderNo));
}
}
}
}
这里AliPay里面的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using
using
namespace TestDaoDui.BLL
{
/// <summary>
/// created by sunzhizhi 2006.5.21,sunzhizhi@。
/// </summary>
public class AliPay
{
public AliPay()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 与ASP兼容的MD5加密算法
/// </summary>
public static string GetMD5(string s, string _input_charset)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] t = (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();
}
/// <summary>
/// 冒泡排序法
/// 按照字母序列从a到z的顺序排列
/// </summary>
public static string[] BubbleSort(string[] r)
{
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.(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;
}
/// <summary>
/// 生成URL链接或加密结果
/// </summary>
/// <param name="para">参数加密数组</param>
/// <param name="_input_charset">编码格式</param>
/// <param name="sign_type">加密类型</param>
/// <param name="key">安全校验码</param>
/// <returns>字符串URL或加密结果</returns>
public static string CreatUrl(
//string gateway,//GET方式传递参数时请去掉注释
string[] para,
string _input_charset,
string sign_type,
string key
)
{
int i;
//进行排序;
string[] Sortedstr = BubbleSort(para);
//构造待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);
//以下是POST方式传递参数
return sign;
//以下是GET方式传递参数
//构造支付Url;
// char[] delimiterChars = { '='};
// StringBuilder parameter = new StringBuilder();
// parameter.Append(gateway);
// for (i = 0; i < Sortedstr.Length; i++)
// {//UTF-8格式的编码转换
// 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();
}
//获取远程服务器ATN结果,验证是否是支付宝服务器发来的请求
public static 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;
}
}
}
因为刚注册所以没有可用分分给大家,我确定,如果我有分以后一定会补给你们的,在这里谢谢大家了
[ 本帖最后由 soular 于 2010-9-26 18:08 编辑 ]