C#.net 支付宝接口

生成URL链接
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Collections;
  5using System.Web;
  6using System.Web.Security;
  7using System.Web.UI;
  8using System.Web.UI.WebControls;
  9using System.Web.UI.WebControls.WebParts;
 10using System.Web.UI.HtmlControls;
 11using System.Text;
 12
 13public partial class pay : System.Web.UI.Page
 14{
 15    protected void Page_Load(object sender, EventArgs e)
 16    {
 17        Encoding gb2312 = Encoding.GetEncoding("gb2312");
 18        Response.ContentEncoding = gb2312;
 19        Request.ContentEncoding = gb2312;
 20    }

 21    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
 22    {
 23        if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
 24        {
 25            Vlike_DB_read a = new Vlike_DB_read();
 26            string zfb_ddh=a.get_zfb_id(Membership.GetUser().UserName,Convert.ToInt32(DropDownList1.SelectedValue));
 27            Response.Redirect(img("0001""包月""影视包月  10元/月", zfb_ddh, "10.00", Membership.GetUser().UserName.ToUpper(), DropDownList1.SelectedValue));
 28        }

 29        else 
 30        {
 31            Response.Write("<script type='text/javascript'>alert('请先登陆!');</script>");
 32        }

 33    }

 34
 35    private string img(string strcmd, string strSub, string strSubinfo, string strid, string strMoney, string strUser, string strNum)
 36    {
 37        string strsellerEmail = "341081@qq.com";                            //卖家支付宝帐号
 38        string strAc = "";        //卖家支付宝安全校验码
 39        string INTERFACE_URL = "https://www.alipay.com/payto:";
 40        string strCmd = strcmd;                                    //命令字
 41        string strSubject = strSub;                                    //商品名
 42        string strBody = strSubinfo;                                //商品描述
 43        string strOrder_no = strid;                                    //商户订单号
 44        string strPrice = strMoney;                                //商品单价 0.01~50000.00
 45        string rurl = "http://";            //商品展示网址
 46        string strType = "2";                                    //type支付类型    1:商品购买2:服务购买3:网络拍卖4:捐赠
 47        string strNumber = strNum;                                    //购买数量
 48        string strTransport = "3";                                    //发货方式        1:平邮2:快递3:虚拟物品
 49        string strOrdinary_fee = "";                                        //平邮运费
 50        string strExpress_fee = "";                                        //快递运费
 51        string strReadOnly = "true";                                    //交易信息是否只读
 52        string strBuyer_msg = "";                                        //买家给卖家的留言
 53
 54        string strBuyer = "";                                        //买家EMAIL
 55        string strBuyer_name = strUser;                                    //买家姓名
 56        string strBuyer_address = "";                                        //买家地址
 57        string strBuyer_zipcode = "";                                        //买家邮编
 58        string strBuyer_tel = "";                                        //买家电话号码
 59        string strBuyer_mobile = "";                                        //买家手机号码
 60        string strPartner = "";                                        //合作伙伴ID    保留字段
 61        return CreatUrl(strsellerEmail, strAc, INTERFACE_URL, strCmd, strSubject, strBody,
 62            strOrder_no, strPrice, rurl, strType, strNumber, strTransport,
 63            strOrdinary_fee, strExpress_fee, strReadOnly, strBuyer_msg, strBuyer,
 64            strBuyer_name, strBuyer_address, strBuyer_zipcode, strBuyer_tel,
 65            strBuyer_mobile, strPartner);
 66    }

 67    private string CreatUrl(string strsellerEmail, string strAc, string INTERFACE_URL, string strCmd, string strSubject, string strBody, string strOrder_no, string strPrice, string rurl, string strType, string strNumber, string strTransport, string strOrdinary_fee, string strExpress_fee, string strReadOnly, string strBuyer_msg, string strBuyer, string strBuyer_name, string strBuyer_address, string strBuyer_zipcode, string strBuyer_tel, string strBuyer_mobile, string strPartner)
 68    {   //以下参数值不能留空
 69
 70        string str2CreateAc = "";
 71        str2CreateAc += "cmd" + strCmd + "subject" + strSubject;
 72        str2CreateAc += "body" + strBody;
 73        str2CreateAc += "order_no" + strOrder_no;
 74        str2CreateAc += "price" + strPrice;
 75        str2CreateAc += "url" + rurl;
 76        str2CreateAc += "type" + strType;
 77        str2CreateAc += "number" + strNumber;
 78        str2CreateAc += "transport" + strTransport;
 79        str2CreateAc += "ordinary_fee" + strOrdinary_fee;
 80        str2CreateAc += "express_fee" + strExpress_fee;
 81        str2CreateAc += "readonly" + strReadOnly;
 82        str2CreateAc += "buyer_msg" + strBuyer_msg;
 83        str2CreateAc += "seller" + strsellerEmail;
 84        str2CreateAc += "buyer" + strBuyer;
 85        str2CreateAc += "buyer_name" + strBuyer_name;
 86        str2CreateAc += "buyer_address" + strBuyer_address;
 87        str2CreateAc += "buyer_zipcode" + strBuyer_zipcode;
 88        str2CreateAc += "buyer_tel" + strBuyer_tel;
 89        str2CreateAc += "buyer_mobile" + strBuyer_mobile;
 90        str2CreateAc += "partner" + strPartner;
 91        str2CreateAc += strAc;
 92
 93
 94        string acCode = GetMD5(str2CreateAc);
 95
 96        string parameter = "";
 97
 98        parameter += INTERFACE_URL + strsellerEmail + "?cmd=" + strCmd;
 99        parameter += "&subject=" + Server.UrlEncode(strSubject);
100        parameter += "&body=" + Server.UrlEncode(strBody);
101        parameter += "&order_no=" + strOrder_no;
102        parameter += "&url=" + rurl;
103        parameter += "&price=" + strPrice;
104        parameter += "&type=" + strType;
105        parameter += "&number=" + strNumber;
106        parameter += "&transport=" + strTransport;
107        parameter += "&ordinary_fee=" + strOrdinary_fee;
108        parameter += "&express_fee=" + strExpress_fee;
109        parameter += "&readonly=" + strReadOnly;
110        parameter += "&buyer_msg=" + strBuyer_msg;
111        parameter += "&buyer=" + strBuyer;
112        parameter += "&buyer_name=" + Server.UrlEncode(strBuyer_name);
113        parameter += "&buyer_address=" + strBuyer_address;
114        parameter += "&buyer_zipcode=" + strBuyer_zipcode;
115        parameter += "&buyer_tel=" + strBuyer_tel;
116        parameter += "&buyer_mobile=" + strBuyer_mobile;
117        parameter += "&partner=" + strPartner;
118        parameter += "&ac=" + acCode;
119
120        return parameter;
121    }

122
123    private static string GetMD5(string s)
124    {
125
126        System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
127        byte[] t = md5.ComputeHash(System.Text.Encoding.GetEncoding("gb2312").GetBytes(s));
128        System.Text.StringBuilder sb = new System.Text.StringBuilder(32);
129        for (int i = 0; i < t.Length; i++)
130        {
131            sb.Append(t[i].ToString("x").PadLeft(2'0'));
132        }

133        return sb.ToString();
134    }

135}

