Beispiel #1
0
        /// <summary>
        ///  微信发送红包接口
        /// </summary>
        /// <param name="info">string billno, string openid, double totalamout, string appId, string mchid, string apiKey, string cert</param>
        /// <param name="result">输入发送结果</param>
        /// <returns></returns>
        public static Result SendRedPack(RedPackInfo info, out string result)
        {
            Result rlt     = new Result();
            string xmldata = RedPackXml(info);

            result = "";
            string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";

            try
            {
                Encoding encode = Encoding.Default;
                encode = System.Text.Encoding.GetEncoding("utf-8");
                byte[]         byteArray = encode.GetBytes(xmldata); //转化
                HttpWebRequest webReq    = (HttpWebRequest)WebRequest.Create(new Uri(url));
                //添加证书
                //@"E:\C#Worker\wolvesbrand\apiclient_cert.p12";
                string password = info.mchid;
                //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                //X509Certificate cer = new X509Certificate(cert, password);
                X509Certificate2 cer = new System.Security.Cryptography.X509Certificates.X509Certificate2(info.cert, password, X509KeyStorageFlags.MachineKeySet);

                CookieContainer cookieContainer = new CookieContainer();
                webReq.CookieContainer   = cookieContainer;
                webReq.AllowAutoRedirect = true;
                webReq.Method            = "POST";
                webReq.ContentType       = "text/xml";
                webReq.ContentLength     = byteArray.Length;
                webReq.ClientCertificates.Add(cer);

                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader    sr       = new StreamReader(response.GetResponseStream(), encode);
                string          ret      = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
                result = ret;
            }
            catch (Exception ex)
            {
                result = ex.Message;
                Log.Log.Error(ex.Message, ex);
                rlt.Add(ex.Message);
            }
            return(rlt);
        }
Beispiel #2
0
        /// <summary>
        /// 返回发送微信红包需要的xml数据包
        /// </summary>
        /// <param name="mchbillno">系统订单号10位</param>
        /// <param name="mchid">商户号</param>
        /// <param name="apikey">商户密钥</param>
        /// <param name="wxappid">微信公众平台appid</param>
        /// <param name="reopenid">接受人的OpenId</param>
        /// <param name="totalamount">发送的金额数(单位:元)</param>
        /// <returns></returns>
        private static string RedPackXml(RedPackInfo info)
        {
            //获得所需参数
            //string actname = "微信摇一摇";
            //string nickname = "上海狼群";
            //string sendname = "上海狼群";
            //string wishing = "欢迎您参加活动,祝你天天开心";
            //string clientip = "127.0.0.1";
            //string remark = "越摇越开心";
            //string noncestr = Rand.Str(32);
            Int64 totalamounttemp = (Int64)(info.totalamout * 100);

            //生成Sign签名
            //对参数按照key=value的格式,并按照参数名ASCII字典序排序如下
            String[] signtemp =
            {
                "mch_billno=" + info.mchbillno,
                "mch_id=" + info.mchid,
                "wxappid=" + info.appId,
                "nick_name=" + info.nickname,
                "send_name=" + info.sendname,
                "re_openid=" + info.openid,
                "total_amount=" + totalamounttemp,
                "min_value=" + totalamounttemp,
                "max_value=" + totalamounttemp,
                "total_num=1",
                "wishing=" + info.wishing,
                "client_ip=" + info.clientip,
                "act_name=" + info.actname,
                "remark=" + info.remark,
                "nonce_str=" + info.noncestr,
                "scene_id=" + info.scene_id
            };
            Array.Sort(signtemp);
            string signTempStr = String.Join("&", signtemp);
            //拼接API密钥:
            string stringSignTemp = signTempStr + "&key=" + info.apiKey;
            string sign           = EncryptorHelper.Md5Encryptor32(stringSignTemp).ToUpper();

            //生成xml包
            StringBuilder sb = new StringBuilder(0);

            sb.Append("<xml>");
            sb.AppendFormat("<sign>{0}</sign>", sign);                            //签名
            sb.AppendFormat("<mch_billno>{0}</mch_billno>", info.mchbillno);      //商户订单号(每个订单号必须唯一)组成: mch_id+yyyymmdd+10位一天内不能重复的数字。接口根据商户订单号支持重入, 如出现超时可再调用。
            sb.AppendFormat("<mch_id>{0}</mch_id>", info.mchid);                  //微信支付分配的商户号
            sb.AppendFormat("<wxappid>{0}</wxappid>", info.appId);                //商户appid
            sb.AppendFormat("<nick_name>{0}</nick_name>", info.nickname);         //提供方名称
            sb.AppendFormat("<send_name>{0}</send_name>", info.sendname);         //红包发送者名称
            sb.AppendFormat("<re_openid>{0}</re_openid>", info.openid);           //接受收红包的用户
            sb.AppendFormat("<total_amount>{0}</total_amount>", totalamounttemp); //付款金额,单位分
            sb.AppendFormat("<min_value>{0}</min_value>", totalamounttemp);       //最小红包金额,单位分
            sb.AppendFormat("<max_value>{0}</max_value>", totalamounttemp);       //最大红包金额,单位分( 最小金额等于最大金额: min_value=max_value =total_amount)
            sb.AppendFormat("<total_num>1</total_num>");                          //红包发放总人数total_num=1
            sb.AppendFormat("<wishing>{0}</wishing>", info.wishing);              //红包祝福语
            sb.AppendFormat("<client_ip>{0}</client_ip>", info.clientip);         //调用接口的机器Ip地址
            sb.AppendFormat("<act_name>{0}</act_name>", info.actname);            //活动名称
            // sb.AppendFormat("<act_id></act_id>");
            sb.AppendFormat("<remark>{0}</remark>", info.remark);                 //备注信息
            sb.AppendFormat("<scene_id>{0}</scene_id>", info.scene_id);
            //sb.AppendFormat("<logo_imgurl></logo_imgurl>");//商户logo的url
            //sb.AppendFormat("<share_content></share_content>");//分享文案
            //sb.AppendFormat("<share_url></share_url>");//分享链接
            //sb.AppendFormat("<share_imgurl></share_imgurl>");//分享的图片url
            sb.AppendFormat("<nonce_str>{0}</nonce_str>", info.noncestr);//随机字符串,不长于32位
            sb.AppendFormat("</xml>");
            return(sb.ToString());
        }