asp.net關於在線支付的實現過程


此例分三個頁面分別為:default.aspx用於支付顯示后的頁面包括商品名稱,支付金額等。

SubmitPayPage.aspx為提交支付的頁面 ,跳轉到支付寶官方網站

NoticeReturn.aspx系統返回頁面(對賬頁面)

1、default.aspx為HTML代碼 沒有內容顯示 

而default.aspx.cs代碼如下:

using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web.UI;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 支付寶的網關
        string alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?";
        // 用戶注冊支付寶時生成的校驗碼(必須填寫自己的)
        string key = "xxxxxxx";
        // 頁面編碼格式
        string _input_charset = "utf-8";
        // 用戶注冊支付寶時生成的合作伙伴id(必須填寫自己的)
        string partner = "xxxxxx";
        alipayNotifyURL = alipayNotifyURL + "service=create_digital_goods_trade_p" + "&partner=" + partner +
                          "&notify_id=" + Request.QueryString["notify_id"];
        // 獲取支付寶ATN返回結果,true是正確的訂單信息,false 是無效的
        string responseTxt = Get_Http(alipayNotifyURL, 120000);
        int i;
        NameValueCollection coll;
        // 在集合中裝載返回信息
        coll = Request.QueryString;
        // 將所有的鍵值保存在數組中
        String[] requestarr = coll.AllKeys;
        // 進行排序
        string[] Sortedstr = BubbleSort(requestarr);
        for (i = 0; i < Sortedstr.Length; i++)
        {
            Response.Write("Form: " + Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]] + "<br>");
        }
        // 構造待md5摘要字符串
        StringBuilder prestr = new StringBuilder();
        for (i = 0; i < Sortedstr.Length; i++)
        {
            if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type")
            {
                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 = GetMD5(prestr.ToString(), _input_charset);
        string sign = Request.QueryString["sign"];
        // 測試返回的結果
        // Response.Write(prestr.ToString());  
        // 驗證支付發過來的消息,簽名是否正確
        if (mysign == sign && responseTxt == "true")
        {
            // 此時可以更新網站的數據,比如商品的減少等等。
            Response.Write("success"); // 返回給支付寶消息,成功
        }
        else
        {
            Response.Write("fail");
        }
    }

    public static string GetMD5(string s, string _input_charset)
    {
        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)
    {
        int i, j;
        string temp;
        bool exchange;
        // 最多做R.Length-1趟排序 
        for (i = 0; i < r.Length; i++)
        {
            // 本趟排序開始前,交換標志應為假
            exchange = false;
            for (j = r.Length - 2; j >= i; j--)
            {
                // 交換條件
                if (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;
    }

    // 獲取遠程服務器ATN結果
    public String Get_Http(String a_strUrl, int timeout)
    {
        string strResult;
        try
        {
            // 創建訪問頁面
            HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create(a_strUrl);
            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;
    }
}

  2、SubmitPayPage.aspx 代碼為空

后台代碼:

using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web.UI;

public partial class NoticeReturn : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?";
        // partner合作伙伴id(必須填寫)
        string partner = "xxxxxxxxxx";
        // partner 的對應交易安全校驗碼(必須填寫)
        string key = "xxxxxxxx";
        alipayNotifyURL = alipayNotifyURL + "service=create_digital_goods_trade_p" + "&partner=" + partner +
                          "&notify_id=" + Request.Form["notify_id"];
        // 獲取支付寶ATN返回結果,true是正確的訂單信息,false 是無效的
        string responseTxt = Get_Http(alipayNotifyURL, 120000);
        int i;
        NameValueCollection coll;
        // 在集合中裝載返回信息
        coll = Request.Form;
        // 將所有的鍵值保存在數組中
        String[] requestarr = coll.AllKeys;
        // 進行排序
        string[] Sortedstr = BubbleSort(requestarr);
        // 構造待md5摘要字符串
        string prestr = "";
        for (i = 0; i < Sortedstr.Length; i++)
        {
            if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type")
            {
                if (i == Sortedstr.Length - 1)
                {
                    prestr = prestr + Sortedstr[i] + "=" + Request.Form[Sortedstr[i]];
                }
                else
                {
                    prestr = prestr + Sortedstr[i] + "=" + Request.Form[Sortedstr[i]] + "&";
                }
            }
        }
        prestr = prestr + key;
        string mysign = GetMD5(prestr);
        string sign = Request.Form["sign"];
        // 驗證支付發過來的消息,簽名是否正確
        if (mysign == sign && responseTxt == "true")
        {
            // 判斷支付狀態TRADE_FINISHED(文檔中有枚舉表可以參考) 
            if (Request.Form["trade_status"] == "WAIT_SELLER_SEND_GOODS")
            {
                // 更新自己數據庫的訂單語句
                // 返回給支付寶消息,成功
                Response.Write("success");
            }
            else
            {
                Response.Write("fail");
            }
        }
    }

    public static string GetMD5(string s)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] t = md5.ComputeHash(Encoding.GetEncoding("utf-8").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)
    {
        int i, j; // 交換標志 
        string temp;
        bool exchange;
        // 最多做R.Length-1趟排序
        for (i = 0; i < R.Length; i++)
        {
            // 本趟排序開始前,交換標志應為假
            exchange = false;
            for (j = R.Length - 2; j >= i; j--)
            {
                // 交換條件
                if (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;
    }

    // 獲取遠程服務器ATN結果
    public String Get_Http(String a_strUrl, int timeout)
    {
        string strResult;
        try
        {
            // 創建訪問頁面
            HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create(a_strUrl);
            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;
    }
}

  3、NoticeReturn.aspx 代碼為空;

后台代碼如下:

using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web.UI;

public partial class NoticeReturn : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?";
        // partner合作伙伴id(必須填寫)
        string partner = "xxxxxxxxxx";
        // partner 的對應交易安全校驗碼(必須填寫)
        string key = "xxxxxxxx";
        alipayNotifyURL = alipayNotifyURL + "service=create_digital_goods_trade_p" + "&partner=" + partner +
                          "&notify_id=" + Request.Form["notify_id"];
        // 獲取支付寶ATN返回結果,true是正確的訂單信息,false 是無效的
        string responseTxt = Get_Http(alipayNotifyURL, 120000);
        int i;
        NameValueCollection coll;
        // 在集合中裝載返回信息
        coll = Request.Form;
        // 將所有的鍵值保存在數組中
        String[] requestarr = coll.AllKeys;
        // 進行排序
        string[] Sortedstr = BubbleSort(requestarr);
        // 構造待md5摘要字符串
        string prestr = "";
        for (i = 0; i < Sortedstr.Length; i++)
        {
            if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type")
            {
                if (i == Sortedstr.Length - 1)
                {
                    prestr = prestr + Sortedstr[i] + "=" + Request.Form[Sortedstr[i]];
                }
                else
                {
                    prestr = prestr + Sortedstr[i] + "=" + Request.Form[Sortedstr[i]] + "&";
                }
            }
        }
        prestr = prestr + key;
        string mysign = GetMD5(prestr);
        string sign = Request.Form["sign"];
        // 驗證支付發過來的消息,簽名是否正確
        if (mysign == sign && responseTxt == "true")
        {
            // 判斷支付狀態TRADE_FINISHED(文檔中有枚舉表可以參考) 
            if (Request.Form["trade_status"] == "WAIT_SELLER_SEND_GOODS")
            {
                // 更新自己數據庫的訂單語句
                // 返回給支付寶消息,成功
                Response.Write("success");
            }
            else
            {
                Response.Write("fail");
            }
        }
    }

    public static string GetMD5(string s)
    {
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] t = md5.ComputeHash(Encoding.GetEncoding("utf-8").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)
    {
        int i, j; // 交換標志 
        string temp;
        bool exchange;
        // 最多做R.Length-1趟排序
        for (i = 0; i < R.Length; i++)
        {
            // 本趟排序開始前,交換標志應為假
            exchange = false;
            for (j = R.Length - 2; j >= i; j--)
            {
                // 交換條件
                if (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;
    }

    // 獲取遠程服務器ATN結果
    public String Get_Http(String a_strUrl, int timeout)
    {
        string strResult;
        try
        {
            // 創建訪問頁面
            HttpWebRequest myReq = (HttpWebRequest) WebRequest.Create(a_strUrl);
            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;
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM