.net微信服务号发送红包

.net微信服务号发送红包,第1张

概述本文实例为大家分享了.net微信红包发送代码,供大家参考,具体内容如下注:需要开通微信支付的服务号!

本文实例为大家分享了.net微信红包发送代码,供大家参考,具体内容如下

注:需要开通微信支付的服务号!

//跳转微信登录页面public ActionResult Index(){ VIEwBag.url = "https://open.weixin.qq.com/connect/oauth2/authorize?appID=" + {服务号appID} + "&redirect_uri=http%3A%2F%2F" + {微信重定向域名(填写程序域名,例如:www.xxxx.com)} + "%2F"+{程序控制器名,例如:Home}+"%2F"+{程序Action名,例如:RedirectWeChat}+"&response_type=code&scope=snsAPI_userinfo&state=STATE#wechat_redirect"; return VIEw();}//获取accesstoken(访问微信接口需要)public static string accesstoken(string WeChatWxAppID,string WeChatWxAppSecret){ string strjson = httpRequestUtil.RequestUrl(string.Format("https://API.weixin.qq.com/cgi-bin/token?grant_type=clIEnt_credential&appID={0}&secret={1}",WeChatWxAppID,WeChatWxAppSecret)); if (strjson.IndexOf("errcode") == -1) {  return GetJsonValue(strjson,"access_token"); } else {  return ""; }}//解析Jsonpublic static string GetJsonValue(string JsonStr,string key){ string result = string.Empty; if (!string.IsNullOrEmpty(JsonStr)) {  key = "\"" + key.Trim('"') + "\"";  int index = JsonStr.IndexOf(key) + key.Length + 1;  if (index > key.Length + 1)  {   //先截逗号,若是最后一个,截“}”号,取最小值   int end = JsonStr.IndexOf(',',index);   if (end == -1)   {    end = JsonStr.IndexOf('}',index);   }   result = JsonStr.Substring(index,end - index);   result = result.Trim(new char[] { '"',' ','\'' }); //过滤引号或空格  } } return result;}//请求urlpublic static string RequestUrl(string url,string method="post"){ // 设置参数 httpWebRequest request = WebRequest.Create(url) as httpWebRequest; cookieContainer cookieContainer = new cookieContainer(); request.cookieContainer = cookieContainer; request.AllowautoRedirect = true; request.Method = method; request.ContentType = "text/HTML"; request.headers.Add("charset","utf-8"); //发送请求并获取相应回应数据 httpWebResponse response = request.GetResponse() as httpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream responseStream = response.GetResponseStream(); StreamReader sr = new StreamReader(responseStream,EnCoding.UTF8); //返回结果网页(HTML)代码 string content = sr.ReadToEnd(); return content;}//接收微信返回code//接收微信数据获取用户信息public ActionResult RedirectWeChat(string code,string state){ if (string.IsNullOrEmpty(code)) {  return Content("您拒绝了授权!"); } string access_token = accesstoken(微信AppID,微信AppSecret); string st = "https://API.weixin.qq.com/sns/oauth2/access_token?appID=" + 微信AppID + "&secret=" + 微信AppSecret + "&code=" + code + "&grant_type=authorization_code"; string data = RequestUrl(st);//拿到用户openIDstring openID=GetJsonValue(data,"openID");//获取用户其他信息 string url = "https://API.weixin.qq.com/cgi-bin/user/info?access_token=" + access_token + "&openID=" + openID + "&lang=zh_CN"; data = RequestUrl(url);string subscribe=GetJsonValue(data,"subscribe"); if (subscribe == "0") {  ///未关注  return RedirectToAction(""); } return RedirectToAction("");}//发送红包Actionpublic ActionResult HB(){ string openID = "";//用户openID string url = "https://API.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";  string orderNo = 商户号 + DateTime.Now.ToString("yyyymmdd")+"随机10位数字";//商户订单号 组成:mch_ID+yyyymmdd+10位一天内不能重复的数字。  string Code = ""//32为随机字符串; string key="key=" + "";//支付密钥(在商户平台设置32为字符串)  Dictionary<string,string> data = new Dictionary<string,string>(); data.Add("act_name","");//活动名称  data.Add("clIEnt_ip","192.168.1.1");//Ip地址  data.Add("mch_billno",orderNo);//商户订单号 组成:mch_ID+yyyymmdd+10位一天内不能重复的数字。  data.Add("mch_ID","");//商户号  data.Add("nonce_str",Code);//随机字符串  data.Add("re_openID",openID);//用户openID  data.Add("remark","");//备注  data.Add("send_name","");//商户名称  data.Add("total_amount","100");//付款金额 单位分  data.Add("total_num","1");//红包发放总人数  data.Add("wishing","恭喜发财");//红包祝福语  data.Add("wxappID",);//公众账号appID  string xml = GetXML(data,key);//签名+拼接xml  string str=PostWebRequests(url,xml);//微信返回xml err_code=SUCCESS 就是成功 return VIEw(""); }//发送红包(MD5签名+拼接XML)public static string GetXML(Dictionary<string,string> data,string paykey){ string retStr; MD5CryptoServiceProvIDer m5 = new MD5CryptoServiceProvIDer(); var data1=from d in data orderby d.Key select d; string data2 = ""; string XML = "<xml>"; foreach (var item in data1) {  //空值不参与签名  if (item.Value + "" != "")  {   data2 += item.Key +"="+ item.Value + "&";  }  XML += "<" + item.Key + ">" + item.Value+""+ "</" + item.Key + ">"; } data2 += paykey; //创建md5对象 byte[] inputBye; byte[] outputBye; //使用GB2312编码方式把字符串转化为字节数组. try {  inputBye = EnCoding.UTF8.GetBytes(data2); } catch {  inputBye = EnCoding.GetEnCoding("GB2312").GetBytes(data2); } outputBye = m5.ComputeHash(inputBye); retStr = System.BitConverter.ToString(outputBye); retStr = retStr.Replace("-","").toupper(); XML += "<sign>" + retStr + "</sign>";//签名 XML += "</xml>"; return XML;}//发送红包请求Post方法public static string PostWebRequests(string postUrl,string menuInfo){ string returnValue = string.Empty; try {  EnCoding enCoding = EnCoding.UTF8;  byte[] bytes = enCoding.GetBytes(menuInfo);  string cert = @"E:\cdcert\apiclient_cert.p12";//支付证书路径  string password = "1212121";//支付证书密码  ServicePointManager.ServerCertificateValIDationCallback = new RemoteCertificateValIDationCallback(CheckValIDationResult);  X509Certificate cer = new X509Certificate(cert,password,X509KeyStorageFlags.MachineKeySet);  httpWebRequest webrequest = (httpWebRequest)httpWebRequest.Create(postUrl);  webrequest.ClIEntCertificates.Add(cer);  webrequest.Method = "post";  webrequest.ContentLength = bytes.Length;  webrequest.GetRequestStream().Write(bytes,bytes.Length);  httpWebResponse webreponse = (httpWebResponse)webrequest.GetResponse();  Stream stream = webreponse.GetResponseStream();  string resp = string.Empty;  using (StreamReader reader = new StreamReader(stream))  {    return reader.ReadToEnd();  } } catch (Exception ex) {  return ""; }} 

以下是微信开发官方相关文档

1. 【微信支付】公众号支付开发者文档
2. 微信开放平台
3.企业号开发者接口文档
4.微信公众平台开发者文档

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的.net微信服务号发送红包全部内容,希望文章能够帮你解决.net微信服务号发送红包所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1257095.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存