C# 支付寶支付

原文鏈接:https://www.cnblogs.com/superMay/p/9474214.html

c#支付寶支付

1、進入支付寶去申請appid和密鑰 https://open.alipay.com/platform/home.htm 支付寶開放平臺

2、下載sdk根據自己是啥開發語言下載啥 

3、開始寫代碼

    後臺請求參數和方式

    

 

/// <summary>

        /// 支付寶支付

        /// </summary>

        /// <param name="model"></param>

        /// <param name="configPath"></param>

        /// <returns></returns>

        public  string Alipay(HttpContext context)

        {

            string OrderNumber = "" ;//訂單號,此單號必須唯一

            string app_id = ""; //"你的app_id";

            string merchant_private_key = "";//"支付寶私鑰";

            string alipay_public_key = "";// "支付寶公鑰";

            string timeout_express = "30m";//訂單有效時間(分鐘)

            string postUrl = "https://openapi.alipay.com/gateway.do";//這個地址都是固定的

            string sign_type = "RSA2";//加簽方式 有兩種RSA和RSA2 我這裏使用的RSA2(支付寶推薦的)

            string version = "1.0";//固定值 不用改

            string format = "json";//固定值

            string Amount = "0.01";//訂單金額

            string method = "alipay.trade.wap.pay";//調用接口 固定值 不用改

            IAopClient client = new DefaultAopClient(postUrl, app_id, merchant_private_key, format, version, sign_type, alipay_public_key, "UTF-8", false);

            AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();

            request.SetNotifyUrl("Notify_url.aspx");//外網能訪問的後臺回調地址

            request.SetReturnUrl("Return_url.aspx");//外網能訪問的前臺回調地址

            request.BizContent = "{" +

            "    \"body\":\"對一筆交易的具體描述信息。如果是多種商品,請將商品描述字符串累加傳給body。\"," +

            "    \"subject\":\"商品描述\"," +

            "    \"out_trade_no\":\"" + OrderNumber + "\"," +   //商家唯一訂單,填寫你項目裏生成的唯一訂單號

            "    \"timeout_express\":\"" + timeout_express + "\"," +

            "    \"total_amount\":" + Amount + "," +

            "    \"product_code\":\"" + method + "\"" +

            "  }";

            AlipayTradeWapPayResponse response = client.pageExecute(request);

            string form = response.Body.Substring(0, response.Body.IndexOf("<script>"));

            return form;

        }

 //

同步回調地址

 

using Aop.Api.Util;

 

using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class Return_url : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        /* 實際驗證過程建議商戶添加以下校驗。

        1、商戶需要驗證該通知數據中的out_trade_no是否爲商戶系統中創建的訂單號,

        2、判斷total_amount是否確實爲該訂單的實際金額(即商戶訂單創建時的金額),

        3、校驗通知中的seller_id(或者seller_email) 是否爲out_trade_no這筆單據的對應的操作方(有的時候,一個商戶可能有多個seller_id/seller_email)

        4、驗證app_id是否爲該商戶本身。

        */

        // 應用ID,您的APPID

        string app_id = "";

        // 支付寶網關

        string gatewayUrl = "";

        // 商戶私鑰,您的原始格式RSA私鑰

        string private_key = "";

        // 支付寶公鑰,查看地址:https://openhome.alipay.com/platform/keyManage.htm 對應APPID下的支付寶公鑰。

        string alipay_public_key = "";

        // 簽名方式

        string sign_type = "RSA2";

        // 編碼格式

        string charset = "UTF-8";

        Dictionary<string, string> sArray = GetRequestGet();

        if (sArray.Count != 0)

        {

            bool flag = AlipaySignature.RSACheckV1(sArray, alipay_public_key, charset, sign_type, false);

            if (flag)

            {

                Response.Write("同步驗證通過");

            }

            else

            {

                Response.Write("同步驗證失敗");

            }

        }

    }

    public Dictionary<string, string> GetRequestGet()

    {

        int i = 0;

        Dictionary<string, string> sArray = new Dictionary<string, string>();

        NameValueCollection coll;

        //coll = Request.Form;

        coll = Request.QueryString;

        String[] requestItem = coll.AllKeys;

        for (i = 0; i < requestItem.Length; i++)

        {

            sArray.Add(requestItem[i], Request.QueryString[requestItem[i]]);

        }

        return sArray;

    }

}

 

 

