支付宝接口小实现例子

foreve 2010-11-07

以下是摘抄的例子,望对你我都有点启发,

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //lbl_Num.Text = Request["OrderID"].ToString();
            //lbl_Prices.Text = Request["Money"].ToString();

            //按时构造订单号;
            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;
            string out_trade_no = currentTime.ToString("g");
            out_trade_no = out_trade_no.Replace("-", "");
            out_trade_no = out_trade_no.Replace(":", "");
            out_trade_no = out_trade_no.Replace(" ", "");
            lbl_Num.Text = out_trade_no;
        }
    }


    //进入支付宝支付
    protected void Button1_Click(object sender, EventArgs e)
    {
        string out_trade_no = "2009052610003";
        //业务参数赋值;
        string gateway = "https://www.alipay.com/cooperate/gateway.do?";    //'支付接口
        string service = "create_direct_pay_by_user";
        string partner = "2086102833883695";        //partner        合作伙伴ID            保留字段
        string sign_type = "MD5";
        string subject = "####购物网商品";    //subject        商品名称
        string body = "####购物网";        //body            商品描述    
        string payment_type = "1";                  //支付类型    
        string total_fee = "0.01";                      //总金额                    0.01~50000.00
        string show_url = "www.163.3com";
        string seller_email = "[email protected]";             //卖家账号
        string key = "7xlym97z460sz8psuidrslw7k3o986cz";              //partner账户的支付宝安全校验码
        string return_url = "http://www.163.com/Alipay_Return.aspx"; //服务器通知返回接口
        string notify_url = "http://www.163.com/Alipay_Notify.aspx"; //服务器通知返回接口
        string _input_charset = "utf-8";

        AliPay ap = new AliPay();
        string aliay_url = ap.CreatUrl(
            gateway,
            service,
            partner,
            sign_type,
            out_trade_no,
            subject,
            body,
            payment_type,
            total_fee,
            show_url,
            seller_email,
            key,
            return_url,
            _input_charset,
            notify_url
            );

        Response.Redirect(aliay_url);
    }

    //构建AliPay类
    public class AliPay
    {

        public static string GetMD5(string s, string _input_charset)
        {

            /// <summary>
            /// 与ASP兼容的MD5加密算法
            /// </summary>

            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).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();
        }

        public static string[] BubbleSort(string[] r)
        {
            /// <summary>
            /// 冒泡排序法
            /// </summary>

            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;
        }

        public string CreatUrl(
            string gateway,
            string service,
            string partner,
            string sign_type,
            string out_trade_no,
            string subject,
            string body,
            string payment_type,
            string total_fee,
            string show_url,
            string seller_email,
            string key,
            string return_url,
            string _input_charset,
            string notify_url
            )
        {
            /// <summary>
            /// created by sunzhizhi 2006.5.21,[email protected]。
            /// </summary>
            int i;

            //构造数组;
            string[] Oristr ={ 
                "service="+service, 
                "partner=" + partner, 
                "subject=" + subject, 
                "body=" + body, 
                "out_trade_no=" + out_trade_no, 
                "total_fee=" + total_fee, 
                "show_url=" + show_url,  
                "payment_type=" + payment_type, 
                "seller_email=" + seller_email, 
                "notify_url=" + notify_url,
                "_input_charset="+_input_charset,          
                "return_url=" + return_url
                };

            //进行排序;
            string[] Sortedstr = BubbleSort(Oristr);


            //构造待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(), _input_charset);

            //构造支付Url;
            char[] delimiterChars = { '=' };
            StringBuilder parameter = new StringBuilder();
            parameter.Append(gateway);
            for (i = 0; i < Sortedstr.Length; i++)
            {

                parameter.Append(Sortedstr[i].Split(delimiterChars)[0] + "=" + HttpUtility.UrlEncode(Sortedstr[i].Split(delimiterChars)[1]) + "&");
            }

            parameter.Append("sign=" + sign + "&sign_type=" + sign_type);


            //返回支付Url;
            return parameter.ToString();

        }
    }

以下是另外一位朋友的代码:

