微信小程序 c#後台支付結果回調 - 新聞資訊 - 雲南小程序開發|雲南軟件開發|雲南網站建設-昆明融晨信息技術有限公司

159-8711-8523

雲南網建設/小程序開發/軟件開發

知識

不(bù)管是(shì)網站,軟件還是(shì)小程序,都要(yào / yāo)直接或間接能爲(wéi / wèi)您産生價值,我們在(zài)追求其視覺表現的(de)同時(shí),更側重于(yú)功能的(de)便捷,營銷的(de)便利,運營的(de)高效,讓網站成爲(wéi / wèi)營銷工具,讓軟件能切實提升企業内部管理水平和(hé / huò)效率。優秀的(de)程序爲(wéi / wèi)後期升級提供便捷的(de)支持!

您當前位置>首頁 » 新聞資訊 » 小程序相關 >

微信小程序 c#後台支付結果回調

發表時(shí)間:2022-9-6

發布人(rén):融晨科技

浏覽次數:94

又爲(wéi / wèi)大(dà)家帶來(lái)簡單的(de)c#後台支付結果回調方法,首先還是(shì)要(yào / yāo)去微信官網下載模闆(WxPayAPI),将模闆(WxPayAPI)添加到(dào)服務器上(shàng),然後在(zài)打開WxPayAPI項目中的(de)example文件下的(de)

NativeNotifyPage.aspx打開網頁中的(de)代碼頁如圖:

将以(yǐ)下代碼加入進去就(jiù)能完成:

