大型网上购物系统除了能让会员选择货到付款结账方式外,还应该提供一些更方便快捷的网上支付方式。如果网上商店没有足够的实力提供会员直接在网站中建立现金账户的功能,就可以将订单信息转接到支付宝,让会员从支付宝付款。当然就算会员可以在网站上建立自己的现金账户,提供支付宝支付功能也不失为另一种方便快捷的支付方式,这可以给客户提供更多可选的支付方式。
首先,网上购物系统必须与支付宝公司签订合作协议,以确保从本购物网站上传到
支付宝网站上的订单信息能被正确接收。
当会员于购物网站上买下一系列商品并选择支付宝付款方式后,购物系统即将会员购物的订单信息转发到支付宝,网站页面也会转到支付宝的付款页面。此时,支付宝页面会发送一个验证信息到本网站以确认支付宝正确收到订单信息。
会员于支付宝网站付款完成后,网站页面会重新跳回本购物网站,同时支付宝会将已付款的订单信息发回本网站以便对本购物网站的数据库进行必要的修改操作。另外本网站还需要向支付宝网站发送一个返回信息,告知支付宝本系统已正确收到付款完毕的订单信息并且已经完成对数据的处理操作。
向支付宝网站传送订单信息时主要参数的含义:
gateway :支付接口
service:识别是何接口实现何功能的表示
selleremail:商家签约时的支付宝账号,即收款的支付宝账号
key:安全校验码,与partner是一组
partner:商户ID,合作伙伴ID
signtype:加密类型
inputcharset:编码类型
showurl:展示地址,即在支付宝页面时商品名称旁边的“详情”的链接地址
outtradeno:会员订单编号,订单编号必须在本系统中保持唯一
subject:商品名称,也可称为订单名称,该接口并不是单一的只能买一样东西,可把一次支付当作一次下订单
body:商品描述,即备注
totalfee:商品价格,也可称为订单的总金额
源码分析(C#):
首先必须建立一个通知页面(Notify.aspx)和一个返回页面(Return.aspx)以接受并验证从支付宝返回的信息并对数据库中相应的订单信息做修改处理操作。
Notify.aspx.cs
 
     代码
    
      代码
    
    
        
          
    
    <!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
    
    
          
            using
          
          
             System; 
          
          
            using
          
          
             System.Data; 
          
          
            using
          
          
             System.Configuration; 
          
          
            using
          
          
             System.Collections; 
          
          
            using
          
          
             System.Web; 
          
          
            using
          
          
             System.Web.Security; 
          
          
            using
          
          
             System.Web.UI; 
          
          
            using
          
          
             System.Web.UI.WebControls; 
          
          
            using
          
          
             System.Web.UI.WebControls.WebParts; 
          
          
            using
          
          
             System.Web.UI.HtmlControls; 
          
          
            using
          
          
             System.Text; 
          
          
            using
          
          
             System.Collections.Specialized; 
          
          
            using
          
          
             System.IO; 
          
          
            using
          
          
             Gateway; 
          
          
            ///
          
          
          
          
          
          
          
          
            ///
          
          
             创建该页面文件时,请留心该页面文件中无任何HTML代码及空格。 
          
          
            ///
          
          
             该页面称作“通知页”,是异步被支付宝服务器所调用。 
          
          
            ///
          
          
             当支付宝的订单状态改变时,支付宝服务器则会自动调用此页面,因此请做好自身网站订单信息与支付宝上的订单的同步工作 
          
          
            ///
          
          
          
          
          
          
          
          
            public
          
          
          
          
            partial
          
          
          
          
            class
          
          
             AlipayNotify : System.Web.UI.Page { 
          
          
            protected
          
          
          
          
            void
          
          
             PageLoad(
          
          
            object
          
          
             sender, EventArgs e) { 
          
          
            string
          
          
             alipayNotifyURL 
          
          
            =
          
          
          
          
            "
          
          
            https://www.alipay.com/cooperate/gateway.do?service=notifyverify
          
          
            "
          
          
            ; 
          
          
            //
          
          
            string alipayNotifyURL = "
          
          
            http://notify.alipay.com/trade/notifyquery.do?
          
          
            ";
          
          
            //
          
          
            此路径是在上面链接地址无法起作用时替换使用。
          
          
          
          
          
          
            string
          
          
             partner 
          
          
            =
          
          
          
          
            ""
          
          
            ; 
          
          
            //
          
          
            partner合作伙伴id(必须填写)
          
          
          
          
          
          
            string
          
          
             key 
          
          
            =
          
          
          
          
            ""
          
          
            ; 
          
          
            //
          
          
            partner 的对应交易安全校验码(必须填写)
          
          
          
          
          
          
            string
          
          
             inputcharset 
          
          
            =
          
          
          
          
            "
          
          
            utf-8
          
          
            "
          
          
            ;
          
          
            //
          
          
            编码类型,完全根据客户自身的项目的编码格式而定,千万不要填错。否则极其容易造成MD5加密错误。
          
          
          
          
             alipayNotifyURL 
          
          
            =
          
          
             alipayNotifyURL 
          
          
            +
          
          
          
          
            "
          
          
            &partner=
          
          
            "
          
          
          
          
            +
          
          
             partner 
          
          
            +
          
          
          
          
            "
          
          
            ¬ifyid=
          
          
            "
          
          
          
          
            +
          
          
             Request.Form[
          
          
            "
          
          
            notifyid
          
          
            "
          
          
            ]; 
          
          
            //
          
          
            获取支付宝ATN返回结果,true是正确的订单信息,false 是无效的
          
          
          
          
          
          
            string
          
          
             responseTxt 
          
          
            =
          
          
             AliPay.GetHttp(alipayNotifyURL, 
          
          
            120000
          
          
            ); 
          
          
            //
          
          
            *******加密签名程序开始*******
          
          
          
          
          
          
            int
          
          
             i; NameValueCollection coll; 
          
          
            //
          
          
            Load Form variables into NameValueCollection variable.
          
          
          
          
             coll 
          
          
            =
          
          
             Request.Form; 
          
          
            //
          
          
             Get names of all forms into a string array.
          
          
          
          
             String[] requestarr 
          
          
            =
          
          
             coll.AllKeys; 
          
          
            //
          
          
            进行排序;
          
          
          
          
          
          
            string
          
          
            [] Sortedstr 
          
          
            =
          
          
             AliPay.BubbleSort(requestarr); 
          
          
            //
          
          
            构造待md5摘要字符串 ;
          
          
          
          
             StringBuilder prestr 
          
          
            =
          
          
          
          
            new
          
          
             StringBuilder(); 
          
          
            for
          
          
             (i 
          
          
            =
          
          
          
          
            0
          
          
            ; i 
          
          
            <
          
          
             Sortedstr.Length; i
          
          
            ++
          
          
            ) { 
          
          
            if
          
          
             (Request.Form[Sortedstr[i]] 
          
          
            !=
          
          
          
          
            ""
          
          
          
          
            &&
          
          
             Sortedstr[i] 
          
          
            !=
          
          
          
          
            "
          
          
            sign
          
          
            "
          
          
          
          
            &&
          
          
             Sortedstr[i] 
          
          
            !=
          
          
          
          
            "
          
          
            signtype
          
          
            "
          
          
            ) { 
          
          
            if
          
          
             (i 
          
          
            ==
          
          
             Sortedstr.Length 
          
          
            -
          
          
          
          
            1
          
          
            ) { prestr.Append(Sortedstr[i] 
          
          
            +
          
          
          
          
            "
          
          
            =
          
          
            "
          
          
          
          
            +
          
          
             Request.Form[Sortedstr[i]]); } 
          
          
            else
          
          
             { prestr.Append(Sortedstr[i] 
          
          
            +
          
          
          
          
            "
          
          
            =
          
          
            "
          
          
          
          
            +
          
          
             Request.Form[Sortedstr[i]] 
          
          
            +
          
          
          
          
            "
          
          
            &
          
          
            "
          
          
            ); } } } prestr.Append(key); 
          
          
            string
          
          
             mysign 
          
          
            =
          
          
             AliPay.GetMD5(prestr.ToString(), inputcharset); 
          
          
            //
          
          
            *******加密签名程序结束*******
          
          
          
          
          
          
            string
          
          
             sign 
          
          
            =
          
          
             Request.Form[
          
          
            "
          
          
            sign
          
          
            "
          
          
            ]; 
          
          
            if
          
          
             (mysign 
          
          
            ==
          
          
             sign 
          
          
            &&
          
          
             responseTxt 
          
          
            ==
          
          
          
          
            "
          
          
            true
          
          
            "
          
          
            ) 
          
          
            //
          
          
            验证支付发过来的消息,签名是否正确,只要成功进如这个判断里,则表示该页面已被支付宝服务器成功调用 
          
          
            //
          
          
            但判断内出现自身编写的程序相关错误导致通知给支付宝并不是发送success的消息或没有更新客户自身的数据库的情况,请自身程序编写好应对措施,否则查明原因时困难之极
          
          
          
          
             { 
          
          
            if
          
          
             (Request.Form[
          
          
            "
          
          
            tradestatus
          
          
            "
          
          
            ] 
          
          
            ==
          
          
          
          
            "
          
          
            WAITBUYERPAY
          
          
            "
          
          
            )
          
          
            //
          
          
             判断支付状态等待买家付款(文档中有枚举表可以参考) 
          
          
          
          
             { 
          
          
            //
          
          
            更新自己数据库的订单语句,请自己填写一下
          
          
          
          
          
          
            string
          
          
             strOrderNO 
          
          
            =
          
          
             Request.Form[
          
          
            "
          
          
            outtradeno
          
          
            "
          
          
            ];
          
          
            //
          
          
            订单号
          
          
          
          
          
          
            string
          
          
             strPrice 
          
          
            =
          
          
             Request.Form[
          
          
            "
          
          
            totalfee
          
          
            "
          
          
            ];
          
          
            //
          
          
            金额 如果你申请了商家购物卷功能,在返回信息里面请不要做金额的判断,否则会校验通过不了。
          
          
          
          
             } 
          
          
            else
          
          
          
          
            if
          
          
             (Request.Form[
          
          
            "
          
          
            tradestatus
          
          
            "
          
          
            ] 
          
          
            ==
          
          
          
          
            "
          
          
            TRADEFINISHED
          
          
            "
          
          
          
          
            ||
          
          
             Request.Form[
          
          
            "
          
          
            tradestatus
          
          
            "
          
          
            ] 
          
          
            ==
          
          
          
          
            "
          
          
            TRADESUCCESS
          
          
            "
          
          
            )
          
          
            //
          
          
             判断支付状态交易成功结束(文档中有枚举表可以参考) 
          
          
          
          
             { 
          
          
            //
          
          
            更新自己数据库的订单语句,请自己填写一下
          
          
          
          
          
          
            string
          
          
             strOrderNO 
          
          
            =
          
          
             Request.Form[
          
          
            "
          
          
            outtradeno
          
          
            "
          
          
            ];
          
          
            //
          
          
            订单号
          
          
          
          
          
          
            string
          
          
             strPrice 
          
          
            =
          
          
             Request.Form[
          
          
            "
          
          
            totalfee
          
          
            "
          
          
            ];
          
          
            //
          
          
            金额 
          
          
          
          
             } 
          
          
            else
          
          
             { 
          
          
            //
          
          
            更新自己数据库的订单语句,请自己填写一下
          
          
          
          
             } Response.Write(
          
          
            "
          
          
            success
          
          
            "
          
          
            ); 
          
          
            //
          
          
            返回给支付宝消息,成功,请不要改写这个success 
          
          
            //
          
          
            success与fail及其他字符的区别在于,支付宝的服务器若遇到success时,则不再发送请求通知(即不再调用该页面,让该页面再次运行起来), 
          
          
            //
          
          
            若不是success,则支付宝默认没有收到成功的信息,则会反复不停地调用该页面直到失效,有效调用时间是24小时以内。 
          
          
            //
          
          
            最好写TXT文件,以记录下是否异步返回记录。
          
          
          
          
          
          
            ///
          
          
            /写文本,纪录支付宝返回消息,比对md5计算结果(如网站不支持写txt文件,可改成写数据库)
          
          
          
          
          
          
            //
          
          
            string TOEXCELLR = "MD5结果:mysign=" + mysign + ",sign=" + sign + ",responseTxt=" + responseTxt; 
          
          
            //
          
          
            StreamWriter fs = new StreamWriter(Server.MapPath("NotifyDATA/" + DateTime.Now.ToString().Replace(":", "")) + ".txt", false, System.Text.Encoding.Default); 
          
          
            //
          
          
            fs.Write(TOEXCELLR); 
          
          
            //
          
          
            fs.Close();
          
          
          
          
             } 
          
          
            else
          
          
             { Response.Write(
          
          
            "
          
          
            fail
          
          
            "
          
          
            ); 
          
          
            //
          
          
            最好写TXT文件,以记录下是否异步返回记录。 
          
          
            //
          
          
            写文本,纪录支付宝返回消息,比对md5计算结果(如网站不支持写txt文件,可改成写数据库)
          
          
          
          
          
          
            string
          
          
             TOEXCELLR 
          
          
            =
          
          
          
          
            "
          
          
            MD5结果:mysign=
          
          
            "
          
          
          
          
            +
          
          
             mysign 
          
          
            +
          
          
          
          
            "
          
          
            ,sign=
          
          
            "
          
          
          
          
            +
          
          
             sign 
          
          
            +
          
          
          
          
            "
          
          
            ,responseTxt=
          
          
            "
          
          
          
          
            +
          
          
             responseTxt; StreamWriter fs 
          
          
            =
          
          
          
          
            new
          
          
             StreamWriter(Server.MapPath(
          
          
            "
          
          
            NotifyDATA/
          
          
            "
          
          
          
          
            +
          
          
             DateTime.Now.ToString().Replace(
          
          
            "
          
          
            :
          
          
            "
          
          
            , 
          
          
            ""
          
          
            )) 
          
          
            +
          
          
          
          
            "
          
          
            .txt
          
          
            "
          
          
            , 
          
          
            false
          
          
            , System.Text.Encoding.Default); fs.Write(TOEXCELLR); fs.Close(); } } }
          
        
      
    Return.aspx.cs
 
     代码
    
      代码
    
    
        
          
    
    <!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
    
    
          
            using
          
          
             System; 
          
          
            using
          
          
             System.Data; 
          
          
            using
          
          
             System.Configuration; 
          
          
            using
          
          
             System.Collections; 
          
          
            using
          
          
             System.Web; 
          
          
            using
          
          
             System.Web.Security; 
          
          
            using
          
          
             System.Web.UI; 
          
          
            using
          
          
             System.Web.UI.WebControls; 
          
          
            using
          
          
             System.Web.UI.WebControls.WebParts; 
          
          
            using
          
          
             System.Web.UI.HtmlControls; 
          
          
            using
          
          
             System.Text; 
          
          
            using
          
          
             System.Collections.Specialized; 
          
          
            using
          
          
             System.IO; 
          
          
            using
          
          
             Gateway; 
          
          
            ///
          
          
          
          
          
          
          
          
            ///
          
          
             创建该页面文件时,请留心该页面文件是可以对其进行美工处理的,原因在于支付完成以后,当前窗口会从支付宝的页面跳转回这个页面。 
          
          
            ///
          
          
             该页面称作“返回页”,是同步被支付宝服务器所调用,可当作是支付完成后的提示信息页,如“您的某某某订单,多少金额已支付成功”。 
          
          
            ///
          
          
          
          
          
          
          
          
            public
          
          
          
          
            partial
          
          
          
          
            class
          
          
             AlipayReturn : System.Web.UI.Page { 
          
          
            protected
          
          
          
          
            void
          
          
             PageLoad(
          
          
            object
          
          
             sender, EventArgs e) { 
          
          
            string
          
          
             alipayNotifyURL 
          
          
            =
          
          
          
          
            "
          
          
            https://www.alipay.com/cooperate/gateway.do?service=notifyverify
          
          
            "
          
          
            ; 
          
          
            //
          
          
            string alipayNotifyURL = "
          
          
            http://notify.alipay.com/trade/notifyquery.do?
          
          
            ";
          
          
            //
          
          
            此路径是在上面链接地址无法起作用时替换使用。
          
          
          
          
          
          
            string
          
          
             key 
          
          
            =
          
          
          
          
            ""
          
          
            ; 
          
          
            //
          
          
            partner 的对应交易安全校验码(必须填写)
          
          
          
          
          
          
            string
          
          
             partner 
          
          
            =
          
          
          
          
            ""
          
          
            ; 
          
          
            //
          
          
            partner合作伙伴id(必须填写)
          
          
          
          
          
          
            string
          
          
             inputcharset 
          
          
            =
          
          
          
          
            "
          
          
            utf-8
          
          
            "
          
          
            ;
          
          
            //
          
          
            编码类型,完全根据客户自身的项目的编码格式而定,千万不要填错。否则极其容易造成MD5加密错误。
          
          
          
          
             alipayNotifyURL 
          
          
            =
          
          
             alipayNotifyURL 
          
          
            +
          
          
          
          
            "
          
          
            &partner=
          
          
            "
          
          
          
          
            +
          
          
             partner 
          
          
            +
          
          
          
          
            "
          
          
            ¬ifyid=
          
          
            "
          
          
          
          
            +
          
          
             Request.QueryString[
          
          
            "
          
          
            notifyid
          
          
            "
          
          
            ]; 
          
          
            //
          
          
            获取支付宝ATN返回结果,true是正确的订单信息,false 是无效的
          
          
          
          
          
          
            string
          
          
             responseTxt 
          
          
            =
          
          
             AliPay.GetHttp(alipayNotifyURL, 
          
          
            120000
          
          
            ); 
          
          
            //
          
          
            *******加密签名程序开始
          
          
            //
          
          
            *******
          
          
          
          
          
          
            int
          
          
             i; NameValueCollection coll; 
          
          
            //
          
          
            Load Form variables into NameValueCollection variable.
          
          
          
          
             coll 
          
          
            =
          
          
             Request.QueryString; 
          
          
            //
          
          
             Get names of all forms into a string array.
          
          
          
          
             String[] requestarr 
          
          
            =
          
          
             coll.AllKeys; 
          
          
            //
          
          
            进行排序;
          
          
          
          
          
          
            string
          
          
            [] Sortedstr 
          
          
            =
          
          
             AliPay.BubbleSort(requestarr); 
          
          
            //
          
          
            构造待md5摘要字符串 ;
          
          
          
          
             StringBuilder prestr 
          
          
            =
          
          
          
          
            new
          
          
             StringBuilder(); 
          
          
            for
          
          
             (i 
          
          
            =
          
          
          
          
            0
          
          
            ; i 
          
          
            <
          
          
             Sortedstr.Length; i
          
          
            ++
          
          
            ) { 
          
          
            if
          
          
             (Request.Form[Sortedstr[i]] 
          
          
            !=
          
          
          
          
            ""
          
          
          
          
            &&
          
          
             Sortedstr[i] 
          
          
            !=
          
          
          
          
            "
          
          
            sign
          
          
            "
          
          
          
          
            &&
          
          
             Sortedstr[i] 
          
          
            !=
          
          
          
          
            "
          
          
            signtype
          
          
            "
          
          
            ) { 
          
          
            if
          
          
             (i 
          
          
            ==
          
          
             Sortedstr.Length 
          
          
            -
          
          
          
          
            1
          
          
            ) { prestr.Append(Sortedstr[i] 
          
          
            +
          
          
          
          
            "
          
          
            =
          
          
            "
          
          
          
          
            +
          
          
             Request.QueryString[Sortedstr[i]]); } 
          
          
            else
          
          
             { prestr.Append(Sortedstr[i] 
          
          
            +
          
          
          
          
            "
          
          
            =
          
          
            "
          
          
          
          
            +
          
          
             Request.QueryString[Sortedstr[i]] 
          
          
            +
          
          
          
          
            "
          
          
            &
          
          
            "
          
          
            ); } } } prestr.Append(key); 
          
          
            //
          
          
            生成Md5摘要;
          
          
          
          
          
          
            string
          
          
             mysign 
          
          
            =
          
          
             AliPay.GetMD5(prestr.ToString(), inputcharset); 
          
          
            //
          
          
            *******加密签名程序结束*******
          
          
          
          
          
          
            string
          
          
             sign 
          
          
            =
          
          
             Request.QueryString[
          
          
            "
          
          
            sign
          
          
            "
          
          
            ]; 
          
          
            //
          
          
             Response.Write(prestr.ToString()); 
          
          
            //
          
          
            调试用,支付宝服务器返回时的完整路径。
          
          
          
          
          
          
            if
          
          
             (mysign 
          
          
            ==
          
          
             sign 
          
          
            &&
          
          
             responseTxt 
          
          
            ==
          
          
          
          
            "
          
          
            true
          
          
            "
          
          
            ) 
          
          
            //
          
          
            验证支付发过来的消息,签名是否正确
          
          
          
          
             { 
          
          
            //
          
          
            更新自己数据库的订单语句,请自己填写一下
          
          
          
          
          
          
            string
          
          
             strOrderNO 
          
          
            =
          
          
             Request.QueryString[
          
          
            "
          
          
            outtradeno
          
          
            "
          
          
            ];
          
          
            //
          
          
            订单号
          
          
          
          
          
          
            string
          
          
             strPrice 
          
          
            =
          
          
             Request.QueryString[
          
          
            "
          
          
            totalfee
          
          
            "
          
          
            ];
          
          
            //
          
          
            金额
          
          
          
          
          
          
            string
          
          
             strTradeStatus 
          
          
            =
          
          
             Request.QueryString[
          
          
            "
          
          
            TRADESTATUS
          
          
            "
          
          
            ];
          
          
            //
          
          
            订单状态
          
          
          
          
             Response.Write(
          
          
            "
          
          
            订单号:
          
          
            "
          
          
          
          
            +
          
          
             strOrderNO 
          
          
            +
          
          
          
          
            "
          
          
             金额:
          
          
            "
          
          
          
          
            +
          
          
             strPrice); 
          
          
            //
          
          
            成功,可美化该页面,提示信息
          
          
          
          
             } 
          
          
            else
          
          
             { Response.Write(
          
          
            "
          
          
            ------------------------------------------
          
          
            "
          
          
            ); Response.Write(
          
          
            "
          
          
             Result:responseTxt=
          
          
            "
          
          
          
          
            +
          
          
             responseTxt); Response.Write(
          
          
            "
          
          
             Result:mysign=
          
          
            "
          
          
          
          
            +
          
          
             mysign); Response.Write(
          
          
            "
          
          
             Result:sign=
          
          
            "
          
          
          
          
            +
          
          
             sign); Response.Write(
          
          
            "
          
          
            支付失败
          
          
            "
          
          
            ); 
          
          
            //
          
          
            支付失败,提示信息
          
          
          
          
             } } }
          
        
      
    
除此之外在Notify.aspx页面和Return.aspx页面公用的一些方法,可以提取出来放在一个公共的类里面(Alipay.cs)
Alipay.cs
 
     代码
    
      代码
    
    
        
          
    
    <!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
    
    
          
            using
          
          
             System.Web; 
          
          
            using
          
          
             System.Text; 
          
          
            using
          
          
             System.Security.Cryptography; 
          
          
            using
          
          
             System.IO; 
          
          
            using
          
          
             System.Net; 
          
          
            using
          
          
             System; 
          
          
            ///
          
          
          
          
          
          
          
          
            ///
          
          
             New Interface for AliPay 
          
          
            ///
          
          
          
          
          
          
          
          
            namespace
          
          
             Gateway { 
          
          
            public
          
          
          
          
            class
          
          
             AliPay { 
          
          
            ///
          
          
          
          
          
          
          
          
            ///
          
          
             与ASP兼容的MD5加密算法 
          
          
            ///
          
          
          
          
          
          
          
          
          
          
            public
          
          
          
          
            static
          
          
          
          
            string
          
          
             GetMD5(
          
          
            string
          
          
             s, 
          
          
            string
          
          
             inputcharset) { MD5 md5 
          
          
            =
          
          
          
          
            new
          
          
             MD5CryptoServiceProvider(); 
          
          
            byte
          
          
            [] t 
          
          
            =
          
          
             md5.ComputeHash(Encoding.GetEncoding(inputcharset).GetBytes(s)); StringBuilder sb 
          
          
            =
          
          
          
          
            new
          
          
             StringBuilder(
          
          
            32
          
          
            ); 
          
          
            for
          
          
             (
          
          
            int
          
          
             i 
          
          
            =
          
          
          
          
            0
          
          
            ; i 
          
          
            <
          
          
             t.Length; i
          
          
            ++
          
          
            ) { sb.Append(t[i].ToString(
          
          
            "
          
          
            x
          
          
            "
          
          
            ).PadLeft(
          
          
            2
          
          
            , 
          
          
            “
          
          
            0
          
          
            “
          
          
            )); } 
          
          
            return
          
          
             sb.ToString(); } 
          
          
            ///
          
          
          
          
          
          
          
          
            ///
          
          
             冒泡排序法 
          
          
            ///
          
          
             按照字母序列从a到z的顺序排列 
          
          
            ///
          
          
          
          
          
          
          
          
          
          
            public
          
          
          
          
            static
          
          
          
          
            string
          
          
            [] BubbleSort(
          
          
            string
          
          
            [] r) { 
          
          
            int
          
          
             i, j; 
          
          
            //
          
          
            交换标志 
          
          
          
          
          
          
            string
          
          
             temp; 
          
          
            bool
          
          
             exchange; 
          
          
            for
          
          
             (i 
          
          
            =
          
          
          
          
            0
          
          
            ; i 
          
          
            <
          
          
             r.Length; i
          
          
            ++
          
          
            ) 
          
          
            //
          
          
            最多做R.Length-1趟排序 
          
          
          
          
             { exchange 
          
          
            =
          
          
          
          
            false
          
          
            ; 
          
          
            //
          
          
            本趟排序开始前,交换标志应为假
          
          
          
          
          
          
            for
          
          
             (j 
          
          
            =
          
          
             r.Length 
          
          
            -
          
          
          
          
            2
          
          
            ; j 
          
          
            >=
          
          
             i; j
          
          
            --
          
          
            ) {
          
          
            //
          
          
            交换条件
          
          
          
          
          
          
            if
          
          
             (System.String.CompareOrdinal(r[j 
          
          
            +
          
          
          
          
            1
          
          
            ], r[j]) 
          
          
            <
          
          
          
          
            0
          
          
            ) { temp 
          
          
            =
          
          
             r[j 
          
          
            +
          
          
          
          
            1
          
          
            ]; r[j 
          
          
            +
          
          
          
          
            1
          
          
            ] 
          
          
            =
          
          
             r[j]; r[j] 
          
          
            =
          
          
             temp; exchange 
          
          
            =
          
          
          
          
            true
          
          
            ; 
          
          
            //
          
          
            发生了交换,故将交换标志置为真 
          
          
          
          
             } } 
          
          
            if
          
          
             (
          
          
            !
          
          
            exchange) 
          
          
            //
          
          
            本趟排序未发生交换,提前终止算法 
          
          
          
          
             { 
          
          
            break
          
          
            ; } } 
          
          
            return
          
          
             r; } 
          
          
            ///
          
          
          
          
          
          
          
          
            ///
          
          
             生成URL链接或加密结果 
          
          
            ///
          
          
          
          
          
          
          
          
            ///
          
          
          
          
          
          
            参数加密数组
          
          
          
          
          
          
            ///
          
          
          
          
          
          
            编码格式
          
          
          
          
          
          
            ///
          
          
          
          
          
          
            加密类型
          
          
          
          
          
          
            ///
          
          
          
          
          
          
            安全校验码
          
          
          
          
          
          
            ///
          
          
          
          
          
          
            字符串URL或加密结果
          
          
          
          
          
          
          
          
            public
          
          
          
          
            static
          
          
          
          
            string
          
          
             CreatUrl( 
          
          
            //
          
          
            string gateway,
          
          
            //
          
          
            GET方式传递参数时请去掉注释
          
          
          
          
          
          
            string
          
          
            [] para, 
          
          
            string
          
          
             inputcharset, 
          
          
            string
          
          
             signtype, 
          
          
            string
          
          
             key ) { 
          
          
            int
          
          
             i; 
          
          
            //
          
          
            进行排序;
          
          
          
          
          
          
            string
          
          
            [] Sortedstr 
          
          
            =
          
          
             BubbleSort(para); 
          
          
            //
          
          
            构造待md5摘要字符串 ;
          
          
          
          
             StringBuilder prestr 
          
          
            =
          
          
          
          
            new
          
          
             StringBuilder(); 
          
          
            for
          
          
             (i 
          
          
            =
          
          
          
          
            0
          
          
            ; i 
          
          
            <
          
          
             Sortedstr.Length; i
          
          
            ++
          
          
            ) { 
          
          
            if
          
          
             (i 
          
          
            ==
          
          
             Sortedstr.Length 
          
          
            -
          
          
          
          
            1
          
          
            ) { prestr.Append(Sortedstr[i]); } 
          
          
            else
          
          
             { prestr.Append(Sortedstr[i] 
          
          
            +
          
          
          
          
            "
          
          
            &
          
          
            "
          
          
            ); } } prestr.Append(key); 
          
          
            //
          
          
            生成Md5摘要;
          
          
          
          
          
          
            string
          
          
             sign 
          
          
            =
          
          
             GetMD5(prestr.ToString(), inputcharset); 
          
          
            //
          
          
            以下是POST方式传递参数
          
          
          
          
          
          
            return
          
          
             sign; 
          
          
            //
          
          
            以下是GET方式传递参数 
          
          
            //
          
          
            构造支付Url; 
          
          
            //
          
          
             char[] delimiterChars = { “=“}; 
          
          
            //
          
          
             StringBuilder parameter = new StringBuilder(); 
          
          
            //
          
          
             parameter.Append(gateway); 
          
          
            //
          
          
             for (i = 0; i < Sortedstr.Length; i++) 
          
          
            //
          
          
             {
          
          
            //
          
          
            UTF-8格式的编码转换 
          
          
            //
          
          
             parameter.Append(Sortedstr[i].Split(delimiterChars)[0] + "=" + HttpUtility.UrlEncode(Sortedstr[i].Split(delimiterChars)[1]) + "&"); 
          
          
            //
          
          
             } 
          
          
            //
          
          
          
          
            //
          
          
             parameter.Append("sign=" + sign + "&signtype=" + signtype); 
          
          
            //
          
          
          
          
            //
          
          
          
          
            //
          
          
            返回支付Url; 
          
          
            //
          
          
             return parameter.ToString();
          
          
          
          
             } 
          
          
            //
          
          
            获取远程服务器ATN结果,验证是否是支付宝服务器发来的请求
          
          
          
          
          
          
            public
          
          
          
          
            static
          
          
          
          
            string
          
          
             GetHttp(
          
          
            string
          
          
             astrUrl, 
          
          
            int
          
          
             timeout) { 
          
          
            string
          
          
             strResult; 
          
          
            try
          
          
             { HttpWebRequest myReq 
          
          
            =
          
          
             (HttpWebRequest)HttpWebRequest.Create(astrUrl); myReq.Timeout 
          
          
            =
          
          
             timeout; HttpWebResponse HttpWResp 
          
          
            =
          
          
             (HttpWebResponse)myReq.GetResponse(); Stream myStream 
          
          
            =
          
          
             HttpWResp.GetResponseStream(); StreamReader sr 
          
          
            =
          
          
          
          
            new
          
          
             StreamReader(myStream, Encoding.Default); StringBuilder strBuilder 
          
          
            =
          
          
          
          
            new
          
          
             StringBuilder(); 
          
          
            while
          
          
             (
          
          
            -
          
          
            1
          
          
          
          
            !=
          
          
             sr.Peek()) { strBuilder.Append(sr.ReadLine()); } strResult 
          
          
            =
          
          
             strBuilder.ToString(); } 
          
          
            catch
          
          
             (Exception exp) { strResult 
          
          
            =
          
          
          
          
            "
          
          
            错误:
          
          
            "
          
          
          
          
            +
          
          
             exp.Message; } 
          
          
            return
          
          
             strResult; } } }
          
        
      
    
以上三个文件建之后,就可以在需要的地方对支付宝接口进行调用以完成支付宝支付的功能了(Default.aspx.cs)
 
     代码
    
      代码
    
    
        
          
    
    <!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->
    
    
          
            using
          
          
             System; 
          
          
            using
          
          
             System.Data; 
          
          
            using
          
          
             System.Configuration; 
          
          
            using
          
          
             System.Web; 
          
          
            using
          
          
             System.Web.Security; 
          
          
            using
          
          
             System.Web.UI; 
          
          
            using
          
          
             System.Web.UI.WebControls; 
          
          
            using
          
          
             System.Web.UI.WebControls.WebParts; 
          
          
            using
          
          
             System.Web.UI.HtmlControls; 
          
          
            using
          
          
             Gateway; 
          
          
            public
          
          
          
          
            partial
          
          
          
          
            class
          
          
             Default : System.Web.UI.Page { 
          
          
            protected
          
          
          
          
            void
          
          
             PageLoad(
          
          
            object
          
          
             sender, EventArgs e) { } 
          
          
            protected
          
          
          
          
            void
          
          
             BtnAlipayClick(
          
          
            object
          
          
             sender, EventArgs e) { 
          
          
            //
          
          
            业务参数赋值;
          
          
          
          
          
          
            string
          
          
             gateway 
          
          
            =
          
          
          
          
            "
          
          
            https://www.alipay.com/cooperate/gateway.do?
          
          
            "
          
          
            ; 
          
          
            //
          
          
            支付接口
          
          
          
          
          
          
            string
          
          
             service 
          
          
            =
          
          
          
          
            "
          
          
            createdirectpaybyuser
          
          
            "
          
          
            ; 
          
          
            //
          
          
            服务名称,这个是识别是何接口实现何功能的标识,请勿修改
          
          
          
          
          
          
            string
          
          
             selleremail 
          
          
            =
          
          
          
          
            ""
          
          
            ; 
          
          
            //
          
          
            商家签约时的支付宝帐号,即收款的支付宝帐号
          
          
          
          
          
          
            string
          
          
             signtype 
          
          
            =
          
          
          
          
            "
          
          
            MD5
          
          
            "
          
          
            ; 
          
          
            //
          
          
            加密类型,签名方式“不用改”
          
          
          
          
          
          
            string
          
          
             key 
          
          
            =
          
          
          
          
            ""
          
          
            ; 
          
          
            //
          
          
            安全校验码,与partner是一组,获取方式是:用签约时支付宝帐号登陆支付宝网站www.alipay.com,在商家服务我的商家里即可查到。
          
          
          
          
          
          
            string
          
          
             partner 
          
          
            =
          
          
          
          
            ""
          
          
            ; 
          
          
            //
          
          
            商户ID,合作身份者ID,合作伙伴ID
          
          
          
          
          
          
            string
          
          
             inputcharset 
          
          
            =
          
          
          
          
            "
          
          
            utf-8
          
          
            "
          
          
            ; 
          
          
            //
          
          
            编码类型,完全根据客户自身的项目的编码格式而定,千万不要填错。否则极其容易造成MD5加密错误。
          
          
          
          
          
          
            string
          
          
             showurl 
          
          
            =
          
          
          
          
            "
          
          
            http://www.alipay.com/
          
          
            "
          
          
            ; 
          
          
            //
          
          
            展示地址,即在支付页面时,商品名称旁边的“详情”的链接地址。
          
          
          
          
          
          
            string
          
          
             outtradeno 
          
          
            =
          
          
             TxtOrderno.Text.Trim(); 
          
          
            //
          
          
            客户自己的订单号,订单号必须在自身订单系统中保持唯一性
          
          
          
          
          
          
            string
          
          
             subject 
          
          
            =
          
          
             TxtSubject.Text.Trim(); 
          
          
            //
          
          
            商品名称,也可称为订单名称,该接口并不是单一的只能买一样东西,可把一次支付当作一次下订单
          
          
          
          
          
          
            string
          
          
             body 
          
          
            =
          
          
             TxtBody.Text.Trim(); 
          
          
            //
          
          
            商品描述,即备注
          
          
          
          
          
          
            string
          
          
             totalfee 
          
          
            =
          
          
             TxtTotalfee.Text.Trim(); 
          
          
            //
          
          
            商品价格,也可称为订单的总金额 
          
          
            //
          
          
            服务器通知url(AlipayNotify.aspx文件所在路经),必须是完整的路径地址
          
          
          
          
          
          
            string
          
          
             notifyurl 
          
          
            =
          
          
          
          
            "
          
          
            http://localhost:8978/directvs2005utf/AlipayNotify.aspx
          
          
            "
          
          
            ; 
          
          
            //
          
          
            服务器返回url(AlipayReturn.aspx文件所在路经),必须是完整的路径地址
          
          
          
          
          
          
            string
          
          
             returnurl 
          
          
            =
          
          
          
          
            "
          
          
            http://localhost:8978/directvs2005utf/AlipayReturn.aspx
          
          
            "
          
          
            ; 
          
          
            //
          
          
            构造数组; 
          
          
            //
          
          
            以下数组即是参与加密的参数,若参数的值不允许为空,若该参数为空,则不要成为该数组的元素
          
          
          
          
          
          
            string
          
          
            [] para 
          
          
            =
          
          
            { 
          
          
            "
          
          
            service=
          
          
            "
          
          
            +
          
          
            service, 
          
          
            "
          
          
            partner=
          
          
            "
          
          
          
          
            +
          
          
             partner, 
          
          
            "
          
          
            selleremail=
          
          
            "
          
          
          
          
            +
          
          
             selleremail, 
          
          
            "
          
          
            outtradeno=
          
          
            "
          
          
          
          
            +
          
          
             outtradeno, 
          
          
            "
          
          
            subject=
          
          
            "
          
          
          
          
            +
          
          
             subject, 
          
          
            "
          
          
            body=
          
          
            "
          
          
          
          
            +
          
          
             body, 
          
          
            "
          
          
            totalfee=
          
          
            "
          
          
          
          
            +
          
          
             totalfee, 
          
          
            "
          
          
            showurl=
          
          
            "
          
          
          
          
            +
          
          
             showurl, 
          
          
            "
          
          
            paymenttype=1
          
          
            "
          
          
            , 
          
          
            "
          
          
            notifyurl=
          
          
            "
          
          
          
          
            +
          
          
             notifyurl, 
          
          
            "
          
          
            returnurl=
          
          
            "
          
          
          
          
            +
          
          
             returnurl, 
          
          
            "
          
          
            inputcharset=
          
          
            "
          
          
            +
          
          
            inputcharset }; 
          
          
            //
          
          
            支付URL生成
          
          
          
          
          
          
            string
          
          
             aliayurl 
          
          
            =
          
          
             AliPay.CreatUrl( 
          
          
            //
          
          
            gateway,
          
          
            //
          
          
            GET方式传递参数时请去掉注释
          
          
          
          
             para, inputcharset, signtype, key ); 
          
          
            //
          
          
            以下是GET方式传递参数 
          
          
            //
          
          
            Response.Redirect(aliayurl); 
          
          
            //
          
          
            以下是POST方式传递参数
          
          
          
          
             Response.Write(
          
          
            "
          
          
          
          
            "
          
          
            ); Response.Write(
          
          
            "
          
          
            
              "
            
            
            
            
              +
            
            
               service 
            
            
              +
            
            
            
            
              "
            
            
              >
            
            
              "
            
            
              ); Response.Write(
            
            
              "
            
            
              
                "
              
              
              
              
                +
              
              
                 partner 
              
              
                +
              
              
              
              
                "
              
              
                >
              
              
                "
              
              
                ); Response.Write(
              
              
                "
              
              
                
                  "
                
                
                
                
                  +
                
                
                   selleremail 
                
                
                  +
                
                
                
                
                  "
                
                
                  >
                
                
                  "
                
                
                  ); Response.Write(
                
                
                  "
                
                
                  
                    "
                  
                  
                  
                  
                    +
                  
                  
                     outtradeno 
                  
                  
                    +
                  
                  
                  
                  
                    "
                  
                  
                    >
                  
                  
                    "
                  
                  
                    ); Response.Write(
                  
                  
                    "
                  
                  
                    
                      "
                    
                    
                    
                    
                      +
                    
                    
                       subject 
                    
                    
                      +
                    
                    
                    
                    
                      "
                    
                    
                      >
                    
                    
                      "
                    
                    
                      ); Response.Write(
                    
                    
                      "
                    
                    
                      
                        "
                      
                      
                      
                      
                        +
                      
                      
                         body 
                      
                      
                        +
                      
                      
                      
                      
                        "
                      
                      
                        >
                      
                      
                        "
                      
                      
                        ); Response.Write(
                      
                      
                        "
                      
                      
                        
                          "
                        
                        
                        
                        
                          +
                        
                        
                           totalfee 
                        
                        
                          +
                        
                        
                        
                        
                          "
                        
                        
                          >
                        
                        
                          "
                        
                        
                          ); Response.Write(
                        
                        
                          "
                        
                        
                          
                            "
                          
                          
                          
                          
                            +
                          
                          
                             showurl 
                          
                          
                            +
                          
                          
                          
                          
                            "
                          
                          
                            >
                          
                          
                            "
                          
                          
                            ); Response.Write(
                          
                          
                            "
                          
                          
                            
                              "
                            
                            
                            
                            
                              +
                            
                            
                               returnurl 
                            
                            
                              +
                            
                            
                            
                            
                              "
                            
                            
                              >
                            
                            
                              "
                            
                            
                              ); Response.Write(
                            
                            
                              "
                            
                            
                              
                                "
                              
                              
                              
                              
                                +
                              
                              
                                 notifyurl 
                              
                              
                                +
                              
                              
                              
                              
                                "
                              
                              
                                >
                              
                              
                                "
                              
                              
                                ); Response.Write(
                              
                              
                                "
                              
                              
                              
                              
                                "
                              
                              
                                ); Response.Write(
                              
                              
                                "
                              
                              
                                
                                  "
                                
                                
                                
                                
                                  +
                                
                                
                                   aliayurl 
                                
                                
                                  +
                                
                                
                                
                                
                                  "
                                
                                
                                  >
                                
                                
                                  "
                                
                                
                                  ); Response.Write(
                                
                                
                                  "
                                
                                
                                  
                                    "
                                  
                                  
                                  
                                  
                                    +
                                  
                                  
                                     signtype 
                                  
                                  
                                    +
                                  
                                  
                                  
                                  
                                    "
                                  
                                  
                                    >
                                  
                                  
                                    "
                                  
                                  
                                    ); Response.Write(
                                  
                                  
                                    "
                                  
                                  
                                  
                                
                              
                            
                          
                        
                      
                    
                  
                
              
            
          
        
      
    


 
     
					 
					