支付寶的接口調用很不方便,剛作好一個封裝,實現了虛擬交易和實物交易。
解決方案中有三個項目以及NDoc生成的文檔,簡單的序列圖:CommonAliPay,封裝的支付寶接口。
TestAli,asp.net的測試項目
TestCommonAliPay,Nunit的測試項目。
源代碼下載地址:http://files.cnblogs.com/bluewater/CommonAliPay.rar
調用方法:
一、引入CommonAliPay.dll
二、實現支付寶服務接口的方法調用方式: git
複製C#代碼保存代碼app
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);
上面是通用的調用方式。
下面是隻支持虛擬貨物的方式: asp.net
複製C#代碼保存代碼oop
string key = "";//填寫本身的key
string partner = "";//填寫本身的PartnerAliPay 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);
三、實現支付寶通知接口方法的調用(支持虛擬和實物): 測試
複製C#代碼保存代碼ui
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");
}
支付寶的交易狀態都被定義成了相似名稱的事件。
部分源代碼解析:
一、解析Forms集合到NotifyEventArgs類,由於後面此類的數據要用來作MD5Sign,因此全部值類型,不能存在初始值,如:int的0等。所以用Nullable範型。 this
複製C#代碼保存代碼spa
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;
}
二、從類型中獲取排序後的參數
/// <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;
}
三、從SortedList中產生參數 .net
複製C#代碼保存代碼code
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 {
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://files.cnblogs.com/bluewater/CommonAliPay.rar
由於時間很緊,有些地方還不完善,你們提出意見,有時間我會修改的
網上贈與服務集成技術文檔V1.35.pdf
http://www.chenjiliang.com/Article/ArticleAttach/84/2123/apply_alipay_donate_service.zip