导读:
支付宝的接口调用很不方便,刚做好一个封装,实现了虚拟交易和实物交易。
解决方案中有三个项目以及NDoc生成的文档,简单的序列图:CommonAliPay,封装的支付宝接口。
TestAli,asp.net的测试项目
TestCommonAliPay,Nunit的测试项目。
源代码下载地址:http://www.cnblogs.com/Files/bluewater/CommonAliPay.rar
调用方法: 1、引入CommonAliPay.dll
2、实现支付宝服务接口的方法调用方式:
复制 保存
AliPay ap =
newAliPay();
stringkey =
"";
//填写自己的key stringpartner =
"";
//填写自己的Partner StandardGoods bp =
newStandardGoods(
"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); 上面是通用的调用方式。
下面是只支持虚拟货物的方式:
复制 保存
stringkey =
"";
//填写自己的key stringpartner =
"";
//填写自己的Partner AliPay ap =
newAliPay();
DigitalGoods bp =
newDigitalGoods(
"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、实现支付宝通知接口方法的调用(支持虚拟和实物):
复制 保存
protectedvoidPage_Load(
objectsender, EventArgs e)
{
stringkey =
"";
//填写自己的key stringpartner =
"";
//填写自己的Partner AliPay ap =
newAliPay();
stringnotifyid = Request.Form[
"notify_id"];
Verify v =
newVerify(
"notify_verify", partner, notifyid);
ap.WaitSellerSendGoods +=
newAliPay.ProcessNotifyEventHandler(ap_WaitSellerSendGoods);
ap.WaitBuyerPay +=
newAliPay.ProcessNotifyEventHandler(ap_WaitBuyerPay);
ap.ProcessNotify(
this, "https:
//www.alipay.com/cooperate/gateway.do", key, v, "utf-8"); }
voidap_WaitBuyerPay(
objectsender, NotifyEventArgs e)
{
// //加入自己的处理逻辑 Log4net.log.Error(
"wait buyer pay fire");
}
privatevoidap_WaitSellerSendGoods(
objectsender, NotifyEventArgs e)
{
//加入自己的处理逻辑 Log4net.log.Error(
"WaitSellerSendGoods fire");
}
支付宝的交易状态都被定义成了类似名称的事件。
部分源代码解析: 1、解析Forms集合到NotifyEventArgs类,因为后面此类的数据要用来做MD5Sign,所以所有值类型,不能存在初始值,如:int的0等。因此用Nullable范型。
复制 保存
privateNotifyEventArgs ParseNotify(NameValueCollection nv,
objectobj)
{
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach(PropertyInfo pi
inpropertyInfos)
{
stringv = nv.Get(pi.Name.ToLower());
if(v !=
null)
{
if(pi.PropertyType ==
typeof(
string))
{
pi.SetValue(obj, v,
null);
}
elseif(pi.PropertyType ==
typeof(
int?))
{
pi.SetValue(obj,
int.Parse(v),
null);
}
elseif(pi.PropertyType ==
typeof(
decimal?))
{
pi.SetValue(obj,
decimal.Parse(v),
null);
}
elseif(pi.PropertyType ==
typeof(DateTime?))
{
pi.SetValue(obj, DateTime.Parse(v),
null);
}
elseif(pi.PropertyType ==
typeof(
bool))
{
pi.SetValue(obj,
bool.Parse(v),
null);
}
else {
//转型失败会抛出异常 pi.SetValue(obj, v,
null);
}
}
}
return(NotifyEventArgs) obj;
}
2、从类型中获取排序后的参数
复制 保存
/// <summary></summary>/// 获取排序后的参数 /// /// /// <returns></returns>privateSortedListstring,
string>GetParam(
objectobj)
{
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(
BindingFlags.Public | BindingFlags.Instance);
SortedListstring,
string>sortedList
=
newSortedListstring,
string>(StringComparer.CurrentCultureIgnoreCase);
foreach(PropertyInfo pi
inpropertyInfos)
{
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());
}
}
returnsortedList;
}
3、从SortedList中产生参数
复制 保存
privatestringGetUrlParam(SortedListstring,
string>sortedList,
boolisEncode)
{
StringBuilder param =
newStringBuilder();
StringBuilder encodeParam =
newStringBuilder();
if(isEncode ==
false)
{
foreach(KeyValuePairstring,
string>kvp
insortedList)
{
stringt =
string.Format(
"{0}={1}", kvp.Key, kvp.Value);
param.Append(t +
"&");
}
returnparam.ToString().TrimEnd('&');
}
else {
foreach(KeyValuePairstring,
string>kvp
insortedList)
{
stringet =
string.Format(
"{0}={1}",
HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value));
encodeParam.Append(et +
"&");
}
returnencodeParam.ToString().TrimEnd('&');
}
}
下载地址 http://www.cnblogs.com/Files/bluewater/CommonAliPay.rar
因为时间很紧,有些地方还不完善,大家提出意见,有时间我会修改的
网上赠与服务集成技术文档V1.35.pdf
本文转自
http://www.chenjiliang.com/Article/View.aspx?ArticleID=2125&TypeID=84
分享到:
相关推荐
支付宝接口源代码支付宝接口源代码支付宝接口源代码
根据支付宝给与的接口,做了修改,实现支付宝接口
支付宝接口开发源代码可直接运行测试效果
支付宝接口php版,支付宝接口asp.net版,支付宝接口c#版,支付宝接口asp版;php即时到帐,即时到帐接口源代码. 包含支付宝接口源码一个完整的例子源码,里面有详细注释和详细的说明文件,一看就会.
.net(c#)支付宝接口源代码(亲测可用)
支付宝接口源代码(应该是目前最好的了) 支付宝的接口调用很不方便,刚做好一个封装,实现了虚拟交易和实物交易。
本接口对初学者有帮助。 请求的方式和接收的方式可以参数,需要个人修改一下。
支付宝接口支付宝接口支付宝接口支付宝接口支付宝接口支付宝接口支付宝接口支付宝接口支付宝接口支付宝接口支付宝接口支付宝接口支付宝接口
支付宝红包源代码:支付宝推出扫码领红包的活动,10亿红包呦,发红包还能赚赏金,遂引起羊毛党的关注,推广自己红包口令的方式多种多样,最近偶然发现有人恶意使用js代码进行红包推广,特此发布一个正常的支付宝红包...
支付宝源代码以及文档,需要的可以下载,很实用,看完保证会
支付宝源代码~测试过 非常安全
完整的支付宝公司,支付宝接口 asp开发代码,有相关说明。
支付宝java项目源代码
支付宝接口源代码(应该是目前最好的了) 支付宝的接口调用很不方便,刚做好一个封装,实现了虚拟交易和实物交易。所有版本都有
淘宝支付宝接口程序/支付宝接口代码/php 不是很难的
最近项目里用到支付类的封装遇到各种坑,于是自己整理了thinkphp5+bootstrap整合了微信支付和支付宝支付接口类完美实现扫码付款demo(带数据库后台设置支付参数即可完成)。绝对能够实现支付宝和微信支付,支付宝...
支付宝免签约php接口,asp接口代码 支付宝免签约即时到帐系统(接入通用教程) 一、免签约接口运行原理:(用户充值,用户升级,自动发卡等网站) 二、回调参数说明:(GET方式) 参数名 参数 说明 交易密钥 ...
从网上搜索到的支付宝接口代码,包括具体实例。我还没有测试,希望大家测试好后说一声,谢谢
支付宝接口相关代码和文档,支付宝纯担保交易接口