微信公众号 红包接口的功能实现

1.访问微信公众号进行证书的下载安装,安装时注意需要密钥,密钥是商户号;

2.商户的配置的处理以及需要充钱;

3.调用接口是,需要在配置的IP地址上面调用,否则会调用失败提醒;具体的代码:

  public static string SendRedPack(WxPayData inputObj, string re_openid, string total_amount, string client_ip)
        {
            string mchbillno = DateTime.Now.ToString("HHmmss") + TenPayUtil.BuildRandomStr(28);

            // RequestHandler packageReqHandler = new RequestHandler(null);
            //设置package订单参数
            inputObj.SetValue("nonce_str", Guid.NewGuid().ToString("N"));              //随机字符串
            inputObj.SetValue("wxappid", "****");          //公众账号ID
            inputObj.SetValue("mch_id", "****");       //商户号
            inputObj.SetValue("mch_billno", mchbillno);                 //填入商家订单号
            inputObj.SetValue("nick_name", "");                 //提供方名称
            inputObj.SetValue("send_name", "");                 //红包发送者名称
            inputObj.SetValue("re_openid", re_openid);                 //接受收红包的用户的openId
            inputObj.SetValue("total_amount", total_amount);                //付款金额,单位分
            inputObj.SetValue("min_value", total_amount);                //最小红包金额,单位分
            inputObj.SetValue("max_value", total_amount);                //最大红包金额,单位分
            inputObj.SetValue("total_num", "1");               //红包发放总人数
            inputObj.SetValue("wishing", "");               //红包祝福语
            inputObj.SetValue("client_ip", client_ip);               //调用接口的机器Ip地址
            inputObj.SetValue("act_name", "");   //活动名称
            inputObj.SetValue("remark", "备注信息");   //备注信息
            inputObj.SetValue("sign", inputObj.MakeSign());                       //签名
                                                                                                 //退款需要post的数据
            string data = inputObj.ToXml();
            //退款接口地址
            string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
            //本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中)
            string cert = @"C:\apiclient_cert.p12";
            //私钥(在安装证书时设置)
            string password = "";  //商户号
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            //调用证书
            X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);
            #region 发起post请求
            HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
            webrequest.ClientCertificates.Add(cer);
            webrequest.Method = "post";
            byte[] postdatabyte = Encoding.UTF8.GetBytes(data);
            webrequest.ContentLength = postdatabyte.Length;
            Stream stream;
            stream = webrequest.GetRequestStream();
            stream.Write(postdatabyte, 0, postdatabyte.Length);
            stream.Close();
            HttpWebResponse httpWebResponse = (HttpWebResponse)webrequest.GetResponse();
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
            string responseContent = streamReader.ReadToEnd();
            #endregion
            return responseContent;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喜欢猪猪

你的打赏是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值