{
    public static string wxJsApiParam { get; set; } //前段顯示
    public string return_result = "";
    protected void Page_Load(object sender, EventArgs e)
    {
       Response.ContentType = "text/plain";
        Response.Write("Hello World");

        LogHelper.WriteLog(typeof(NativeNotifyPage), "可以(yǐ)運行1-1");

        String xmlData = http://www.wxapp-union.com/getPostStr();//獲取請求數據  
        if (xmlData =="")
        {

        }
        else
        {
            var dic = new Dictionary<string, string>
             {
              {"return_code", "SUCCESS"},
              {"return_msg","OK"}

             };
            var sb = new StringBuilder();
            sb.Append("<xml>");


            foreach (var d in dic)
            {
                sb.Append("<" + d.Key + ">" + d.Value + "</" + d.Key + ">");
            }
            sb.Append("</xml>");





            //把數據重新返回給客戶端  
            DataSet ds = new DataSet();
            StringReader stram = new StringReader(xmlData);
            XmlTextReader datareader = new XmlTextReader(stram);
            ds.ReadXml(datareader);
            if (ds.Tables[0].Rows[0]["return_code"].ToString() == "SUCCESS")
            {

                LogHelper.WriteLog(typeof(NativeNotifyPage), "數據能返回");


                string wx_appid = "";//微信開放平台審核通過的(de)應用APPID  
                string wx_mch_id = "";//微信支付分配的(de)商戶号  

                string wx_nonce_str = "";//     随機字符串,不(bù)長于(yú)32位  
                string wx_sign = "";//簽名,詳見簽名算法  
                string wx_result_code = "";//SUCCESS/FAIL  

                string wx_return_code = "";
                string wx_openid = "";//用戶在(zài)商戶appid下的(de)唯一标識  
                string wx_is_subscribe = "";//用戶是(shì)否關注公衆賬号,Y-關注,N-未關注,僅在(zài)公衆賬号類型支付有效  
                string wx_trade_type = "";//    APP  
                string wx_bank_type = "";//     銀行類型,采用字符串類型的(de)銀行标識,銀行類型見銀行列表  
                string wx_fee_type = "";//  貨币類型,符合ISO4217标準的(de)三位字母代碼,默認人(rén)民币:CNY,其他(tā)值列表詳見貨币類型  


                string wx_transaction_id = "";//微信支付訂單号  
                string wx_out_trade_no = "";//商戶系統的(de)訂單号,與請求一緻。  
                string wx_time_end = "";//  支付完成時(shí)間,格式爲(wéi / wèi)yyyyMMddHHmmss,如2009年12月25日9點10分10秒表示爲(wéi / wèi)20091225091010。其他(tā)詳見時(shí)間規則  
                int wx_total_fee = 1;//    訂單總金額,單位爲(wéi / wèi)分  
                int wx_cash_fee = 1;//現金支付金額訂單現金支付金額,詳見支付金額  


                #region  數據解析  
                //列 是(shì)否存在(zài)  
                string signstr = "";//需要(yào / yāo)前面的(de)字符串  
                                    //wx_appid  
                if (ds.Tables[0].Columns.Contains("appid"))
                {
                    wx_appid = ds.Tables[0].Rows[0]["appid"].ToString();
                    if (!string.IsNullOrEmpty(wx_appid))
                    {
                        signstr += "appid=" + wx_appid;
                    }
                }

                //wx_bank_type  
                if (ds.Tables[0].Columns.Contains("bank_type"))
                {
                    wx_bank_type = ds.Tables[0].Rows[0]["bank_type"].ToString();
                    if (!string.IsNullOrEmpty(wx_bank_type))
                    {
                        signstr += "&bank_type=" + wx_bank_type;
                    }
                }
                //wx_cash_fee  
                if (ds.Tables[0].Columns.Contains("cash_fee"))
                {
                    wx_cash_fee = Convert.ToInt32(ds.Tables[0].Rows[0]["cash_fee"].ToString());

                    signstr += "&cash_fee=" + wx_cash_fee;
                }

                //wx_fee_type  
                if (ds.Tables[0].Columns.Contains("fee_type"))
                {
                    wx_fee_type = ds.Tables[0].Rows[0]["fee_type"].ToString();
                    if (!string.IsNullOrEmpty(wx_fee_type))
                    {
                        signstr += "&fee_type=" + wx_fee_type;
                    }
                }

                //wx_is_subscribe  
                if (ds.Tables[0].Columns.Contains("is_subscribe"))
                {
                    wx_is_subscribe = ds.Tables[0].Rows[0]["is_subscribe"].ToString();
                    if (!string.IsNullOrEmpty(wx_is_subscribe))
                    {
                        signstr += "&is_subscribe=" + wx_is_subscribe;
                    }
                }

                //wx_mch_id  
                if (ds.Tables[0].Columns.Contains("mch_id"))
                {
                    wx_mch_id = ds.Tables[0].Rows[0]["mch_id"].ToString();
                    if (!string.IsNullOrEmpty(wx_mch_id))
                    {
                        signstr += "&mch_id=" + wx_mch_id;
                    }
                }

                //wx_nonce_str  
                if (ds.Tables[0].Columns.Contains("nonce_str"))
                {
                    wx_nonce_str = ds.Tables[0].Rows[0]["nonce_str"].ToString();
                    if (!string.IsNullOrEmpty(wx_nonce_str))
                    {
                        signstr += "&nonce_str=" + wx_nonce_str;
                    }
                }

                //wx_openid  
                if (ds.Tables[0].Columns.Contains("openid"))
                {
                    wx_openid = ds.Tables[0].Rows[0]["openid"].ToString();
                    if (!string.IsNullOrEmpty(wx_openid))
                    {
                        signstr += "&openid=" + wx_openid;
                    }
                }

                //wx_out_trade_no  
                if (ds.Tables[0].Columns.Contains("out_trade_no"))
                {
                    wx_out_trade_no = ds.Tables[0].Rows[0]["out_trade_no"].ToString();
                    if (!string.IsNullOrEmpty(wx_out_trade_no))
                    {
                        signstr += "&out_trade_no=" + wx_out_trade_no;
                    }

                }

                //wx_result_code   
                if (ds.Tables[0].Columns.Contains("result_code"))
                {
                    wx_result_code = ds.Tables[0].Rows[0]["result_code"].ToString();
                    if (!string.IsNullOrEmpty(wx_result_code))
                    {
                        signstr += "&result_code=" + wx_result_code;
                    }
                }

                //wx_result_code   
                if (ds.Tables[0].Columns.Contains("return_code"))
                {
                    wx_return_code = ds.Tables[0].Rows[0]["return_code"].ToString();
                    if (!string.IsNullOrEmpty(wx_return_code))
                    {
                        signstr += "&return_code=" + wx_return_code;
                    }
                    LogHelper.WriteLog(typeof(NativeNotifyPage), "wx_return_code" + wx_return_code);
                }

                //wx_sign   
                if (ds.Tables[0].Columns.Contains("sign"))
                {
                    wx_sign = ds.Tables[0].Rows[0]["sign"].ToString();
                    //if (!string.IsNullOrEmpty(wx_sign))  
                    //{  
                    //    signstr += "&sign=" + wx_sign;  
                    //}  
                }

                //wx_time_end  
                if (ds.Tables[0].Columns.Contains("time_end"))
                {
                    wx_time_end = ds.Tables[0].Rows[0]["time_end"].ToString();
                    if (!string.IsNullOrEmpty(wx_time_end))
                    {
                        signstr += "&time_end=" + wx_time_end;
                    }
                    LogHelper.WriteLog(typeof(NativeNotifyPage), "time_end" + wx_time_end);
                }

                //wx_total_fee  
                if (ds.Tables[0].Columns.Contains("total_fee"))
                {
                    wx_total_fee = Convert.ToInt32(ds.Tables[0].Rows[0]["total_fee"].ToString());

                    signstr += "&total_fee=" + wx_total_fee;

                    LogHelper.WriteLog(typeof(NativeNotifyPage), "wx_total_fee" + wx_total_fee);
                }

                //wx_trade_type  
                if (ds.Tables[0].Columns.Contains("trade_type"))
                {
                    wx_trade_type = ds.Tables[0].Rows[0]["trade_type"].ToString();
                    if (!string.IsNullOrEmpty(wx_trade_type))
                    {
                        signstr += "&trade_type=" + wx_trade_type;
                    }
                }

                //wx_transaction_id  
                if (ds.Tables[0].Columns.Contains("transaction_id"))
                {
                    wx_transaction_id = ds.Tables[0].Rows[0]["transaction_id"].ToString();
                    if (!string.IsNullOrEmpty(wx_transaction_id))
                    {
                        signstr += "&transaction_id=" + wx_transaction_id;
                    }
                    LogHelper.WriteLog(typeof(NativeNotifyPage), "wx_transaction_id" + wx_transaction_id);
                }

                #endregion

                //追加key 密鑰  
                signstr += "&key=" + System.Web.Configuration.WebConfigurationManager.AppSettings["key"].ToString();
                //簽名正确  
                string orderStrwhere = "ordernumber='" + wx_out_trade_no + "'";



                if (wx_sign == System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(signstr, "MD5").ToUpper())
                {
                    //簽名正确   處理訂單操作邏輯  


                }
                else
                {
                    //追加備注信息  

                }

            }
            else
            {
                // 返回信息,如非空,爲(wéi / wèi)錯誤原因  簽名失敗 參數格式校驗錯誤  
                string return_msg = ds.Tables[0].Rows[0]["return_msg"].ToString();

            }


            return_result = sb.ToString();
        }


    }






    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //獲得Post過來(lái)的(de)數據  
    public string getPostStr()
    {
        Int32 intLen = Convert.ToInt32(System.Web.HttpContext.Current.Request.InputStream.Length);
        byte[] b = new byte[intLen];
        System.Web.HttpContext.Current.Request.InputStream.Read(b, 0, intLen);
        return System.Text.Encoding.UTF8.GetString(b);
    }



}

備注:記得将方法地(dì / de)址加入到(dào)統一下單的(de)中去如圖

相關案例查看更多