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

本文介绍了一个针对支付宝接口的封装方案,支持虚拟及实物交易,并提供详细的调用示例和流程说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘引自:http://www.cnblogs.com/bluewater/archive/2006/10/16/530630.html
支付宝的接口调用很不方便,刚做好一个封装,实现了虚拟交易和实物交易。
解决方案中有三个项目以及NDoc生成的文档,简单的序列图:CommonAliPay,封装的支付宝接口。
 TestAli,asp.net的测试项目
TestCommonAliPay,Nunit的测试项目。
源代码下载地址: http://files.cnblogs.com/bluewater/CommonAliPay.rar
调用方法:
1、引入CommonAliPay.dll
2、实现支付宝服务接口的方法调用方式:
None.gif  AliPay ap  =   new  AliPay();
None.gif        
string  key  =   "" ; // 填写自己的key
None.gif
         string  partner  =   "" ; // 填写自己的Partner
None.gif
        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 "
None.gif            , 
" EMS " 25.00m " BUYER_PAY " , " 1 " );
None.gif           bp.Notify_Url 
=   " http://203.86.79.185/ali/notify.aspx " ;
None.gif        ap.CreateStandardTrade(
" https://www.alipay.com/cooperate/gateway.do " , bp,  this );
上面是通用的调用方式。
下面是只支持虚拟货物的方式:
None.gif   string  key  =   "" ; // 填写自己的key
None.gif
         string  partner  =   "" ; // 填写自己的Partner
None.gif
        AliPay ap  =   new  AliPay();
None.gif        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 " );
None.gif        bp.Notify_Url 
=   " http://203.86.79.185/ali/notify.aspx " ;
None.gif        ap.CreateDigitalTrade(
" https://www.alipay.com/cooperate/gateway.do " , bp,  this );
3、实现支付宝通知接口方法的调用(支持虚拟和实物):
None.gif protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif       
InBlock.gif        
string key = "";//填写自己的key
InBlock.gif
        string partner = "";//填写自己的Partner
InBlock.gif
         AliPay ap = new AliPay();
InBlock.gif         
string notifyid = Request.Form["notify_id"];
InBlock.gif         Verify v 
= new Verify("notify_verify", partner, notifyid);
InBlock.gif        ap.WaitSellerSendGoods
+=new AliPay.ProcessNotifyEventHandler(ap_WaitSellerSendGoods);
InBlock.gif        ap.WaitBuyerPay 
+= new AliPay.ProcessNotifyEventHandler(ap_WaitBuyerPay);
InBlock.gif        ap.ProcessNotify(
this"https://www.alipay.com/cooperate/gateway.do",key,v, "utf-8");
ExpandedBlockEnd.gif    }

None.gif
None.gif    
void  ap_WaitBuyerPay( object  sender, NotifyEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
// //加入自己的处理逻辑
InBlock.gif
        Log4net.log.Error("wait buyer pay fire");
ExpandedBlockEnd.gif    }

None.gif
None.gif   
None.gif    
private   void  ap_WaitSellerSendGoods( object  sender, NotifyEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
//加入自己的处理逻辑
InBlock.gif
        Log4net.log.Error("WaitSellerSendGoods fire");
ExpandedBlockEnd.gif    }
支付宝的交易状态都被定义成了类似名称的事件。
部分源代码解析:
1、解析Forms集合到NotifyEventArgs类,因为后面此类的数据要用来做MD5Sign,所以所有值类型,不能存在初始值,如:int的0等。因此用Nullable范型。
None.gif     private  NotifyEventArgs ParseNotify(NameValueCollection nv,  object  obj)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            PropertyInfo[] propertyInfos 
= obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
InBlock.gif
InBlock.gif            
foreach (PropertyInfo pi in propertyInfos)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string v = nv.Get(pi.Name.ToLower());
InBlock.gif                
if (v != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (pi.PropertyType == typeof(string))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif
InBlock.gif                        pi.SetValue(obj, v, 
null);
InBlock.gif
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (pi.PropertyType == typeof(int?))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        pi.SetValue(obj, 
int.Parse(v), null);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (pi.PropertyType == typeof(decimal?))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif
InBlock.gif                        pi.SetValue(obj, 
decimal.Parse(v), null);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (pi.PropertyType == typeof(DateTime?))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif
InBlock.gif                        pi.SetValue(obj, DateTime.Parse(v), 
null);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else if (pi.PropertyType == typeof(bool))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif
InBlock.gif                        pi.SetValue(obj, 
bool.Parse(v), null);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
//转型失败会抛出异常
InBlock.gif
                        pi.SetValue(obj, v, null);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return (NotifyEventArgs)obj;
InBlock.gif
ExpandedBlockEnd.gif        }

2、从类型中获取排序后的参数
ExpandedBlockStart.gif ContractedBlock.gif   /**/ /// <summary>
InBlock.gif        
/// 获取排序后的参数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="obj"></param>
ExpandedBlockEnd.gif        
/// <returns></returns>

None.gif          private  SortedList < string , string >  GetParam( object  obj)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
InBlock.gif            PropertyInfo[] propertyInfos 
= obj.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance);          
InBlock.gif            SortedList
<stringstring> sortedList = new SortedList<stringstring>(StringComparer.CurrentCultureIgnoreCase);
InBlock.gif            
foreach (PropertyInfo pi in propertyInfos)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
if (pi.GetValue(obj, null!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (pi.Name == "Sign" || pi.Name == "Sign_Type")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
continue;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    sortedList.Add(pi.Name.ToLower(), pi.GetValue(obj, 
null).ToString());
InBlock.gif                  
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return sortedList;
InBlock.gif                    
ExpandedBlockEnd.gif        }
3、从SortedList中产生参数
None.gif   private   string  GetUrlParam(SortedList < string string >  sortedList, bool  isEncode)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            StringBuilder param 
= new StringBuilder();
InBlock.gif            StringBuilder encodeParam 
= new StringBuilder();
InBlock.gif            
if (isEncode == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
foreach (KeyValuePair<stringstring> kvp in sortedList)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string t = string.Format("{0}={1}", kvp.Key, kvp.Value);
InBlock.gif                    param.Append(t 
+ "&");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return param.ToString().TrimEnd('&');
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach (KeyValuePair<stringstring> kvp in sortedList)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                     
string et = string.Format("{0}={1}", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value));
InBlock.gif                     encodeParam.Append(et 
+ "&");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return encodeParam.ToString().TrimEnd('&');
ExpandedSubBlockEnd.gif            }

InBlock.gif 
ExpandedBlockEnd.gif        }
下载地址: http://files.cnblogs.com/bluewater/CommonAliPay.rar
因为时间很紧,有些地方还不完善,大家提出意见,有时间我会修改的

转载于:https://www.cnblogs.com/pojianhuadie/archive/2006/10/17/531398.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值