标签:
开源项目见:https://github.com/JeffreySu/WeiXinMPSDK
public ActionResult SendRedPack() { string mchbillno = DateTime.Now.ToString("HHmmss") + TenPayV3Util.BuildRandomStr(28); string nonceStr = TenPayV3Util.GetNoncestr();
RequestHandler packageReqHandler = new RequestHandler(null); packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("wxappid", AppId); //公众账号ID packageReqHandler.SetParameter("mch_id", MchId); //商户号 packageReqHandler.SetParameter("mch_billno", mchbillno); //填入商家订单号 packageReqHandler.SetParameter("nick_name", "提供方名称"); //提供方名称 packageReqHandler.SetParameter("send_name", "红包发送者名称"); //红包发送者名称 packageReqHandler.SetParameter("re_openid", "接受收红包的用户的openId"); //接受收红包的用户的openId packageReqHandler.SetParameter("total_amount", "100"); //付款金额,单位分 packageReqHandler.SetParameter("min_value", "100"); //最小红包金额,单位分 packageReqHandler.SetParameter("max_value", "100"); //最大红包金额,单位分 packageReqHandler.SetParameter("total_num", "1"); //红包发放总人数 packageReqHandler.SetParameter("wishing", "红包祝福语"); //红包祝福语 packageReqHandler.SetParameter("client_ip", Request.UserHostAddress); //调用接口的机器Ip地址 packageReqHandler.SetParameter("act_name", "活动名称"); //活动名称 packageReqHandler.SetParameter("remark", "备注信息"); //备注信息 string sign = packageReqHandler.CreateMd5Sign("key", Key); packageReqHandler.SetParameter("sign", sign); //签名
//发红包需要post的数据 string data = packageReqHandler.ParseXML(); //发红包接口地址 string url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
//本地或者服务器的证书位置(证书在微信支付申请成功发来的通知邮件中) string cert = @"F:\apiclient_cert.p12";
//私钥(在安装证书时设置) string password = MchId;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); //调用证书 X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet); 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(); return Content(responseContent); }
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
if (errors == SslPolicyErrors.None)
return true;
return false;
}
标签:
原文地址:http://www.cnblogs.com/rockchen/p/4484191.html