支付寶的接口調用很不方便,剛做好一個封裝,實現了虛擬交易和實物交易。
解決方案中有三個項目以及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, "[email protected]", "[email protected]"
, "EMS", 25.00m, "BUYER_PAY","1");
bp.Notify_Url = " 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, "[email protected]", "[email protected]");
bp.Notify_Url = " ap.CreateDigitalTrade("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, " }
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.companysz.com/Files/bluewater/CommonAliPay.rar
新聞熱點
疑難解答