protected void BtnAlipay_Click(object sender, EventArgs e)
    {
        //业务参数赋值;
        string gateway = "https://www.alipay.com/cooperate/gateway.do?";    //支付接口
        string service = "create_direct_pay_by_user";                       //服务名称,这个是识别是何接口实现何功能的标识,请勿修改

        string seller_email = "";                     //商家签约时的支付宝帐号,即收款的支付宝帐号
        string sign_type = "MD5";                                           //加密类型,签名方式“不用改”
        string key = "";                    //安全校验码,与partner是一组,获取方式是:用签约时支付宝帐号登陆支付宝网站www.alipay.com,在商家服务我的商家里即可查到。
        string partner = "";                                //商户ID,合作身份者ID,合作伙伴ID
        string _input_charset = "utf-8";                                    //编码类型,完全根据客户自身的项目的编码格式而定,千万不要填错。否则极其容易造成MD5加密错误。

        string show_url = "http://www.alipay.com/";                         //展示地址,即在支付页面时,商品名称旁边的“详情”的链接地址。

        string out_trade_no = TxtOrderno.Text.Trim();                       //客户自己的订单号,订单号必须在自身订单系统中保持唯一性
        string subject = TxtSubject.Text.Trim();                            //商品名称,也可称为订单名称,该接口并不是单一的只能买一样东西,可把一次支付当作一次下订单
        string body = TxtBody.Text.Trim();                                  //商品描述,即备注
        string total_fee = TxtTotal_fee.Text.Trim();                        //商品价格,也可称为订单的总金额

        //服务器通知url(Alipay_Notify.aspx文件所在路经),必须是完整的路径地址
        string notify_url = "http://localhost:8978/direct_vs2005_utf/Alipay_Notify.aspx";
        //服务器返回url(Alipay_Return.aspx文件所在路经),必须是完整的路径地址
        string return_url = "http://localhost:8978/direct_vs2005_utf/Alipay_Return.aspx";

        //构造数组;
        //以下数组即是参与加密的参数,若参数的值不允许为空,若该参数为空,则不要成为该数组的元素
        string[] para ={
        "service="+service,
        "partner=" + partner,
        "seller_email=" + seller_email,
        "out_trade_no=" + out_trade_no,
        "subject=" + subject,
        "body=" + body,
        "total_fee=" + total_fee, 
        "show_url=" + show_url,
        "payment_type=1",
        "notify_url=" + notify_url,
        "return_url=" + return_url,
        "_input_charset="+_input_charset
        };

        //支付URL生成
        string aliay_url = AliPay.CreatUrl(
            //gateway,//GET方式传递参数时请去掉注释
            para,
            _input_charset,
            sign_type,
            key
            );

        //以下是GET方式传递参数
        //Response.Redirect(aliay_url);


        //以下是POST方式传递参数
        Response.Write("<form name='alipaysubmit' method='post' action='https://www.alipay.com/cooperate/gateway.do?_input_charset=utf-8'>");
        Response.Write("<input type='hidden' name='service' value=" + service + ">");
        Response.Write("<input type='hidden' name='partner' value=" + partner + ">");
        Response.Write("<input type='hidden' name='seller_email' value=" + seller_email + ">");
        Response.Write("<input type='hidden' name='out_trade_no' value=" + out_trade_no + ">");
        Response.Write("<input type='hidden' name='subject' value=" + subject + ">");
        Response.Write("<input type='hidden' name='body' value=" + body + ">");
        Response.Write("<input type='hidden' name='total_fee' value=" + total_fee + ">");
        Response.Write("<input type='hidden' name='show_url' value=" + show_url + ">");
        Response.Write("<input type='hidden' name='return_url' value=" + return_url + ">");
        Response.Write("<input type='hidden' name='notify_url' value=" + notify_url + ">");
        Response.Write("<input type='hidden' name='payment_type' value=1>");
        Response.Write("<input type='hidden' name='sign' value=" + aliay_url + ">");
        Response.Write("<input type='hidden' name='sign_type' value=" + sign_type + ">");
        Response.Write("</form>");
        Response.Write("<script>");
        Response.Write("document.alipaysubmit.submit()");
        Response.Write("</script>");
    }

相关推荐