<p>支付宝的接口调用很不方便,刚做好一个封装,实现了虚拟交易和实物交易。
解决方案中有三个项目以及NDoc生成的文档,简单的序列图:CommonAliPay,封装的支付宝接口。
TestAli,asp.net的测试项目
TestCommonAliPay,Nunit的测试项目。
源代码下载地址:http://files.cnblogs.com/bluewater/CommonAliPay.rar</p><div style="page-break-after: always;"><span style="display: none;"><!--more-->& nbsp ;</span></div><p>
调用方法:
1、引入CommonAliPay.dll
2、实现支付宝服务接口的方法调用方式:</p><div> AliPay ap = new AliPay();
string key = "";//填写自己的key
string partner = "";//填写自己的Partner
StandardGoods bp = new StandardGoods("trade_create_by_buyer", partner, key, "MD5", "卡2", Guid.NewGuid().ToString(), 2.551m, 1, "hao_ding2000@yahoo.com.cn", "hao_ding2000@yahoo.com.cn"
, "EMS", 25.00m, "BUYER_PAY","1");
bp.Notify_Url = "http://203.86.79.185/ali/notify.aspx";
ap.CreateStandardTrade("https://www.alipay.com/cooperate/gateway.do", bp, this);</div><p>上面是通用的调用方式。
下面是只支持虚拟货物的方式:</p><div> string key = "";//填写自己的key
string partner = "";//填写自己的Partner
AliPay ap = new AliPay();
DigitalGoods bp = new DigitalGoods("create_digital_goods_trade_p", partner, key, "MD5", "卡2", Guid.NewGuid().ToString(), 2.551m, 1, "hao_ding2000@yahoo.com.cn", "hao_ding2000@yahoo.com.cn");
bp.Notify_Url = "http://203.86.79.185/ali/notify.aspx";
ap.CreateDigitalTrade("https://www.alipay.com/cooperate/gateway.do", bp, this);</div><p>3、实现支付宝通知接口方法的调用(支持虚拟和实物):</p><p> </p><div>protected void Page_Load(object sender, EventArgs e)
<span id="Codehighlighter1_57_576_Open_Text">{
string key = "";//填写自己的key
string partner = "";//填写自己的Partner
AliPay ap = new AliPay();
string notifyid = Request.Form["notify_id"];
Verify v = new Verify("notify_verify", partner, notifyid);
ap.WaitSellerSendGoods+=new AliPay.ProcessNotifyEventHandler(ap_WaitSellerSendGoods);
ap.WaitBuyerPay += new AliPay.ProcessNotifyEventHandler(ap_WaitBuyerPay);
ap.ProcessNotify(this, "https://www.alipay.com/cooperate/gateway.do",key,v, "utf-8");
}</span>
void ap_WaitBuyerPay(object sender, NotifyEventArgs e)
<span id="Codehighlighter1_642_721_Open_Text">{
// //加入自己的处理逻辑
Log4net.log.Error("wait buyer pay fire");
}</span>
private void ap_WaitSellerSendGoods(object sender, NotifyEventArgs e)
<span id="Codehighlighter1_806_887_Open_Text">{
//加入自己的处理逻辑
Log4net.log.Error("WaitSellerSendGoods fire");
}</span></div><p>支付宝的交易状态都被定义成了类似名称的事件。
部分源代码解析:
1、解析Forms集合到NotifyEventArgs类,因为后面此类的数据要用来做MD5Sign,所以所有值类型,不能存在初始值,如:int的0等。因此用Nullable范型。</p><p> </p><p> </p><div> private NotifyEventArgs ParseNotify(NameValueCollection nv, object obj)
<span id="Codehighlighter1_83_1464_Open_Text">{
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo pi in propertyInfos)
<span id="Codehighlighter1_270_1412_Open_Text">{
string v = nv.Get(pi.Name.ToLower());
if (v != null)
<span id="Codehighlighter1_373_1397_Open_Text">{
if (pi.PropertyType == typeof(string))
<span id="Codehighlighter1_454_529_Open_Text">{
pi.SetValue(obj, v, null);
}</span>
else if (pi.PropertyType == typeof(int?))
<span id="Codehighlighter1_613_697_Open_Text">{
pi.SetValue(obj, int.Parse(v), null);
}</span>
else if (pi.PropertyType == typeof(decimal?))
<span id="Codehighlighter1_785_874_Open_Text">{
pi.SetValue(obj, decimal.Parse(v), null);
}</span>
else if (pi.PropertyType == typeof(DateTime?))
<span id="Codehighlighter1_963_1053_Open_Text">{
pi.SetValue(obj, DateTime.Parse(v), null);
}</span>
else if (pi.PropertyType == typeof(bool))
<span id="Codehighlighter1_1137_1223_Open_Text">{
pi.SetValue(obj, bool.Parse(v), null);
}</span>
else
<span id="Codehighlighter1_1270_1379_Open_Text">{
//转型失败会抛出异常
pi.SetValue(obj, v, null);
}</span>
}</span>
}</span>
return (NotifyEventArgs)obj;
}</span></div><p>
2、从类型中获取排序后的参数</p><p> </p><p> </p><div> <span id="Codehighlighter1_1_129_Open_Text">/// <summary>
/// 获取排序后的参数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns></span>
private SortedList<string,string> GetParam(object obj)
<span id="Codehighlighter1_201_954_Open_Text">{
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance);
SortedList<string, string> sortedList = new SortedList<string, string>(StringComparer.CurrentCultureIgnoreCase);
foreach (PropertyInfo pi in propertyInfos)
<span id="Codehighlighter1_533_892_Open_Text">{
if (pi.GetValue(obj, null) != null)
<span id="Codehighlighter1_604_878_Open_Text">{
if (pi.Name == "Sign" || pi.Name == "Sign_Type")
<span id="Codehighlighter1_695_751_Open_Text">{
continue;
}</span>
sortedList.Add(pi.Name.ToLower(), pi.GetValue(obj, null).ToString());
}</span>
}</span>
return sortedList;
}</span></div><p>3、从SortedList中产生参数</p><p> </p><p> </p><div> private string GetUrlParam(SortedList<string, string> sortedList,bool isEncode)
<span id="Codehighlighter1_89_954_Open_Text">{
StringBuilder param = new StringBuilder();
StringBuilder encodeParam = new StringBuilder();
if (isEncode == false)
<span id="Codehighlighter1_254_552_Open_Text">{
foreach (KeyValuePair<string, string> kvp in sortedList)
<span id="Codehighlighter1_346_484_Open_Text">{
string t = string.Format("{0}={1}", kvp.Key, kvp.Value);
param.Append(t + "&");
}</span>
return param.ToString().TrimEnd('&');
}</span>
else
<span id="Codehighlighter1_583_942_Open_Text">{
foreach (KeyValuePair<string, string> kvp in sortedList)
<span id="Codehighlighter1_674_868_Open_Text">{
string et = string.Format("{0}={1}", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value));
encodeParam.Append(et + "&");
}</span>
return encodeParam.ToString().TrimEnd('&');
}</span>
}</span></div><p>下载地址:http://files.cnblogs.com/bluewater/CommonAliPay.rar
因为时间很紧,有些地方还不完善,大家提出意见,有时间我会修改的
可以看:http://www.cnblogs.com/bluewater/archive/2007/07/17/821540.html,新版本</p>
阅读剩余部分 -