. Net WeChat service number to send red packets,. net to send red packets

Source: Internet
Author: User

. Net service number to send red packets,. net to send red packets

The example in this article shares the. net red packet sending code for your reference. The specific content is as follows:

Note:The service number to be paid!

// Jump to the logon page public ActionResult Index () {ViewBag. url =" https://open.weixin.qq.com/connect/oauth2/authorize?appid= "+ {Service No. appid} +" & redirect_uri = http % 3A % 2F % 2F "+ {redirect domain name (enter the program domain name, for example, www.xxxx.com )} + "% 2F" + {Program Controller name, for example: Home} + "% 2F" + {program Action name, for example: redirectWeChat} + "& response_type = code & scope = snsapi_userinfo & state = STATE # wechat_redirect"; return View () ;}// obtain the accesstoken (required by the access interface) 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 "";}} // parse 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) {// cut the comma first. If it is the last one, cut the "}" and take the minimum value int end = jsonStr. indexOf (',', index); if (end =-1) {end = jsonStr. indexOf ('}', index);} result = jsonStr. substring (index, end-index); result = result. trim (new char [] {'"','', '\ ''}); // filter quotation marks or spaces} return result;} // request Urlpublic static string RequestUrl (string url, string method = "post") {// set the parameter 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"); // send a request and obtain the response data Http WebResponse response = request. getResponse () as HttpWebResponse; // wait until request. the GetResponse () program starts to send the Post request Stream responseStream = response to the target webpage. getResponseStream (); StreamReader sr = new StreamReader (responseStream, Encoding. UTF8); // returned result webpage (html) code string content = sr. readToEnd (); return content;} // receives the Returned code // receives the user information. public ActionResult RedirectWeChat (string code, string state) {if (string. isNullOr Empty (code) {return Content ("you have denied authorization! ");} 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 ); // obtain the user openidstring openid = GetJsonValue (data, "openid"); // obtain other user information 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 ("") ;}// send the red envelope Actionpublic ActionResult HB () {string openid = ""; // user openid string url =" https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack "; String orderNo = Merchant ID + DateTime. now. toString ("yyyymmdd") + "random 10 digits"; // The Merchant's Order Number: mch_id + yyyymmdd + 10 digits that cannot be repeated within one day. String Code = "" // 32 is a random string; string key = "key =" + ""; // payment key (set 32 as a string on the merchant's platform) Dictionary <string, string> data = new Dictionary <string, string> (); data. add ("act_name", ""); // activity name data. add ("client_ip", "192.168.1.1"); // ip address data. add ("mch_billno", orderNo); // Merchant Order Number: mch_id + yyyymmdd + 10 digits that cannot be repeated within one day. Data. add ("mch_id", ""); // merchant ID data. add ("nonce_str", Code); // random string data. add ("re_openid", openid); // user openid data. add ("remark", ""); // remarks data. add ("send_name", ""); // merchant name data. add ("total_amount", "100"); // payment amount unit: data. add ("total_num", "1"); // total number of red packets issued data. add ("wishing", ""); // red envelope greetings data. add ("wxappid",); // public account appid string xml = GetXML (data, key); // signature + splice xml string str = PostWebRequests (url, xml ); // returns x Ml err_code = SUCCESS is successful return View ("");} // send a red packet (MD5 Signature + splicing 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) {// null values do not participate in the signature if (item. value + ""! = "") {Data2 + = item. key + "=" + item. value + "&";} XML + = "<" + item. key + ">" + item. value + "" + "</" + item. key + ">" ;}data2 + = paykey; // create the md5 object byte [] inputBye; byte [] outputBye; // convert the string into a byte array using GB2312 encoding. 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>"; // signature XML + = "</xml>"; return XML ;} // send a red packet request Post method 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"; // payment certificate path string password = "1212121"; // payment certificate password 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, 0, 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 "";}}

The following are official development documents

1. [payment] developer documentation for Public Account Payment
2. Open Platform
3. Enterprise number developer interface document
4. Public platform developer documentation

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.