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

 

支付宝的接口调用很不方便,刚做好一个封装,实现了虚拟 交易 和实物交易。
解决方案 中有三个项目以及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

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
支付宝接口 asp.net c#支付宝接口详细代码   支付宝Payto接口的C#.net实现方法。支付宝现在这种支付方式比较多象网银在线等使用的方法都是url验证,就是通过url参数和一个这些url参数的md5编码来确认这个连接的正确性,支付宝在你购买成功后跳转自定义连接的时候会传2次过来,第一次是数据底层请求,第二次是web请求,而只有第一次有验证码,这个只能通过记录下来才看的到,因为两次请求间隔很小,如果光显示的话最后的结果是被第二次覆盖了的。所以在接收的时候就要设定接收条件,一种是没有notify_type参数的,一种是有的。   我们先来看一下创建一个连接地址 t1=ConfigurationSettings.AppSettings["interface"];//支付接口,就是给的一个连接地址   t2=ConfigurationSettings.AppSettings["account"];//支付宝帐户你的帐户   t3=ConfigurationSettings.AppSettings["password"];//安全校验码,设置的商家验证码   t4="images/logo_zfbsmall.gif";//按钮图片地址   t5="test";//悬停说明   cmd="0001";//默认   subject="item";//商品名称   body="decrip";//描述   order_no=;//定单号,用户自己生成,方便自己管理 prices=100;//价格0.01~50000.00   rurl="http://www.xxx.com/";//商品展示网址   types="1";//1:商品购买2:服务购买3:网络拍卖4:捐赠   number="1";//购买数量   transport="3";//1:平邮2:快递3:虚拟物品   ordinary_fee="";//平邮运费   express_fee="";//快递运费   readonlys="true";//交易信息是否只读   buyer_msg="";//买家给卖家的留言   buyer="";//买家Email   buyer_name="";//买家姓名   buyer_address="";//买家地址   buyer_zipcode="";//买家邮编   buyer_tel="";//买家电话号码   buyer_mobile="";//买家手机号码   partner=ConfigurationSettings.AppSettings["partenid"];//合作伙伴ID,这个是固定的   上面就是要提供得基本信息,然后就是生成支付宝得连接,也就是给支付宝提供一条带验证的购买信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值