C#微信提现功能

1. 今天第一次了解微信提现功能,主要就是提供appid,appsercert,keys,商户号等

2. 先在微信公众号中开通【企业付款到用户零钱】,下载关于商户号的p12文件

.............

//引用都是必须的
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Xml;
using Hidistro.Entities.OutPay;
using Hidistro.ControlPanel.OutPay;

    public class OutPayHelp
	{
		// Token: 0x060003DC RID: 988 RVA: 0x00014B74 File Offset: 0x00012D74
		

		// Token: 0x060003E1 RID: 993 RVA: 0x000155D8 File Offset: 0x000137D8
		public static string BuildRequest(SortedDictionary<string, string> sParaTemp, string strMethod, string strButtonValue, string _key, string _input_charset)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary = OutPayHelp.BuildRequestPara(sParaTemp, _key, _input_charset);
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(string.Concat(new string[]
			{
				"<form id='alipaysubmit' name='alipaysubmit' action='",
				OutPayHelp.GATEWAY_NEW,
				"_input_charset=",
				_input_charset,
				"' method='",
				strMethod.ToLower().Trim(),
				"'>"
			}));
			foreach (KeyValuePair<string, string> current in dictionary)
			{
				stringBuilder.Append(string.Concat(new string[]
				{
					"<input type='hidden' name='",
					current.Key,
					"' value='",
					current.Value,
					"'/>"
				}));
			}
			stringBuilder.Append("<input type='submit' value='" + strButtonValue + "' style='display:none;'></form>");
			stringBuilder.Append("<script>document.forms['alipaysubmit'].submit();</script>");
			return stringBuilder.ToString();
		}

		// Token: 0x060003E3 RID: 995 RVA: 0x00015754 File Offset: 0x00013954
		private static string BuildRequestMysign(Dictionary<string, string> sPara, string _key, string _input_charset)
		{
			string prestr = OutPayHelp.CreateLinkString(sPara);
			return OutPayHelp.Sign(prestr, _key, _input_charset);
		}

		// Token: 0x060003E2 RID: 994 RVA: 0x0001570C File Offset: 0x0001390C
		private static Dictionary<string, string> BuildRequestPara(SortedDictionary<string, string> sParaTemp, string _key, string _input_charset)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary = OutPayHelp.FilterPara(sParaTemp);
			string value = OutPayHelp.BuildRequestMysign(dictionary, _key, _input_charset);
			dictionary.Add("sign", value);
			dictionary.Add("sign_type", "MD5");
			return dictionary;
		}

		// Token: 0x060003E5 RID: 997 RVA: 0x00015844 File Offset: 0x00013A44
		public static string CreateLinkString(Dictionary<string, string> dicArray)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (KeyValuePair<string, string> current in dicArray)
			{
				stringBuilder.Append(current.Key + "=" + current.Value + "&");
			}
			int length = stringBuilder.Length;
			stringBuilder.Remove(length - 1, 1);
			return stringBuilder.ToString();
		}

		// Token: 0x060003E4 RID: 996 RVA: 0x00015778 File Offset: 0x00013978
		public static Dictionary<string, string> FilterPara(SortedDictionary<string, string> dicArrayPre)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			foreach (KeyValuePair<string, string> current in dicArrayPre)
			{
				if (current.Key.ToLower() != "sign" && current.Key.ToLower() != "sign_type" && current.Value != "" && current.Value != null)
				{
					dictionary.Add(current.Key, current.Value);
				}
			}
			return dictionary;
		}

		// Token: 0x060003DA RID: 986 RVA: 0x00014A90 File Offset: 0x00012C90
		public static string GetMD5(string myString, string _input_charset = "utf-8")
		{
			MD5 mD = new MD5CryptoServiceProvider();
			byte[] bytes = Encoding.GetEncoding(_input_charset).GetBytes(myString);
			byte[] array = mD.ComputeHash(bytes);
			string text = null;
			for (int i = 0; i < array.Length; i++)
			{
				text += array[i].ToString("x").PadLeft(2, '0');
			}
			return text;
		}

		// Token: 0x060003DB RID: 987 RVA: 0x00014B00 File Offset: 0x00012D00
		public static string GetRandomString(int length)
		{
			StringBuilder stringBuilder = new StringBuilder();
			Random random = new Random();
			string value = DateTime.Now.ToString("yyyyMMdd");
			stringBuilder.Append(value);
			for (int i = 0; i < length; i++)
			{
				stringBuilder.Append(OutPayHelp.Chars[random.Next(0, OutPayHelp.Chars.Length)]);
			}
			return stringBuilder.ToString();
		}

		// Token: 0x060003DF RID: 991 RVA: 0x00015590 File Offset: 0x00013790
		public static string Sign(string prestr, string key, string _input_charset)
		{
			return OutPayHelp.GetMD5(prestr + key, "utf-8");
		}

		// Token: 0x060003DD RID: 989 RVA: 0x00014E54 File Offset: 0x00013054
		public static WeiPayResult1 SingleWeiPay(int amount, string desc, string useropenid, string realname, string tradeno, int UserId,string txtip, string WeixinPartnerID,string WeixinAppId,string WeixinPartnerKey,string BatchWeixinPayCheckRealName, string WeixinCertPath,string WeixinCertPassword)
		{
			OutPayHelp.WeiXinMchid = WeixinPartnerID;   //商户号
			OutPayHelp.WeiXinAppid = WeixinAppId;   //Appid
			OutPayHelp.WeiXinKey = WeixinPartnerKey; //商户平台密钥
			OutPayHelp.BatchWeixinPayCheckRealName = BatchWeixinPayCheckRealName.ToString();
			OutPayHelp.WeiXinCertPath = WeixinCertPath;  //P12文件路径
			OutPayHelp.WeixinCertPassword = WeixinCertPassword;
			string batchWeixinPayCheckRealName = OutPayHelp.BatchWeixinPayCheckRealName;
			if (batchWeixinPayCheckRealName != null)
			{
				if (!(batchWeixinPayCheckRealName == "0"))
				{
					if (!(batchWeixinPayCheckRealName == "1"))
					{
						if (batchWeixinPayCheckRealName == "2")
						{
							OutPayHelp.BatchWeixinPayCheckRealName = "OPTION_CHECK";
						}
					}
					else
					{
						OutPayHelp.BatchWeixinPayCheckRealName = "FORCE_CHECK";
					}
				}
				else
				{
					OutPayHelp.BatchWeixinPayCheckRealName = "NO_CHECK";
				}
			}
            WeiPayResult1 weiPayResult = new WeiPayResult1();
			weiPayResult.return_code = "SUCCESS";
			weiPayResult.err_code = "";
			weiPayResult.return_msg = "微信企业付款参数配置错误";
			if (OutPayHelp.WeiXinMchid == "")
			{
				weiPayResult.return_code = "FAIL";
				weiPayResult.return_msg = "商户号未配置!";
			}
			else if (OutPayHelp.WeiXinAppid == "")
			{
				weiPayResult.return_code = "FAIL";
				weiPayResult.return_msg = "公众号APPID未配置!";
			}
			else if (OutPayHelp.WeiXinKey == "")
			{
				weiPayResult.return_code = "FAIL";
				weiPayResult.return_msg = "商户密钥未配置!";
			}
            WeiPayResult1 result;
			if (weiPayResult.return_code == "FAIL")
			{
				result = weiPayResult;
			}
			else
			{
				weiPayResult.return_code = "FAIL";
				weiPayResult.return_msg = "用户参数出错了!";
				result = OutPayHelp.WeiXinPayOut(new OutPayWeiInfo
				{
					Amount = amount,
					Partner_Trade_No = tradeno,
					Openid = useropenid,
					Re_User_Name = realname,
					Desc = desc,
					UserId = UserId,
					device_info = "",
					Nonce_Str = OutPayHelp.GetRandomString(20)
				}, OutPayHelp.WeiXinAppid, OutPayHelp.WeiXinMchid, OutPayHelp.BatchWeixinPayCheckRealName, OutPayHelp.WeiXinKey, txtip);
			}
            return result;
		}

		// Token: 0x060003E0 RID: 992 RVA: 0x000155B4 File Offset: 0x000137B4
		public static bool Verify(string prestr, string sign, string key, string _input_charset)
		{
			string a = OutPayHelp.Sign(prestr, key, _input_charset);
			return a == sign;
		}

		// Token: 0x060003E6 RID: 998 RVA: 0x000158DC File Offset: 0x00013ADC
		public static bool VerifyNotify(SortedDictionary<string, string> inputPara, string notify_id, string sign, string _key, string _input_charset, string _partner)
		{
			Dictionary<string, string> dicArray = new Dictionary<string, string>();
			dicArray = OutPayHelp.FilterPara(inputPara);
			string prestr = OutPayHelp.CreateLinkString(dicArray);
			bool flag = OutPayHelp.Verify(prestr, sign, _key, _input_charset);
			string a = "true";
			if (notify_id != null && notify_id != "")
			{
				string text = "https://mapi.alipay.com/gateway.do?service=notify_verify&";
				a = new HttpHelp().DoGet(string.Concat(new string[]
				{
					text,
					"partner=",
					_partner,
					"&notify_id=",
					notify_id
				}), null);
			}
			return a == "true" && flag;
		}

		// Token: 0x060003DE RID: 990 RVA: 0x000150A8 File Offset: 0x000132A8
		public static WeiPayResult1 WeiXinPayOut(OutPayWeiInfo payinfos, string Mch_appid, string Mchid, string Check_Name, string _key,string txtip)
		{
			SortedDictionary<string, string> sortedDictionary = new SortedDictionary<string, string>();
			sortedDictionary.Add("mch_appid", Mch_appid);
			sortedDictionary.Add("mchid", Mchid);
			sortedDictionary.Add("nonce_str", payinfos.Nonce_Str);
			sortedDictionary.Add("partner_trade_no", payinfos.Partner_Trade_No);
			sortedDictionary.Add("openid", payinfos.Openid);
			sortedDictionary.Add("check_name", Check_Name);
			sortedDictionary.Add("amount", payinfos.Amount.ToString());
			sortedDictionary.Add("desc", payinfos.Desc);
			sortedDictionary.Add("spbill_create_ip", txtip);
			sortedDictionary.Add("re_user_name", payinfos.Re_User_Name);
			sortedDictionary.Add("device_info", "");
			string text = "";
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.AppendLine("<xml>");
			foreach (string current in sortedDictionary.Keys)
			{
				if (sortedDictionary[current] != "")
				{
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						"&",
						current,
						"=",
						sortedDictionary[current]
					});
					stringBuilder.AppendLine(string.Concat(new string[]
					{
						"<",
						current,
						">",
						sortedDictionary[current],
						"</",
						current,
						">"
					}));
				}
			}
			text = text.Remove(0, 1);
			text = text + "&key=" + _key;
			text = OutPayHelp.GetMD5(text, "utf-8");
			text = text.ToUpper();
			stringBuilder.AppendLine("<sign>" + text + "</sign>");
			stringBuilder.AppendLine("</xml>");
			HttpHelp httpHelp = new HttpHelp();
			string text3 = httpHelp.DoPost(OutPayHelp.WeiPayUrl, stringBuilder.ToString(), OutPayHelp.WeixinCertPassword, OutPayHelp.WeiXinCertPath);
            WeiPayResult1 weiPayResult = new WeiPayResult1();
			weiPayResult.return_code = "FAIL";
			weiPayResult.return_msg = "访问服务器出错了!";
			weiPayResult.err_code = "SERVERERR";
			weiPayResult.UserId = payinfos.UserId;
			weiPayResult.Amount = payinfos.Amount;
			weiPayResult.partner_trade_no = payinfos.Partner_Trade_No;
            WeiPayResult1 result;
			if (httpHelp.errstr != "")
			{
				weiPayResult.return_msg = httpHelp.errstr;
				result = weiPayResult;
			}
			else
			{
				try
				{
					XmlDocument xmlDocument = new XmlDocument();
					xmlDocument.LoadXml(text3);
					weiPayResult.return_code = xmlDocument.SelectSingleNode("/xml/return_code").InnerText;
					weiPayResult.return_msg = xmlDocument.SelectSingleNode("/xml/return_msg").InnerText;
					if (weiPayResult.return_code.ToUpper() == "SUCCESS")
					{
						weiPayResult.result_code = xmlDocument.SelectSingleNode("/xml/result_code").InnerText;
						if (weiPayResult.result_code.ToUpper() == "SUCCESS")
						{
							weiPayResult.mch_appid = xmlDocument.SelectSingleNode("/xml/mch_appid").InnerText;
							weiPayResult.mchid = xmlDocument.SelectSingleNode("/xml/mchid").InnerText;
							weiPayResult.device_info = xmlDocument.SelectSingleNode("/xml/device_info").InnerText;
							weiPayResult.nonce_str = xmlDocument.SelectSingleNode("/xml/nonce_str").InnerText;
							weiPayResult.result_code = xmlDocument.SelectSingleNode("/xml/result_code").InnerText;
							weiPayResult.partner_trade_no = xmlDocument.SelectSingleNode("/xml/partner_trade_no").InnerText;
							weiPayResult.payment_no = xmlDocument.SelectSingleNode("/xml/payment_no").InnerText;
							weiPayResult.payment_time = xmlDocument.SelectSingleNode("/xml/payment_time").InnerText;
						}
						else
						{
							weiPayResult.err_code = xmlDocument.SelectSingleNode("/xml/err_code").InnerText;
						}
					}
					else
					{
						weiPayResult.err_code = "FAIL";
					}
				}
				catch (Exception ex)
				{
					weiPayResult.return_code = "FAIL";
					weiPayResult.return_msg = ex.Message.ToString();
				}
				result = weiPayResult;
			}
			return result;
		}

		// Token: 0x0400003B RID: 59
		public static string BatchWeixinPayCheckRealName = "";

		// Token: 0x04000037 RID: 55
		public static char[] Chars = new char[]
		{
			'A',
			'B',
			'C',
			'D',
			'E',
			'F',
			'G',
			'H',
			'R',
			'J',
			'K',
			'L',
			'M',
			'N',
			'O',
			'P',
			'Q',
			'R',
			'S',
			'T',
			'U',
			'V',
			'W',
			'X',
			'Y',
			'Z',
			'a',
			'b',
			'c',
			'd',
			'e',
			'f',
			'g',
			'h',
			'i',
			'j',
			'k',
			'l',
			'm',
			'n',
			'o',
			'p',
			'q',
			'r',
			's',
			't',
			'u',
			'v',
			'w',
			'x',
			'y',
			'z'
		};

		// Token: 0x04000040 RID: 64
		private static string GATEWAY_NEW = "https://mapi.alipay.com/gateway.do?";

		// Token: 0x0400003E RID: 62
		private static bool IsReadSeting = false;

		// Token: 0x0400003F RID: 63
		private static string WeiPayUrl = string.Format("https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers", new object[0]);

		// Token: 0x04000038 RID: 56
		private static string WeiXinAppid = "";

		// Token: 0x0400003D RID: 61
		private static string WeixinCertPassword = "";

		// Token: 0x0400003C RID: 60
		private static string WeiXinCertPath = "";

		// Token: 0x0400003A RID: 58
		private static string WeiXinKey = "";

		// Token: 0x04000039 RID: 57
		private static string WeiXinMchid = "";
	}
