アリペイインタフェースの小実現例


以下は抜粋の例ですが、あなたには少し啓発されています.

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