public static string createPaySign(List <QueryParameter> parameters) { string query = QueryParameter.NormalizeRequestParameters(parameters); StringBuilder enValue = new StringBuilder(); enValue.Append(query); enValue.Append("&key=" + WeChatUtilities.getConfigValue(WeChatUtilities.PAYAPI_KEY)); string sign = CommonUtilities.encrypt_MD5(enValue.ToString()).ToUpper(); return(sign); }
public string WxRedPackCreate(string openid, int amount, string mch_billno, string act_name, string send_name, string remark, string wishing) { //Wx_WebOauthAccessToken webToken = utilities.getWebOauthAccessToken(code); string nonce_str = CommonUtilities.generateNonce(); //string mch_billno = "WXREDPACK" + CommonUtilities.generateTimeStamp() + random.Next(1000, 9999); string mch_id = WeUtil.getMchId(); string wxappid = WeUtil.getAppId(); string re_openid = openid; int total_amount = amount; int total_num = 1; string client_ip = WeChatUtilities.getConfigValue(WeChatUtilities.IP); List <QueryParameter> redpackParameter = new List <QueryParameter>(); redpackParameter.Add(new QueryParameter("nonce_str", nonce_str)); redpackParameter.Add(new QueryParameter("mch_billno", mch_billno)); redpackParameter.Add(new QueryParameter("mch_id", mch_id)); redpackParameter.Add(new QueryParameter("wxappid", wxappid)); redpackParameter.Add(new QueryParameter("send_name", send_name)); redpackParameter.Add(new QueryParameter("re_openid", re_openid)); redpackParameter.Add(new QueryParameter("total_amount", total_amount.ToString())); redpackParameter.Add(new QueryParameter("total_num", total_num.ToString())); redpackParameter.Add(new QueryParameter("wishing", wishing)); redpackParameter.Add(new QueryParameter("client_ip", client_ip)); redpackParameter.Add(new QueryParameter("act_name", act_name)); redpackParameter.Add(new QueryParameter("remark", remark)); string sign = WeChatUtilities.createPaySign(redpackParameter); string content = parseXml(redpackParameter, sign); string post_url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack"; var request = WebRequest.Create(post_url) as HttpWebRequest; string certpath = @"D:\apiclient_cert.p12"; string password = "******"; X509Certificate2 cert = new X509Certificate2(certpath, password); request.ClientCertificates.Add(cert); try { request.Method = "post"; StreamWriter streamWriter = new StreamWriter(request.GetRequestStream()); streamWriter.Write(content); streamWriter.Flush(); streamWriter.Close(); var response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); //return reader.ReadToEnd(); XmlSerializer xmldes = new XmlSerializer(typeof(WxHBSend_Result)); WxHBSend_Result info = xmldes.Deserialize(reader) as WxHBSend_Result; // 是否成功 string err_code_des = ""; if (info.return_code == "SUCCESS") { if (info.result_code == "SUCCESS") { WxRedPackOrder item = new WxRedPackOrder() { act_name = act_name, mch_billno = mch_billno, err_code_desc = err_code_des, status = "SENDING", send_time = DateTime.Now, re_openid = openid, send_name = send_name, total_amount = amount, total_num = 1, remark = remark, wishing = wishing }; PaymentDb.WxRedPackOrder.Add(item); PaymentDb.SaveChanges(); return("SUCCESS"); } else { err_code_des = info.err_code_des; } } else { err_code_des = info.return_msg; } WxRedPackOrder redpack = new WxRedPackOrder() { act_name = act_name, mch_billno = mch_billno, err_code_desc = err_code_des, status = "FAILED", re_openid = openid, send_name = send_name, total_amount = amount, total_num = 1, remark = remark, wishing = wishing }; PaymentDb.WxRedPackOrder.Add(redpack); PaymentDb.SaveChanges(); return("FAIL"); } catch (Exception ex) { return(ex.Message); } }