namespace Hidistro.ControlPanel.OutPay
{
	// Token: 0x0200001C RID: 28
	public class HttpHelp
	{
		// Token: 0x060001D3 RID: 467 RVA: 0x000092F0 File Offset: 0x000074F0
		public string BuildGetUrl(string url, IDictionary<string, string> parameters)
		{
			string result;
			if (parameters != null && parameters.Count > 0)
			{
				if (url.Contains("?"))
				{
					url = url + "&" + HttpHelp.BuildQuery(parameters);
					result = url;
					return result;
				}
				url = url + "?" + HttpHelp.BuildQuery(parameters);
			}
			result = url;
			return result;
		}

		// Token: 0x060001D4 RID: 468 RVA: 0x00009358 File Offset: 0x00007558
		public static string BuildQuery(IDictionary<string, string> parameters)
		{
			StringBuilder stringBuilder = new StringBuilder();
			bool flag = false;
			IEnumerator<KeyValuePair<string, string>> enumerator = parameters.GetEnumerator();
			while (enumerator.MoveNext())
			{
				KeyValuePair<string, string> current = enumerator.Current;
				string key = current.Key;
				current = enumerator.Current;
				string value = current.Value;
				if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
				{
					if (flag)
					{
						stringBuilder.Append("&");
					}
					stringBuilder.Append(key);
					stringBuilder.Append("=");
                    stringBuilder.Append(Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(value)));//HttpUtility.UrlEncode(value, Encoding.UTF8)
                    //stringBuilder.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));//
                    flag = true;
				}
			}
			return stringBuilder.ToString();
		}

