微信小程序支付结果 c#后台回调

又为大家带来简单的c#后台支付结果回调方法,首先还是要去微信官网下载模板(WxPayAPI),将模板(WxPayAPI)添加到服务器上,然后在打开WxPayAPI项目中的example文件下的

NativeNotifyPage.aspx打开网页中的代码页如图:

将以下代码加入进去就能完成:

  1 public partial class NativeNotifyPage : System.Web.UI.Page
  2     {
  3         public static string wxJsApiParam { get; set; } //前段显示
  4         public string return_result = "";
  5         protected void Page_Load(object sender, EventArgs e)
  6         {
  7            Response.ContentType = "text/plain";
  8             Response.Write("Hello World");
  9 
 10             LogHelper.WriteLog(typeof(NativeNotifyPage), "可以运行1-1");
 11 
 12             String xmlData = getPostStr();//获取请求数据  
 13             if (xmlData == "")
 14             {
 15 
 16             }
 17             else
 18             {
 19                 var dic = new Dictionary<string, string>
 20                  {
 21                   {"return_code", "SUCCESS"},
 22                   {"return_msg","OK"}
 23 
 24                  };
 25                 var sb = new StringBuilder();
 26                 sb.Append("<xml>");
 27 
 28 
 29                 foreach (var d in dic)
 30                 {
 31                     sb.Append("<" + d.Key + ">" + d.Value + "</" + d.Key + ">");
 32                 }
 33                 sb.Append("</xml>");
 34 
 35 
 36 
 37 
 38 
 39                 //把数据重新返回给客户端  
 40                 DataSet ds = new DataSet();
 41                 StringReader stram = new StringReader(xmlData);
 42                 XmlTextReader datareader = new XmlTextReader(stram);
 43                 ds.ReadXml(datareader);
 44                 if (ds.Tables[0].Rows[0]["return_code"].ToString() == "SUCCESS")
 45                 {
 46 
 47                     LogHelper.WriteLog(typeof(NativeNotifyPage), "数据能返回");
 48 
 49 
 50                     string wx_appid = "";//微信开放平台审核通过的应用APPID  
 51                     string wx_mch_id = "";//微信支付分配的商户号  
 52 
 53                     string wx_nonce_str = "";//     随机字符串,不长于32位  
 54                     string wx_sign = "";//签名,详见签名算法  
 55                     string wx_result_code = "";//SUCCESS/FAIL  
 56 
 57                     string wx_return_code = "";
 58                     string wx_openid = "";//用户在商户appid下的唯一标识  
 59                     string wx_is_subscribe = "";//用户是否关注公众账号,Y-关注,N-未关注,仅在公众账号类型支付有效  
 60                     string wx_trade_type = "";//    APP  
 61                     string wx_bank_type = "";//     银行类型,采用字符串类型的银行标识,银行类型见银行列表  
 62                     string wx_fee_type = "";//  货币类型,符合ISO4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型  
 63 
 64 
 65                     string wx_transaction_id = "";//微信支付订单号  
 66                     string wx_out_trade_no = "";//商户系统的订单号,与请求一致。  
 67                     string wx_time_end = "";//  支付完成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。其他详见时间规则  
 68                     int wx_total_fee = 1;//    订单总金额,单位为分  
 69                     int wx_cash_fee = 1;//现金支付金额订单现金支付金额,详见支付金额  
 70 
 71 
 72                     #region  数据解析  
 73                     //列 是否存在  
 74                     string signstr = "";//需要前面的字符串  
 75                                         //wx_appid  
 76                     if (ds.Tables[0].Columns.Contains("appid"))
 77                     {
 78                         wx_appid = ds.Tables[0].Rows[0]["appid"].ToString();
 79                         if (!string.IsNullOrEmpty(wx_appid))
 80                         {
 81                             signstr += "appid=" + wx_appid;
 82                         }
 83                     }
 84 
 85                     //wx_bank_type  
 86                     if (ds.Tables[0].Columns.Contains("bank_type"))
 87                     {
 88                         wx_bank_type = ds.Tables[0].Rows[0]["bank_type"].ToString();
 89                         if (!string.IsNullOrEmpty(wx_bank_type))
 90                         {
 91                             signstr += "&bank_type=" + wx_bank_type;
 92                         }
 93                     }
 94                     //wx_cash_fee  
 95                     if (ds.Tables[0].Columns.Contains("cash_fee"))
 96                     {
 97                         wx_cash_fee = Convert.ToInt32(ds.Tables[0].Rows[0]["cash_fee"].ToString());
 98 
 99                         signstr += "&cash_fee=" + wx_cash_fee;
100                     }
101 
102                     //wx_fee_type  
103                     if (ds.Tables[0].Columns.Contains("fee_type"))
104                     {
105                         wx_fee_type = ds.Tables[0].Rows[0]["fee_type"].ToString();
106                         if (!string.IsNullOrEmpty(wx_fee_type))
107                         {
108                             signstr += "&fee_type=" + wx_fee_type;
109                         }
110                     }
111 
112                     //wx_is_subscribe  
113                     if (ds.Tables[0].Columns.Contains("is_subscribe"))
114                     {
115                         wx_is_subscribe = ds.Tables[0].Rows[0]["is_subscribe"].ToString();
116                         if (!string.IsNullOrEmpty(wx_is_subscribe))
117                         {
118                             signstr += "&is_subscribe=" + wx_is_subscribe;
119                         }
120                     }
121 
122                     //wx_mch_id  
123                     if (ds.Tables[0].Columns.Contains("mch_id"))
124                     {
125                         wx_mch_id = ds.Tables[0].Rows[0]["mch_id"].ToString();
126                         if (!string.IsNullOrEmpty(wx_mch_id))
127                         {
128                             signstr += "&mch_id=" + wx_mch_id;
129                         }
130                     }
131 
132                     //wx_nonce_str  
133                     if (ds.Tables[0].Columns.Contains("nonce_str"))
134                     {
135                         wx_nonce_str = ds.Tables[0].Rows[0]["nonce_str"].ToString();
136                         if (!string.IsNullOrEmpty(wx_nonce_str))
137                         {
138                             signstr += "&nonce_str=" + wx_nonce_str;
139                         }
140                     }
141 
142                     //wx_openid  
143                     if (ds.Tables[0].Columns.Contains("openid"))
144                     {
145                         wx_openid = ds.Tables[0].Rows[0]["openid"].ToString();
146                         if (!string.IsNullOrEmpty(wx_openid))
147                         {
148                             signstr += "&openid=" + wx_openid;
149                         }
150                     }
151 
152                     //wx_out_trade_no  
153                     if (ds.Tables[0].Columns.Contains("out_trade_no"))
154                     {
155                         wx_out_trade_no = ds.Tables[0].Rows[0]["out_trade_no"].ToString();
156                         if (!string.IsNullOrEmpty(wx_out_trade_no))
157                         {
158                             signstr += "&out_trade_no=" + wx_out_trade_no;
159                         }
160 
161                     }
162 
163                     //wx_result_code   
164                     if (ds.Tables[0].Columns.Contains("result_code"))
165                     {
166                         wx_result_code = ds.Tables[0].Rows[0]["result_code"].ToString();
167                         if (!string.IsNullOrEmpty(wx_result_code))
168                         {
169                             signstr += "&result_code=" + wx_result_code;
170                         }
171                     }
172 
173                     //wx_result_code   
174                     if (ds.Tables[0].Columns.Contains("return_code"))
175                     {
176                         wx_return_code = ds.Tables[0].Rows[0]["return_code"].ToString();
177                         if (!string.IsNullOrEmpty(wx_return_code))
178                         {
179                             signstr += "&return_code=" + wx_return_code;
180                         }
181                         LogHelper.WriteLog(typeof(NativeNotifyPage), "wx_return_code" + wx_return_code);
182                     }
183 
184                     //wx_sign   
185                     if (ds.Tables[0].Columns.Contains("sign"))
186                     {
187                         wx_sign = ds.Tables[0].Rows[0]["sign"].ToString();
188                         //if (!string.IsNullOrEmpty(wx_sign))  
189                         //{  
190                         //    signstr += "&sign=" + wx_sign;  
191                         //}  
192                     }
193 
194                     //wx_time_end  
195                     if (ds.Tables[0].Columns.Contains("time_end"))
196                     {
197                         wx_time_end = ds.Tables[0].Rows[0]["time_end"].ToString();
198                         if (!string.IsNullOrEmpty(wx_time_end))
199                         {
200                             signstr += "&time_end=" + wx_time_end;
201                         }
202                         LogHelper.WriteLog(typeof(NativeNotifyPage), "time_end" + wx_time_end);
203                     }
204 
205                     //wx_total_fee  
206                     if (ds.Tables[0].Columns.Contains("total_fee"))
207                     {
208                         wx_total_fee = Convert.ToInt32(ds.Tables[0].Rows[0]["total_fee"].ToString());
209 
210                         signstr += "&total_fee=" + wx_total_fee;
211 
212                         LogHelper.WriteLog(typeof(NativeNotifyPage), "wx_total_fee" + wx_total_fee);
213                     }
214 
215                     //wx_trade_type  
216                     if (ds.Tables[0].Columns.Contains("trade_type"))
217                     {
218                         wx_trade_type = ds.Tables[0].Rows[0]["trade_type"].ToString();
219                         if (!string.IsNullOrEmpty(wx_trade_type))
220                         {
221                             signstr += "&trade_type=" + wx_trade_type;
222                         }
223                     }
224 
225                     //wx_transaction_id  
226                     if (ds.Tables[0].Columns.Contains("transaction_id"))
227                     {
228                         wx_transaction_id = ds.Tables[0].Rows[0]["transaction_id"].ToString();
229                         if (!string.IsNullOrEmpty(wx_transaction_id))
230                         {
231                             signstr += "&transaction_id=" + wx_transaction_id;
232                         }
233                         LogHelper.WriteLog(typeof(NativeNotifyPage), "wx_transaction_id" + wx_transaction_id);
234                     }
235 
236                     #endregion
237 
238                     //追加key 密钥  
239                     signstr += "&key=" + System.Web.Configuration.WebConfigurationManager.AppSettings["key"].ToString();
240                     //签名正确  
241                     string orderStrwhere = "ordernumber='" + wx_out_trade_no + "'";
242 
243 
244 
245                     if (wx_sign == System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(signstr, "MD5").ToUpper())
246                     {
247                         //签名正确   处理订单操作逻辑  
248 
249 
250                     }
251                     else
252                     {
253                         //追加备注信息  
254 
255                     }
256 
257                 }
258                 else
259                 {
260                     // 返回信息,如非空,为错误原因  签名失败 参数格式校验错误  
261                     string return_msg = ds.Tables[0].Rows[0]["return_msg"].ToString();
262 
263                 }
264 
265 
266                 return_result = sb.ToString();
267             }
268 
269 
270         }
271 
272 
273 
274  
275 
276     
277         public bool IsReusable
278         {
279             get
280             {
281                 return false;
282             }
283         }
284 
285         //获得Post过来的数据  
286         public string getPostStr()
287         {
288             Int32 intLen = Convert.ToInt32(System.Web.HttpContext.Current.Request.InputStream.Length);
289             byte[] b = new byte[intLen];
290             System.Web.HttpContext.Current.Request.InputStream.Read(b, 0, intLen);
291             return System.Text.Encoding.UTF8.GetString(b);
292         }
293 
294     
295     
296     }

备注:记得将方法地址加入到统一下单的中去如图

转载于:https://www.cnblogs.com/softwyy/p/9084221.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值