当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  支付宝接口(刚完成,应该是目前最好的了)

 支付宝接口(刚完成,应该是目前最好的了)

点击次数:27 次 发布日期:2008-11-26 10:48:05 作者:源代码网
源代码网推荐      支付宝的接口调用很不方便,刚做好一个封装,实现了虚拟交易和实物交易。
源代码网推荐  解决方案中有三个项目以及NDoc生成的文档,简单的序列图:CommonAliPay,封装的支付宝接口。
源代码网推荐   TestAli,asp.net的测试项目
源代码网推荐  TestCommonAliPay,Nunit的测试项目。
源代码网推荐  调用方法:
源代码网推荐  1、引入CommonAliPay.dll
源代码网推荐  2、实现支付宝服务接口的方法调用方式:
源代码网推荐   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);上面是通用的调用方式。
源代码网推荐  下面是只支持虚拟货物的方式:
源代码网推荐   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);3、实现支付宝通知接口方法的调用(支持虚拟和实物):
源代码网推荐  protected void Page_Load(object sender, EventArgs e)
源代码网推荐   {
源代码网推荐  
源代码网推荐   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");
源代码网推荐   }
源代码网推荐  
源代码网推荐   void ap_WaitBuyerPay(object sender, NotifyEventArgs e)
源代码网推荐   {
源代码网推荐   // //加入自己的处理逻辑
源代码网推荐   Log4net.log.Error("wait buyer pay fire");
源代码网推荐   }
源代码网推荐  
源代码网推荐  
源代码网推荐   private void ap_WaitSellerSendGoods(object sender, NotifyEventArgs e)
源代码网推荐   {
源代码网推荐   //加入自己的处理逻辑
源代码网推荐   Log4net.log.Error("WaitSellerSendGoods fire");
源代码网推荐   }支付宝的交易状态都被定义成了类似名称的事件。
源代码网推荐  部分源代码解析:
源代码网推荐  1、解析Forms集合到NotifyEventArgs类,因为后面此类的数据要用来做MD5Sign,所以所有值类型,不能存在初始值,如:int的0等。因此用Nullable范型。
源代码网推荐   private NotifyEventArgs ParseNotify(NameValueCollection nv, object obj)
源代码网推荐   {
源代码网推荐   PropertyInfo[] propertyInfos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
源代码网推荐  
源代码网推荐   foreach (PropertyInfo pi in propertyInfos)
源代码网推荐   {
源代码网推荐   string v = nv.Get(pi.Name.ToLower());
源代码网推荐   if (v != null)
源代码网推荐   {
源代码网推荐   if (pi.PropertyType == typeof(string))
源代码网推荐   {
源代码网推荐  
源代码网推荐   pi.SetValue(obj, v, null);
源代码网推荐  
源代码网推荐   }
源代码网推荐   else if (pi.PropertyType == typeof(int?))
源代码网推荐   {
源代码网推荐   pi.SetValue(obj, int.Parse(v), null);
源代码网推荐   }
源代码网推荐   else if (pi.PropertyType == typeof(decimal?))
源代码网推荐   {
源代码网推荐  
源代码网推荐   pi.SetValue(obj, decimal.Parse(v), null);
源代码网推荐   }
源代码网推荐   else if (pi.PropertyType == typeof(DateTime?))
源代码网推荐   {
源代码网推荐  
源代码网推荐   pi.SetValue(obj, DateTime.Parse(v), null);
源代码网推荐   }
源代码网推荐   else if (pi.PropertyType == typeof(bool))
源代码网推荐   {
源代码网推荐  
源代码网推荐   pi.SetValue(obj, bool.Parse(v), null);
源代码网推荐   }
源代码网推荐   else
源代码网推荐   {
源代码网推荐   //转型失败会抛出异常
源代码网推荐   pi.SetValue(obj, v, null);
源代码网推荐   }
源代码网推荐   }
源代码网推荐  
源代码网推荐   }
源代码网推荐   return (NotifyEventArgs)obj;
源代码网推荐  
源代码网推荐   }
源代码网推荐  2、从类型中获取排序后的参数
源代码网推荐   /**//// <summary>
源代码网推荐   /// 获取排序后的参数
源代码网推荐   /// </summary>
源代码网推荐   /// <param name="obj"></param>
源代码网推荐   /// <returns></returns>
源代码网推荐   private SortedList<string,string> GetParam(object obj)
源代码网推荐   {
源代码网推荐  
源代码网推荐   PropertyInfo[] propertyInfos = obj.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance);
源代码网推荐   SortedList<string, string> sortedList = new SortedList<string, string>(StringComparer.CurrentCultureIgnoreCase);
源代码网推荐   foreach (PropertyInfo pi in propertyInfos)
源代码网推荐   {
源代码网推荐  
源代码网推荐   if (pi.GetValue(obj, null) != null)
源代码网推荐   {
源代码网推荐   if (pi.Name == "Sign" || pi.Name == "Sign_Type")
源代码网推荐   {
源代码网推荐   continue;
源代码网推荐   }
源代码网推荐   sortedList.Add(pi.Name.ToLower(), pi.GetValue(obj, null).ToString());
源代码网推荐  
源代码网推荐   }
源代码网推荐   }
源代码网推荐   return sortedList;
源代码网推荐  
源代码网推荐   }3、从SortedList中产生参数
源代码网推荐   private string GetUrlParam(SortedList<string, string> sortedList,bool isEncode)
源代码网推荐   {
源代码网推荐   StringBuilder param = new StringBuilder();
源代码网推荐   StringBuilder encodeParam = new StringBuilder();
源代码网推荐   if (isEncode == false)
源代码网推荐   {
源代码网推荐  
源代码网推荐   foreach (KeyValuePair<string, string> kvp in sortedList)
源代码网推荐   {
源代码网推荐   string t = string.Format("{0}={1}", kvp.Key, kvp.Value);
源代码网推荐   param.Append(t + "&");
源代码网推荐   }
源代码网推荐   return param.ToString().TrimEnd("&");
源代码网推荐   }
源代码网推荐   else
源代码网推荐   {
源代码网推荐   foreach (KeyValuePair<string, string> kvp in sortedList)
源代码网推荐   {
源代码网推荐   string et = string.Format("{0}={1}", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value));
源代码网推荐   encodeParam.Append(et + "&");
源代码网推荐   }
源代码网推荐   return encodeParam.ToString().TrimEnd("&");
源代码网推荐   }
源代码网推荐  
源代码网推荐   }
源代码网推荐  下载地址:http://www.cnblogs.com/Files/bluewater/CommonAliPay.rar
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华