		// Token: 0x060001D5 RID: 469 RVA: 0x00009414 File Offset: 0x00007614
		public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
		{
			return true;
		}

		// Token: 0x060001D6 RID: 470 RVA: 0x00009428 File Offset: 0x00007628
		public string DoGet(string url, IDictionary<string, string> parameters)
		{
			if (parameters != null && parameters.Count > 0)
			{
				if (url.Contains("?"))
				{
					url = url + "&" + HttpHelp.BuildQuery(parameters);
				}
				else
				{
					url = url + "?" + HttpHelp.BuildQuery(parameters);
				}
			}
			HttpWebRequest webRequest = this.GetWebRequest(url, "GET", null, null);
			webRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
			HttpWebResponse rsp = (HttpWebResponse)webRequest.GetResponse();
			return this.GetResponseAsString(rsp, Encoding.UTF8);
		}

		// Token: 0x060001D7 RID: 471 RVA: 0x000094C4 File Offset: 0x000076C4
		public string DoPost(string url, IDictionary<string, string> parameters)
		{
			HttpWebRequest webRequest = this.GetWebRequest(url, "POST", null, null);
			webRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
			byte[] bytes = Encoding.UTF8.GetBytes(HttpHelp.BuildQuery(parameters));
			Stream requestStream = webRequest.GetRequestStream();
			requestStream.Write(bytes, 0, bytes.Length);
			requestStream.Close();
			HttpWebResponse rsp = (HttpWebResponse)webRequest.GetResponse();
			return this.GetResponseAsString(rsp, Encoding.UTF8);
		}