136

接收支付宝信息并进行相应操作
  1<%@ Import NameSpace="System.Data.SqlClient"%>
  2<%@ Import NameSpace="System.Data"%>
  3<%@ Import NameSpace="System.Web"%>
  4<%@ Import NameSpace="System.IO"%>
  5<%@ Import NameSpace="System.Net"%>
  6<%@ Import NameSpace="System.Text"%>
  7<%@ Import NameSpace="System.Security.Cryptography"%>
  8<%@ Page Language="C#"%>
  9
 10<script language="C#" runat="server">     
 11        private string returnTxt            = "N";                                                    //返回给支付宝通知接口的结果
 12        private string alipayNotifyURL        = "http://notify.alipay.com/trade/notify_query.do?";    //支付宝查询接口URL
 13        private string myalipayEmail        = "341081@qq.com";                                        //商户的支付宝Email
 14        private string constPaySecurityCode = "";                                                   //
 15        private SqlConnection conn = new SqlConnection();
 16        
 17        private string GetMD5(string s)
 18        {
 19            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
 20            byte[] t = md5.ComputeHash(System.Text.Encoding.GetEncoding("gb2312").GetBytes(s));
 21            StringBuilder sb = new StringBuilder(32);
 22            for (int i = 0; i < t.Length; i++)
 23            {
 24                sb.Append(t[i].ToString("x").PadLeft(2'0'));
 25            }

 26            return sb.ToString();
 27        }

 28        private String Get_Http(String a_strUrl,int timeout)
 29        {
 30            string strResult ;         
 31            try
 32            {
 33                System.Net.HttpWebRequest myReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(a_strUrl) ;
 34                myReq.Timeout = timeout;
 35                System.Net.HttpWebResponse HttpWResp = (System.Net.HttpWebResponse)myReq.GetResponse();
 36                Stream myStream = HttpWResp.GetResponseStream () ;
 37                StreamReader sr = new StreamReader(myStream , System.Text.Encoding.Default);
 38                System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
 39                while (-1 != sr.Peek())
 40                {
 41                    strBuilder.Append(sr.ReadLine()+"\r\n");
 42                }

 43                strResult = strBuilder.ToString();
 44            }

 45            catch(Exception exp)
 46            {
 47
 48                strResult = "错误:" + exp.Message ;
 49            }

 50            return strResult ;
 51        }

 52
 53        public void send(string sql)
 54        {
 55            string dbconn = "data source=127.0.0.1;UID=Cm;PWD=fallenangel;database=text";
 56            conn = new SqlConnection(dbconn);
 57            conn.Open();
 58            SqlCommand cmd = new SqlCommand(sql, conn);
 59            int aa = cmd.ExecuteNonQuery();
 60            conn.Close();
 61        }

 62        
 63        private void Page_Load(object sender, System.EventArgs e)
 64        {       
 65            //检查支付宝通知接口传递过来的参数是否合法
 66            string msg_id            = System.Web.HttpContext.Current.Request["msg_id"];
 67            string order_no            = System.Web.HttpContext.Current.Request["order_no"];
 68            string gross            = System.Web.HttpContext.Current.Request["gross"];
 69            string buyer_email        = System.Web.HttpContext.Current.Request["buyer_email"];
 70            string buyer_name        = System.Web.HttpContext.Current.Request["buyer_name"];
 71            string buyer_address    = System.Web.HttpContext.Current.Request["buyer_address"];
 72            string buyer_zipcode    = System.Web.HttpContext.Current.Request["buyer_zipcode"];
 73            string buyer_tel        = System.Web.HttpContext.Current.Request["buyer_tel"];
 74            string buyer_mobile        = System.Web.HttpContext.Current.Request["buyer_mobile"];
 75            string action            = System.Web.HttpContext.Current.Request["action"];
 76            string s_date            = System.Web.HttpContext.Current.Request["date"];
 77            string ac                = System.Web.HttpContext.Current.Request["ac"];
 78            string notify_type        = System.Web.HttpContext.Current.Request["notify_type"];
 79
 80            alipayNotifyURL    = alipayNotifyURL + "msg_id=" + msg_id + "&email=" + myalipayEmail + "&order_no=" + order_no;
 81
 82            
 83            //获取支付宝ATN返回结果,true和false都是正确的订单信息,invalid 是无效的
 84            string responseTxt=Get_Http(alipayNotifyURL,120000);
 85
 86            string Str = "msg_id" + msg_id + "order_no" + order_no + "gross" + gross  + "buyer_email" + buyer_email + "buyer_name" + buyer_name + "buyer_address" + buyer_address + "buyer_zipcode" + buyer_zipcode + "buyer_tel" + buyer_tel + "buyer_mobile" + buyer_mobile + "action" + action  + "date" + s_date + constPaySecurityCode ;  
 87            
 88            string ac_code=GetMD5(Str);
 89                    
 90            if(action=="test"//支付宝接口测试是否有效
 91            {
 92                returnTxt = "Y";
 93            }

 94            if(action=="sendOff")  //发货通知
 95            {
 96                if(responseTxt.Substring(0,4)=="true" || responseTxt.Substring(0,4)=="fals")//ATN,验证消息是否支付宝发过来 
 97                {
 98                    if(ac_code==ac)//验证消息是否被修改
 99                    {
100                        //数据库操作
101                    }

102                }

103            }

104            if(action=="checkOut")  //交易完成通知 
105            {
106                returnTxt = "N";
107                if(responseTxt.Substring(0,4)=="true" || responseTxt.Substring(0,4)=="fals")//ATN,验证消息是否支付宝发过来 
108                {
109                    if(ac_code==ac)//验证消息是否被修改
110                    {
111                        //数据库操作    
112                    }

113                }

114            }

115            System.Web.HttpContext.Current.Response.Write(returnTxt);
116        }

117</script>
118

posted on 2006-05-17 16:40  苍冥  阅读(11053)  评论(4编辑  收藏  举报

导航