码迷,mamicode.com
首页 > 微信 > 详细

微信发红包

时间:2015-11-26 18:51:26      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:

 

public class PayForWeiXinHelp
{
private readonly ILog _fileLog = LogManager.GetLogger(typeof(PromotionWindowsService));
/// <summary>
/// 调用微信支付接口前处理数据,包括sign验证等
/// </summary>
/// <param name="payForWeiXin"></param>
/// <returns></returns>
/// <remarks>Modify by leo 2015-11-05</remarks>
/// <remarks>删除NICK_NAME</remarks>
/// <remarks>Modify by lk 修复签名错误 2015-11-05</remarks>
public string DoDataForPayWeiXin(PayWeiXin payForWeiXin)
{
payForWeiXin.act_id = "act_id";
//调用接口的机器 Ip 地址

#region 处理nonce_str随机字符串,不长于 32 位(本程序生成长度为16位的)
string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
payForWeiXin.nonce_str = GenNoncestr(16);
#endregion
//原始传入参数
string[] signTemp = { "mch_billno=" + payForWeiXin.mch_billno, "mch_id=" + payForWeiXin.mch_id, "wxappid=" + payForWeiXin.AppId, "send_name=" + payForWeiXin.send_name, "re_openid=" + payForWeiXin.re_openid, "total_amount=" + payForWeiXin.total_amount, "total_num=" + payForWeiXin.total_num, "wishing=" + payForWeiXin.wishing, "client_ip=" + payForWeiXin.client_ip, "act_name=" + payForWeiXin.act_name, "act_id=" + payForWeiXin.act_id, "remark=" + payForWeiXin.remark, "nonce_str=" + payForWeiXin.nonce_str };

List<string> signList = signTemp.ToList();
#region 处理支付签名
//对signList按照ASCII码从小到大的顺序排序
signList.Sort();

string signOld = string.Empty;
string payForWeiXinOld = string.Empty;
int i = 0;
foreach (string temp in signList)
{
signOld += temp + "&";
i++;
}
signOld = signOld.Substring(0, signOld.Length - 1);
//拼接Key
signOld += "&key=" + payForWeiXin.PayKey;
//处理支付签名
payForWeiXin.sign = Encrypt(signOld).ToUpper();
#endregion
string postData = @"<xml>
<mch_billno>{0}</mch_billno>
<mch_id>{1}</mch_id>
<wxappid>{2}</wxappid>
<send_name>{3}</send_name>
<re_openid>{4}</re_openid>
<total_amount>{5}</total_amount>
<total_num>{6}</total_num>
<wishing>{7}</wishing>
<client_ip>{8}</client_ip>
<act_name>{9}</act_name>
<act_id>{10}</act_id>
<remark>{11}</remark>
<nonce_str>{12}</nonce_str>
<sign>{13}</sign></xml>";
postData = string.Format(postData,
payForWeiXin.mch_billno,//商户订单号
payForWeiXin.mch_id,//商户号
payForWeiXin.AppId, payForWeiXin.send_name,
payForWeiXin.re_openid,
payForWeiXin.total_amount, payForWeiXin.total_num,
payForWeiXin.wishing,
payForWeiXin.client_ip,
payForWeiXin.act_name,
payForWeiXin.act_id,
payForWeiXin.remark,
payForWeiXin.nonce_str,
payForWeiXin.sign
);

return postData;
}

/// <summary>
/// 调用微信支付接口
/// </summary>
/// <param name="payForWeiXin"></param>
/// <returns></returns>
public string PayForWeiXin(string postData)
{
string result = string.Empty;
try
{
result = PostPage("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack", postData);
}
catch (Exception ex)
{
throw ex;
}
return result;
}

 

 

public static string PostPage(string url, string xmldata)
{
//证书地址,并且双击apiclient_cert.p12安装证书
string cert = ConfigurationManager.AppSettings["certPath"].ToString(); ;
string password = ConfigurationManager.AppSettings["password"].ToString();
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

//****************注意*********************
//这里添加 证书要注意,在本地调试的时候
//本地调试用这个
// X509Certificate cer = new X509Certificate(cert, password);
//上传服务器上用这个
X509Certificate2 cer = new X509Certificate2(cert, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);
HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
webrequest.ClientCertificates.Add(cer);
webrequest.Method = "post";
webrequest.UseDefaultCredentials = true;
//添加xml数据
StreamWriter swMessages = new StreamWriter(webrequest.GetRequestStream());
//写入的流以XMl格式写入
swMessages.Write(xmldata);
//关闭写入流
swMessages.Close();

HttpWebResponse webreponse = (HttpWebResponse)webrequest.GetResponse();
Stream stream = webreponse.GetResponseStream();
string resp = string.Empty;
using (StreamReader reader = new StreamReader(stream))
{
resp = reader.ReadToEnd();
}
return resp;


}
/*CheckValidationResult的定义*/
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
if (errors == SslPolicyErrors.None)
return true;
return false;
}
//生成随机字符
public static string GenNoncestr(int length)
{
Random r = new Random();
string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string noncestr = "";
for (int i = 0; i < length; i++)
{
noncestr += str[r.Next(str.Length)];
}
return noncestr;
}

public string RandomStr(string str, int Length)
{
string result = string.Empty;
Random rd = new Random();
for (int i = 0; i < Length; i++)
{
result += str[rd.Next(str.Length)];
}
return result;
}

/// <summary>
/// Md5加密
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static String Encrypt(String s)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
bytes = md5.ComputeHash(bytes);
md5.Clear();
string ret = "";
for (int i = 0; i < bytes.Length; i++)
{
ret += Convert.ToString(bytes[i], 16).PadLeft(2, ‘0‘);
}
return ret.PadLeft(32, ‘0‘);
}
}

 

 

/// <summary>
/// 红包微信支付请求model
/// </summary>
public class PayWeiXin
{
public string PayKey { get; set; }
public string AppId { get; set; }
public string MchId { get; set; }
public string Secret { get; set; }
public string nonce_str { get; set; }
public string sign { get; set; }
public string mch_billno { get; set; }
public string mch_id { get; set; }
public string wxappid { get; set; }
public string nick_name { get; set; }
public string send_name { get; set; }
public string re_openid { get; set; }
public int total_amount { get; set; }
public int min_value { get; set; }
public int max_value { get; set; }
public int total_num { get; set; }
public string wishing { get; set; }
public string client_ip { get; set; }
public string act_id { get; set; }
public string act_name { get; set; }
public string remark { get; set; }
public string logo_imgurl { get; set; }
public string share_content { get; set; }
public string share_url { get; set; }
public string share_imgurl { get; set; }

}

微信发红包

标签:

原文地址:http://www.cnblogs.com/liaokui/p/4998416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!