1、第一步:須要準備的東東php
<add key="sAppID" value="wxf2303b260*******"/> // 開發者應用ID ,如圖一web
<add key="sAppSecret" value="5b83018b0b69c4231c14a2a25*******"/> //開發者應用密鑰,如圖一算法
<add key="sPartnerKey" value="E07D38C4EC0C9B42BF808633*******"/> //商戶祕鑰 如圖二windows
<add key="sPartner" value="**********"/> // 商戶號 申請微信支付經過後,收到的郵件裏面有, 10位的商戶號api
(圖一),登陸地址:https://mp.weixin.qq.com/數組
(圖二,你本身設置的32位祕鑰),登陸地址:https://pay.weixin.qq.com/index.php/home/login?return_url=%2F服務器
2、第二步:安裝證書,windows下面選擇.p12格式的證書;取證書時必定得用 絕對路徑(如:D:\wwwroot\BusinessCard\weixin\businesscard\cert\apiclient_cert.p12)微信
證書pkcs12格式 (apiclient_cert.p12)app
3、第三步:賬戶裏面必須有錢,以下圖異步
4、第四步:源碼以下:
using System;
using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Security; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Web; namespace BusinessCard.Web.Code { public class Class1 { /// <summary> /// 企業付款給我的,直接入賬到微信錢包中 /// </summary> public static string TENPAY = "1"; public static string APPID = Vincent._WebConfig.GetAppSettingsString("sAppID"); //開發者應用ID public static string PARTNER = Vincent._WebConfig.GetAppSettingsString("sPartner"); //商戶號 public static string APPSECRET = Vincent._WebConfig.GetAppSettingsString("sAppSecret"); //開發者應用密鑰 public static string PARTNER_KEY = Vincent._WebConfig.GetAppSettingsString("sPartnerKey"); //商戶祕鑰 public const string URL = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; //服務器異步通知頁面路徑(流量卡) public static string WebUrl = Vincent._WebConfig.GetAppSettingsString("WebUrl"); public static readonly string NOTIFY_URL_Card_Store = "http://" + WebUrl + "/weixinpay/WXPayNotify_URL.aspx";// ConfigurationManager.AppSettings["WXPayNotify_URL_CardStore"].ToString(); public static readonly string NOTIFY_URL_Card_User = "http://" + WebUrl + "/weixinpay/WXPayNotify_URL.aspx"; //ConfigurationManager.AppSettings["WXPayNotify_URL_CardUser"].ToString(); public static readonly string NOTIFY_URL_HB_Store = "http://" + WebUrl + "/weixinpay/WXPayNotify_URL.aspx";// ConfigurationManager.AppSettings["WXPayNotify_URL_CardStore"].ToString(); //=======【代理服務器設置】=================================== /* 默認IP和端口號分別爲0.0.0.0和0,此時不開啓代理(若有須要才設置) */ public const string PROXY_URL = "http://10.152.18.220:8080"; //=======【證書路徑設置】===================================== /* 證書路徑,注意應該填寫絕對路徑(僅退款、撤銷訂單時須要) */ public const string SSLCERT_PATH = "weixin\\businesscard\\cert\\apiclient_cert.p12"; public static string SSLCERT_PASSWORD = PARTNER; /// <summary> /// 企業付款給我的 /// </summary> /// <returns></returns> public static string EnterprisePay(string Bill_No, string toOpenid, decimal Charge_Amt, string userName, string title) { //公衆帳號appid mch_appid 是 wx8888888888888888 String 微信分配的公衆帳號ID(企業號corpid即爲此appId) //商戶號 mchid 是 1900000109 String(32) 微信支付分配的商戶號 //設備號 device_info 否 013467007045764 String(32) 微信支付分配的終端設備號 //隨機字符串 nonce_str 是 5K8264ILTKCH16CQ2502SI8ZNMTM67VS String(32) 隨機字符串,不長於32位 //簽名 sign 是 C380BEC2BFD727A4B6845133519F3AD6 String(32) 簽名,詳見簽名算法 //商戶訂單號 partner_trade_no 是 10000098201411111234567890 String 商戶訂單號,需保持惟一性 //用戶openid openid 是 oxTWIuGaIt6gTKsQRLau2M0yL16E String 商戶appid下,某用戶的openid //校驗用戶姓名選項 check_name 是 OPTION_CHECK String NO_CHECK:不校驗真實姓名 //FORCE_CHECK:強校驗真實姓名(未實名認證的用戶會校驗失敗,沒法轉帳) //OPTION_CHECK:針對已實名認證的用戶才校驗真實姓名(未實名認證用戶不校驗,能夠轉帳成功) //收款用戶姓名 re_user_name 可選 馬花花 String 收款用戶真實姓名。 // 若是check_name設置爲FORCE_CHECK或OPTION_CHECK,則必填用戶真實姓名 //金額 amount 是 10099 int 企業付款金額,單位爲分 //企業付款描述信息 desc 是 理賠 String 企業付款操做說明信息。必填。 //Ip地址 spbill_create_ip 是 192.168.0.1 String(32) 調用接口的機器Ip地址 Bill_No = PARTNER + getTimestamp() + Bill_No; //訂單號組成 商戶號 + 隨機時間串 + 記錄ID //設置package訂單參數 SortedDictionary<string, string> dic = new SortedDictionary<string, string>(); string total_fee = (Charge_Amt * 100).ToString("f0"); string wx_nonceStr = Guid.NewGuid().ToString().Replace("-", ""); //Interface_WxPay.getNoncestr(); dic.Add("mch_appid", APPID); dic.Add("mchid", PARTNER);//財付通賬號商家 //dic.Add("device_info", "013467007045711");//可爲空 dic.Add("nonce_str", wx_nonceStr); dic.Add("partner_trade_no", Bill_No); dic.Add("openid", toOpenid); dic.Add("check_name", "NO_CHECK"); dic.Add("amount", total_fee); dic.Add("desc", title);//商品描述 dic.Add("spbill_create_ip", "211.149.234.224"); //用戶的公網ip,不是商戶服務器IP //生成簽名 string get_sign = BuildRequest(dic, PARTNER_KEY); Vincent._Log.SaveMessage("第一步 get_sign:" + get_sign); string _req_data = "<xml>"; _req_data += "<mch_appid>" + APPID + "</mch_appid>"; _req_data += "<mchid>" + PARTNER + "</mchid>"; _req_data += "<nonce_str>" + wx_nonceStr + "</nonce_str>"; _req_data += "<partner_trade_no>" + Bill_No + "</partner_trade_no>"; _req_data += "<openid>" + toOpenid + "</openid>"; _req_data += "<check_name>NO_CHECK</check_name>"; _req_data += "<amount>" + total_fee + "</amount>"; _req_data += "<desc>" + title + "</desc>"; _req_data += "<spbill_create_ip>211.149.234.224</spbill_create_ip>"; _req_data += "<sign>" + get_sign + "</sign>"; _req_data += "</xml>"; Vincent._Log.SaveMessage("企業付款生成的xml:" + _req_data.Trim()); var result = HttpPost(URL, _req_data.Trim(), true, 300); //var result = HttpPost(URL, _req_data, Encoding.UTF8); Vincent._Log.SaveMessage("返回結果:" + result); return result; //ReturnValue retValue = StreamReaderUtils.StreamReader(URL, Encoding.UTF8.GetBytes(_req_data), System.Text.Encoding.UTF8, true); //Vincent._Log.SaveMessage("返回結果:" + retValue.ErrorCode); //return retValue.ErrorCode; } public static string getTimestamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds).ToString(); } public static string BuildRequest(SortedDictionary<string, string> sParaTemp, string key) { //獲取過濾後的數組 Dictionary<string, string> dicPara = new Dictionary<string, string>(); dicPara = FilterPara(sParaTemp); //組合參數數組 string prestr = CreateLinkString(dicPara); //拼接支付密鑰 string stringSignTemp = prestr + "&key=" + key; Vincent._Log.SaveMessage("生成簽名的參數:" + stringSignTemp); //得到加密結果 string myMd5Str = GetMD5(stringSignTemp.Trim()); //返回轉換爲大寫的加密串 return myMd5Str.ToUpper(); } /// <summary> /// 除去數組中的空值和簽名參數並以字母a到z的順序排序 /// </summary> /// <param name="dicArrayPre">過濾前的參數組</param> /// <returns>過濾後的參數組</returns> public static Dictionary<string, string> FilterPara(SortedDictionary<string, string> dicArrayPre) { Dictionary<string, string> dicArray = new Dictionary<string, string>(); foreach (KeyValuePair<string, string> temp in dicArrayPre) { if (temp.Key != "sign" && !string.IsNullOrEmpty(temp.Value)) { dicArray.Add(temp.Key, temp.Value); } } return dicArray; } //組合參數數組 public static string CreateLinkString(Dictionary<string, string> dicArray) { StringBuilder prestr = new StringBuilder(); foreach (KeyValuePair<string, string> temp in dicArray) { prestr.Append(temp.Key + "=" + temp.Value + "&"); } int nLen = prestr.Length; prestr.Remove(nLen - 1, 1); return prestr.ToString(); } //加密 public static string GetMD5(string pwd) { MD5 md5Hasher = MD5.Create(); byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(pwd)); StringBuilder sBuilder = new StringBuilder(); for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } return sBuilder.ToString(); } public static string HttpPost(string postUrl, string paramData, Encoding dataEncode) { string ret = string.Empty; try { byte[] byteArray = dataEncode.GetBytes(paramData); //轉化 HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); webReq.Method = "POST"; webReq.ContentType = "application/x-www-form-urlencoded"; webReq.ContentLength = byteArray.Length; Stream newStream = webReq.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length);//寫入參數 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default); ret = sr.ReadToEnd(); sr.Close(); response.Close(); newStream.Close(); } catch (Exception ex) { Vincent._Log.SaveMessage("Post提交異常:" + ex.Message); } return ret; } public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { //直接確認,不然打不開 return true; } /// <summary> /// post提交支付 /// </summary> /// <param name="xml"></param> /// <param name="url"></param> /// <param name="isUseCert">是否使用證書</param> /// <param name="timeout"></param> /// <returns></returns> public static string HttpPost(string url, string xml, bool isUseCert, int timeout) { System.GC.Collect();//垃圾回收,回收沒有正常關閉的http鏈接 string result = "";//返回結果 HttpWebRequest request = null; HttpWebResponse response = null; Stream reqStream = null; try { //設置最大鏈接數 ServicePointManager.DefaultConnectionLimit = 200; //設置https驗證方式 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); } /*************************************************************** * 下面設置HttpWebRequest的相關屬性 * ************************************************************/ request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = timeout * 1000; //設置代理服務器 //WebProxy proxy = new WebProxy(); //定義一個網關對象 //proxy.Address = new Uri(PROXY_URL); //網關服務器端口:端口 //request.Proxy = proxy; //設置POST的數據類型和長度 request.ContentType = "text/xml"; byte[] data = System.Text.Encoding.UTF8.GetBytes(xml); request.ContentLength = data.Length; //是否使用證書 if (isUseCert) { string path = HttpContext.Current.Request.PhysicalApplicationPath; //X509Certificate2 cert = new X509Certificate2(path + SSLCERT_PATH, SSLCERT_PASSWORD); //將上面的改爲 X509Certificate2 cert = new X509Certificate2(path + SSLCERT_PATH, SSLCERT_PASSWORD, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);//線上發佈須要添加 request.ClientCertificates.Add(cert); Vincent._Log.SaveMessage("證書路徑:" + (path + SSLCERT_PATH)); //Vincent._Log.SaveMessage("WxPayApi:PostXml used cert"); } //往服務器寫入數據 reqStream = request.GetRequestStream(); reqStream.Write(data, 0, data.Length); reqStream.Close(); //獲取服務端返回 response = (HttpWebResponse)request.GetResponse(); //獲取服務端返回數據 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); result = sr.ReadToEnd().Trim(); sr.Close(); } catch (System.Threading.ThreadAbortException e) { Vincent._Log.SaveMessage("HttpService:Thread - caught ThreadAbortException - resetting."); Vincent._Log.SaveMessage("Exception message:" + e.Message); System.Threading.Thread.ResetAbort(); } catch (WebException e) { Vincent._Log.SaveMessage("HttpService" + e.ToString()); if (e.Status == WebExceptionStatus.ProtocolError) { Vincent._Log.SaveMessage("HttpService:StatusCode : " + ((HttpWebResponse)e.Response).StatusCode); Vincent._Log.SaveMessage("HttpService:StatusDescription : " + ((HttpWebResponse)e.Response).StatusDescription); } throw new Exception(e.ToString()); } catch (Exception e) { Vincent._Log.SaveMessage("HttpService" + e.ToString()); throw new Exception(e.ToString()); } finally { //關閉鏈接和流 if (response != null) { response.Close(); } if (request != null) { request.Abort(); } } return result; } /// <summary> /// 處理http GET請求,返回數據 /// </summary> /// <param name="url">請求的url地址</param> /// <returns>http GET成功後返回的數據,失敗拋WebException異常</returns> public static string Get(string url) { System.GC.Collect(); string result = ""; HttpWebRequest request = null; HttpWebResponse response = null; //請求url以獲取數據 try { //設置最大鏈接數 ServicePointManager.DefaultConnectionLimit = 200; //設置https驗證方式 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); } /*************************************************************** * 下面設置HttpWebRequest的相關屬性 * ************************************************************/ request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; //設置代理 WebProxy proxy = new WebProxy(); proxy.Address = new Uri(PROXY_URL); request.Proxy = proxy; //獲取服務器返回 response = (HttpWebResponse)request.GetResponse(); //獲取HTTP返回數據 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); result = sr.ReadToEnd().Trim(); sr.Close(); } catch (System.Threading.ThreadAbortException e) { Vincent._Log.SaveMessage("HttpService:Thread - caught ThreadAbortException - resetting."); Vincent._Log.SaveMessage("Exception message: " + e.Message); System.Threading.Thread.ResetAbort(); } catch (WebException e) { Vincent._Log.SaveMessage("HttpService" + e.ToString()); if (e.Status == WebExceptionStatus.ProtocolError) { Vincent._Log.SaveMessage("HttpService:StatusCode : " + ((HttpWebResponse)e.Response).StatusCode); Vincent._Log.SaveMessage("HttpService:StatusDescription : " + ((HttpWebResponse)e.Response).StatusDescription); } throw new Exception(e.ToString()); } catch (Exception e) { Vincent._Log.SaveMessage("HttpService" + e.ToString()); throw new Exception(e.ToString()); } finally { //關閉鏈接和流 if (response != null) { response.Close(); } if (request != null) { request.Abort(); } } return result; } } }