		// Token: 0x060001D8 RID: 472 RVA: 0x00009538 File Offset: 0x00007738
		public string DoPost(string url, string value, string cerPassword = null, string cerPath = null)
		{
			HttpWebRequest webRequest = this.GetWebRequest(url, "POST", cerPassword, cerPath);
			webRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
			byte[] bytes = Encoding.UTF8.GetBytes(value);
			Stream requestStream = webRequest.GetRequestStream();
			requestStream.Write(bytes, 0, bytes.Length);
			requestStream.Close();
			HttpWebResponse rsp = (HttpWebResponse)webRequest.GetResponse();
			return this.GetResponseAsString(rsp, Encoding.UTF8);
		}

		// Token: 0x060001D9 RID: 473 RVA: 0x000095A8 File Offset: 0x000077A8
		public string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
		{
			Stream stream = null;
			StreamReader streamReader = null;
			string result;
			try
			{
				stream = rsp.GetResponseStream();
				streamReader = new StreamReader(stream, encoding);
				result = streamReader.ReadToEnd();
			}
			finally
			{
				if (streamReader != null)
				{
					streamReader.Close();
				}
				if (stream != null)
				{
					stream.Close();
				}
				if (rsp != null)
				{
					rsp.Close();
				}
			}
			return result;
		}

		// Token: 0x060001DA RID: 474 RVA: 0x00009628 File Offset: 0x00007828
		public HttpWebRequest GetWebRequest(string url, string method, string cerPassword = null, string cerPath = null)
		{
			HttpWebRequest httpWebRequest = null;
			if (url.Contains("https"))
			{
				this.errstr = "";
				ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(this.CheckValidationResult);
				httpWebRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
				try
				{
					if (cerPassword != null)
					{
						X509Certificate2 value = new X509Certificate2(cerPath, cerPassword);
						httpWebRequest.ClientCertificates.Add(value);
					}
				}
				catch (Exception ex)
				{
					this.errstr = ex.Message.ToString().Trim();
				}
			}
			else
			{
				httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
			}
			httpWebRequest.ServicePoint.Expect100Continue = false;
			httpWebRequest.Method = method;
			httpWebRequest.KeepAlive = true;
			httpWebRequest.UserAgent = "Hishop";
			return httpWebRequest;
		}

		// Token: 0x04000017 RID: 23
		public string errstr = "";
	}
}

注:一定要配置好,p12文件路径需明确,微信商户平台要开通

没有更多推荐了,返回首页