微信红包开发

以下是C#开发微信红包的源码

 public ActionResult CashRedPack()
        {

            //post请求
            string reqUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";

            string nonce_str = WeChatMPToken.createNonceStr();//随机字符串
            string sign = ""; // 
            string mch_billno = "";   //商户订单号
            string mch_id = "555555";  //商户号
            string wxappid = "yyyyyy"; //公众号appid 
            string nick_name = "oo";  //提供方名称
            string send_name = "oo";  //商户名称
            string re_openid = "oAeN5js2Jlm7bK851l8sTLSfur64";  //用户openid 
            int total_amount = 100;    //付款金额
            int min_value = 100;    //红包最小的金额
            int max_value = 100;   //红包最大的金额
            int total_num = 1;  //红包发放的人数
            string wishing = "送您的现金红包"; //红包祝福语
            string client_ip = "192.168.0.1";   //ip地址
            string act_name = "晒单返现";  //活动名称
            string remark = "晒单返现第二波";  // 活动备注
            string logo_imgurl = "https://mmbiz.qlogo.cn";// 商户logo的url   选填参数

            DateTime now = DateTime.Now;

            Random don = new Random();
            string str = string.Empty;


            for (int i = 0; i < 10; i++)
            {
                str += don.Next(0, 10);
            }


            mch_billno = mch_id + now.Year + now.Month + now.Day + str;
            //计算sign

            string stringSignTemp = string.Empty;

            stringSignTemp += "act_name=" + act_name;
            stringSignTemp += "&client_ip=" + client_ip;
            if (!string.IsNullOrEmpty(logo_imgurl))
            {
                stringSignTemp += "&logo_imgurl=" + logo_imgurl;

            }


            stringSignTemp += "&max_value=" + max_value;
            stringSignTemp += "&mch_billno=" + mch_billno;
            stringSignTemp += "&mch_id=" + mch_id;
            stringSignTemp += "&min_value=" + min_value;
            stringSignTemp += "&nick_name=" + nick_name;
            stringSignTemp += "&nonce_str=" + nonce_str;
            stringSignTemp += "&re_openid=" + re_openid;
            stringSignTemp += "&remark=" + remark;
            stringSignTemp += "&send_name=" + send_name;
            stringSignTemp += "&total_amount=" + total_amount;
            stringSignTemp += "&total_num=" + total_num;
            stringSignTemp += "&wishing=" + wishing;
            stringSignTemp += "&wxappid=" + wxappid;
            stringSignTemp += "&key=" + "199Mimo3848zhrmdgje33djgifekdo9";   //这个是你的api密钥


          
          


            //计算MD5值
            sign = MD5Security.GetMD5(stringSignTemp).ToUpper();


            string fuckyou = string.Empty;
            fuckyou = "<xml>";
            fuckyou += "<act_name><![CDATA[" + act_name + "]]></act_name>";
            fuckyou += "<client_ip><![CDATA[" + client_ip + "]]></client_ip>";
            fuckyou += "<logo_imgurl><![CDATA[" + logo_imgurl + "]]></logo_imgurl>";
            fuckyou += "<max_value><![CDATA[" + max_value + "]]></max_value>";
            fuckyou += "<mch_billno><![CDATA[" + mch_billno + "]]></mch_billno>";
            fuckyou += "<mch_id><![CDATA[" + mch_id + "]]></mch_id>";
            fuckyou += "<min_value><![CDATA[" + min_value + "]]></min_value>";
            fuckyou += "<nick_name><![CDATA[" + nick_name + "]]></nick_name>";
            fuckyou += "<nonce_str><![CDATA[" + nonce_str + "]]></nonce_str>";
            fuckyou += "<re_openid><![CDATA[" + re_openid + "]]></re_openid>";
            fuckyou += "<remark><![CDATA[" + remark + "]]></remark>";
            fuckyou += "<send_name><![CDATA[" + send_name + "]]></send_name>";
            fuckyou += "<total_amount><![CDATA[" + total_amount + "]]></total_amount>";
            fuckyou += "<total_num><![CDATA[" + total_num + "]]></total_num>";
            fuckyou += "<wishing><![CDATA[" + wishing + "]]></wishing>";
            fuckyou += "<wxappid><![CDATA[" + wxappid + "]]></wxappid>";
            fuckyou += "<sign><![CDATA[" + sign + "]]></sign>";
            fuckyou += "</xml>";
            //System.Xml.Linq.XDocument xml = System.Xml.Linq.XDocument.Parse(fuckyou);



            //Stream xmlStream = new MemoryStream();
            //xml.Save(xmlStream);
            //byte[] bytes = new byte[xmlStream.Length];
            //xmlStream.Read(bytes, 0, bytes.Length);
            //// 设置当前流的位置为流的开始
            //xmlStream.Seek(0, SeekOrigin.Begin);

            Encoding encoding = Encoding.UTF8;
            byte[] bytes = encoding.GetBytes(fuckyou);


            //发送post 请求  附带折证书



            try
            {
                //string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
                string cert = @"C:certapiclient_cert.p12";
                string password = "商户号";

                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                X509Certificate cer = new X509Certificate(cert, password,X509KeyStorageFlags.MachineKeySet);
                HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(reqUrl);
                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))
                {
                    resp = reader.ReadToEnd();
                }
            }
            catch (Exception exp)
            {
                
            }

            //解析xml
            return Json(new { isSuccess = 1 },JsonRequestBehavior.AllowGet);


        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            if (errors == SslPolicyErrors.None)
                return true;
            return false;
        }

  

这里面可能有几个需要注意的地方 当初我开发的时候也是遇到了
1.0
int total_amount = 100; //付款金额 这里的金额 是一分 而不是元
int min_value = 100; //红包最小的金额
int max_value = 100; //红包最大的金额
红包的最大值和最小值要是相同的


2.0
string cert = @"C:certapiclient_cert.p12"; 证书所存放的地址不能是桌面 估计是没有权限 如果是桌面会提示找不到证书(即使安装过)

3.0
切记 安装证书的时候选择本地计算机 而不是当前用户 否则加载不了
使用代码创建证书的时候 要选择从本地计算机加载

原文地址:https://www.cnblogs.com/soaeon/p/4869796.html