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 += "&n="" auto;"=""> 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 ****you = string.Empty;
****you = "<xml>";
****you += "<act_name><![CDATA[" + act_name + "]]></act_name>";
****you += "<client_ip><![CDATA[" + client_ip + "]]></client_ip>";
****you += "<logo_imgurl><![CDATA[" + logo_imgurl + "]]></logo_imgurl>";
****you += "<max_value><![CDATA[" + max_value + "]]></max_value>";
****you += "<mch_billno><![CDATA[" + mch_billno + "]]></mch_billno>";
****you += "<mch_id><![CDATA[" + mch_id + "]]></mch_id>";
****you += "<min_value><![CDATA[" + min_value + "]]></min_value>";
****you += "<nick_name><![CDATA[" + nick_name + "]]></nick_name>";
****you += "<nonce_str><![CDATA[" + nonce_str + "]]></nonce_str>";
****you += "<re_openid><![CDATA[" + re_openid + "]]></re_openid>";
****you += "<remark><![CDATA[" + remark + "]]></remark>";
****you += "<send_name><![CDATA[" + send_name + "]]></send_name>";
****you += "<total_amount><![CDATA[" + total_amount + "]]></total_amount>";
****you += "<total_num><![CDATA[" + total_num + "]]></total_num>";
****you += "<wishing><![CDATA[" + wishing + "]]></wishing>";
****you += "<wxappid><![CDATA[" + wxappid + "]]></wxappid>";
****you += "<sign><![CDATA[" + sign + "]]></sign>";
****you += "</xml>";
//System.Xml.Linq.XDocument xml = System.Xml.Linq.XDocument.Parse(****you);
//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(****you);
//发送post 请求 附带折证书
try
{
//string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
string cert = @"C:\cert\apiclient_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;
}
[此贴子已经被作者于2018/1/24 15:07:41编辑过]