支付完成異步通知接收地址 

using Aop.Api.Util;

 

using System;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

public partial class Notify_url : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        /* 實際驗證過程建議商戶添加以下校驗。

        1、商戶需要驗證該通知數據中的out_trade_no是否爲商戶系統中創建的訂單號,

        2、判斷total_amount是否確實爲該訂單的實際金額(即商戶訂單創建時的金額),

        3、校驗通知中的seller_id(或者seller_email) 是否爲out_trade_no這筆單據的對應的操作方(有的時候,一個商戶可能有多個seller_id/seller_email)

        4、驗證app_id是否爲該商戶本身。

        */

          // 應用ID,您的APPID

        string app_id = "";

    // 支付寶網關

   string gatewayUrl = "";

    // 商戶私鑰,您的原始格式RSA私鑰

   string private_key = "";

    // 支付寶公鑰,查看地址:https://openhome.alipay.com/platform/keyManage.htm 對應APPID下的支付寶公鑰。

   string alipay_public_key = "";

    // 簽名方式

     string sign_type = "RSA2";

    // 編碼格式

     string charset = "UTF-8";

        Dictionary<string, string> sArray = GetRequestPost();

        if (sArray.Count != 0)

        {

            bool flag = AlipaySignature.RSACheckV1(sArray, alipay_public_key, charset, sign_type, false);

            if (flag)

            {

                //交易狀態

                //判斷該筆訂單是否在商戶網站中已經做過處理

                //如果沒有做過處理,根據訂單號(out_trade_no)在商戶網站的訂單系統中查到該筆訂單的詳細,並執行商戶的業務程序

                //請務必判斷請求時的total_amount與通知時獲取的total_fee爲一致的

                //如果有做過處理,不執行商戶的業務程序

                //注意:

                //退款日期超過可退款期限後(如三個月可退款),支付寶系統發送該交易狀態通知

                string trade_status = Request.Form["trade_status"];

                Response.Write("success");

            }

            else

            {

                Response.Write("fail");

            }

        }

    }

    public Dictionary<string, string> GetRequestPost()

    {

        int i = 0;

        Dictionary<string, string> sArray = new Dictionary<string, string>();

        NameValueCollection coll;

        //coll = Request.Form;

        coll = Request.Form;

        String[] requestItem = coll.AllKeys;

        for (i = 0; i < requestItem.Length; i++)

        {

            sArray.Add(requestItem[i], Request.Form[requestItem[i]]);

        }

        return sArray;

    }

}

 

 

   頁面上的請求方式(這裏爲了方便寫的非常簡單)

<head runat="server">

 <meta name="viewport" content="width=device-width" />

    <script src="Scripts/jquery-1.7.1.js"></script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:Button ID="Button1" runat="server" Text="支付啊" OnClick="Button1_Click" />

    </div>

        <div id="formDiv" style="display:none;">

        </div>

    </div>

          <script type="text/javascript">

              $(function(){

                  $("#Button1").click(function () {

                      $.ajax({

                          url: "Handler1.ashx",  //頁面上是到一般處理程序中處理的

                          data: {"action": "Alipay" },

                          type: "POST",

                          cache: false,

                          async: true,

                          success: function (data) {

                              alert(data);

                              if (data != "") {

                                  $("#formDiv").append(data);

                                  $("#alipaysubmit").submit(); //返回的是form表單 直接提交就可以了.

                              }

                          }

                      });

                                        });

                  });

              });

    </script>

    </form>

</body>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章