123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- using System;
- using System.Collections.Generic;
- using Library;
- using LitJson;
- using System.Linq;
- using System.Security.Cryptography.X509Certificates;
- using System.Security.Cryptography;
- using System.Text;
- using System.IO;
- using System.Net;
- using System.Web;
- using MySystem.Models;
- using Org.BouncyCastle.Crypto.Modes;
- using Org.BouncyCastle.Crypto.Parameters;
- using Org.BouncyCastle.Crypto.Engines;
- using MySystem.Models.Main;
- namespace MySystem
- {
- public class WeChatFunctionForHD
- {
- public readonly static WeChatFunctionForHD Instance = new WeChatFunctionForHD();
- private WeChatFunctionForHD()
- { }
- public string MchId = "178590308";
- public string ApiKey = "XTH7TLTSJH4SOZC3NVFQJ726B5VRNVO6";
- public string serialNo = "5293C873581457469F1BE1839EA127B08ADE2D7B";
- public string getSerialNo = "7C6E6AD5DA1F7EB4A1071F2333C2FED0D8CC3A46";
-
- string pubkey = "MIIEFDCCAvygAwIBAgIUfG5q1doffrShBx8jM8L+0NjMOkYwDQYJKoZIhvcNAQELBQAwXjELMAkGA1UEBhMCQ04xEzARBgNVB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Ub3EAnRRxSyPJreTbAN+LbLInf0uQqSUBAW32B6CX1oGHG5xUzy6zsYn3QkKTvxT6RNxzl8LDmZhk80+vE8bWlI9ZL+wKBgQDRIyFJFXfHPQx0/12bqkc8mLSNWGqO1OdxMoUYld7UHXgbNusM2w4f/3TsvZHSWEwAtP4BfeeuEfmXPMK8O8kvrEmbW/tsb3SZ5d1TTtmxb4LVFgMk05BY4G/XaLX4BwcqLPMkzJ1r3vHl86u3cqCLy0Pzb7TULgEaKKjwvPg2eQKBgQCxR7QOQ4IYF0NPoqN0M7P8TrdiHVBNuc+ic8aqmiSPmlenOlYtRIP+VIygWJzUAqkGRYZDfZN1f0TvwwpTPHTWN/vjFofkSubXGyaTaoGEiwe61in9WdHO7dEIcIr3EN3v3hbSZVtIU4MmCWHri4BeNqDkxZeOvLf8bVEY5Hn/vwKBgCq3bDMLSX3OgF7dUfdbnlVIM4izSICICAXc9caBxhO5OTEF1Ji5nXb3rKA2UDSQiGWUklUnrEynqz0y0eu0onE0VYyACyio0z4j70L21gwh5K01Y8r/03nzzaRH5PMWLGZ26+rWSEkiLnJzhN1hcl1bVTPB20ODagI3z9nsHx6xAoGBANoqZ97soR2+lCWxwaa4wrNb4DNLgI1ky91o3LpIFH/lX3grCpNBee9E6kW2XoSe7PUh8/WzQMxtyYEYvt9z+QPWcUjCjs9sexck/o+RTTVBM6JtOwKbUQzsQ0bUUSFAIw1ofGruQMHt/2v4ODFGCNI5FSy4ZruWGmN2d2t3uMP8";
- #region 间连商户开户意愿确认(提交申请单)
- public string MerchantApply(MerchantAddInfo info)
- {
- Dictionary<string, object> result = new Dictionary<string, object>();
- string business_code = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
- if (!string.IsNullOrEmpty(info.WeChatMerchantId))
- {
- result.Add("channel_id", info.WeChatMerchantId);
- }
- result.Add("business_code", business_code);
- WebCMSEntities dbpxc = new WebCMSEntities();
-
- Dictionary<string, object> contact_info = new Dictionary<string, object>();
- contact_info.Add("contact_type", "LEGAL");
- contact_info.Add("name", RSAEncrypt(info.CertLegalPerson));
- contact_info.Add("id_card_number", RSAEncrypt(info.IdCardNumber));
- contact_info.Add("contact_id_doc_type", "IDENTIFICATION_TYPE_IDCARD");
- contact_info.Add("mobile", RSAEncrypt(info.MobilePhone));
- result.Add("contact_info", contact_info);
-
- Dictionary<string, object> subject_info = new Dictionary<string, object>();
- string SubjectType = info.SubjectType;
- if (SubjectType == "SUBJECT_TYPE_SMALL") SubjectType = "SUBJECT_TYPE_MICRO";
- subject_info.Add("subject_type", SubjectType);
- Dictionary<string, object> business_licence_info = new Dictionary<string, object>();
- string CertMerchantName = info.CertMerchantName;
- if (info.SubjectType == "SUBJECT_TYPE_SMALL")
- {
- info.LicenseCopy = info.IdCardCopy;
- for (int i = 0; i < 9; i++)
- {
- CertMerchantName = CertMerchantName.Replace(i.ToString(), "");
- }
- }
- if (info.SubjectType != "SUBJECT_TYPE_SMALL")
- {
- business_licence_info.Add("licence_number", info.LicenseNumber);
- business_licence_info.Add("licence_copy", GetMediaId("/" + info.LicenseCopy));
- business_licence_info.Add("merchant_name", CertMerchantName);
- business_licence_info.Add("legal_person", info.CertLegalPerson);
- business_licence_info.Add("company_address", info.BizStoreAddress);
- string PeriodEnd = info.PeriodEnd.Value.ToString("yyyy-MM-dd");
-
- if (PeriodEnd.Substring(0, 4) == "9999")
- {
-
- PeriodEnd = "长期";
- }
- business_licence_info.Add("licence_valid_date", "[\"" + info.PeriodBegin.Value.ToString("yyyy-MM-dd") + "\",\"" + PeriodEnd + "\"]");
- subject_info.Add("business_licence_info", business_licence_info);
- }
- if (info.SubjectType == "SUBJECT_TYPE_SMALL")
- {
- Dictionary<string, object> assist_prove_info = new Dictionary<string, object>();
- string micro_biz_type = "MICRO_TYPE_STORE";
- var placeType = info.PlaceType;
- if (placeType == 1)
- {
- micro_biz_type = "MICRO_TYPE_MOBILE";
- }
- assist_prove_info.Add("micro_biz_type", micro_biz_type);
- assist_prove_info.Add("store_name", info.BizStoreName);
- assist_prove_info.Add("store_address_code", info.AreaNo);
- assist_prove_info.Add("store_address", info.BizStoreAddress);
- assist_prove_info.Add("store_header_copy", GetMediaId("/" + info.StoreEntrancePic));
- assist_prove_info.Add("store_indoor_copy", GetMediaId("/" + info.IndoorPic));
- subject_info.Add("assist_prove_info", assist_prove_info);
- }
- result.Add("subject_info", subject_info);
- Dictionary<string, object> identification_info = new Dictionary<string, object>();
- identification_info.Add("id_holder_type", "LEGAL");
- identification_info.Add("identification_type", "IDENTIFICATION_TYPE_IDCARD");
- identification_info.Add("identification_name", RSAEncrypt(info.CertLegalPerson));
- identification_info.Add("identification_number", RSAEncrypt(info.IdCardNumber));
- string CardPeriodEnd = info.CardPeriodEnd.Value.ToString("yyyy-MM-dd");
- if (CardPeriodEnd.Substring(0, 4) == "2050")
- {
- CardPeriodEnd = "forever";
-
- }
- identification_info.Add("identification_valid_date", "[\"" + info.CardPeriodBegin.Value.ToString("yyyy-MM-dd") + "\",\"" + CardPeriodEnd + "\"]");
- identification_info.Add("identification_front_copy", GetMediaId("/" + info.IdCardCopy));
- identification_info.Add("identification_back_copy", GetMediaId("/" + info.IdCardNational));
- bool owner = true;
- if (info.SubjectType == "SUBJECT_TYPE_ENTERPRISE")
- {
- identification_info.Add("identification_address", RSAEncrypt(info.IdCardAddress));
- identification_info.Add("owner", owner);
- }
- result.Add("identification_info", identification_info);
-
- if (info.SubjectType == "SUBJECT_TYPE_ENTERPRISE" && !owner)
- {
- List<Dictionary<string, object>> ubo_info_list = new List<Dictionary<string, object>>();
- Dictionary<string, object> ubo_info_list_item = new Dictionary<string, object>();
- ubo_info_list_item.Add("ubo_id_doc_type", "IDENTIFICATION_TYPE_IDCARD");
- ubo_info_list_item.Add("ubo_id_doc_copy", GetMediaId("/" + info.IdCardCopy));
- ubo_info_list_item.Add("ubo_id_doc_copy_back", GetMediaId("/" + info.IdCardNational));
- ubo_info_list_item.Add("ubo_id_doc_name", RSAEncrypt(info.CertLegalPerson));
- ubo_info_list_item.Add("ubo_id_doc_number", RSAEncrypt(info.IdCardNumber));
- ubo_info_list_item.Add("ubo_id_doc_address", RSAEncrypt(info.IdCardAddress));
- ubo_info_list_item.Add("ubo_period_begin", info.CardPeriodBegin.Value.ToString("yyyy-MM-dd"));
- if (CardPeriodEnd == "forever")
- {
- CardPeriodEnd = "长期";
- }
- ubo_info_list_item.Add("ubo_period_end", CardPeriodEnd);
- ubo_info_list.Add(ubo_info_list_item);
- result.Add("ubo_info_list", ubo_info_list);
- }
- dbpxc.Dispose();
- string req = Newtonsoft.Json.JsonConvert.SerializeObject(result);
-
-
-
-
-
- LogHelper.Instance.WriteLog(req, "间连商户开户意愿确认");
- string resp = postJson("https://api.mch.weixin.qq.com/v3/apply4subject/applyment", req);
- LogHelper.Instance.WriteLog(resp + "\n\n", "间连商户开户意愿确认");
- return resp;
- }
- #endregion
- #region 间连商户开户意愿确认(提交申请单)查询申请单状态
- public string QueryMerchant(string BusinessCode)
- {
- string result = "";
- try
- {
- result = postJson("https://api.mch.weixin.qq.com/v3/apply4subject/applyment?applyment_id=" + BusinessCode, "", "GET");
-
-
- LogHelper.Instance.WriteLog(BusinessCode, "间连商户开户意愿确认(提交申请单)查询申请单状态-请求参数");
- LogHelper.Instance.WriteLog(result, "间连商户开户意愿确认(提交申请单)查询申请单状态");
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "间连商户开户意愿确认(提交申请单)查询申请单状态异常");
- }
- return result;
- }
- #endregion
- #region 获取商户开户意愿确认状态
- public string QueryAuthMerchant(string MchId)
- {
- string result = "";
- try
- {
- result = postJson("https://api.mch.weixin.qq.com/v3/apply4subject/applyment/merchants/" + MchId + "/state", "", "GET");
-
- LogHelper.Instance.WriteLog(MchId, "获取商户开户意愿确认状态");
- LogHelper.Instance.WriteLog(result, "获取商户开户意愿确认状态");
- }
- catch (Exception ex)
- {
- LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "获取商户开户意愿确认状态异常");
- }
- return result;
- }
- #endregion
- #region 获取平台证书
- public string GetPlatCert()
- {
- string merchantId = MchId;
- string result = postJson("https://api.mch.weixin.qq.com/v3/certificates", "", "GET");
- return result;
- }
- #endregion
- #region 敏感信息加密
- public string RSAEncrypt(string text)
- {
- byte[] publicKey = Convert.FromBase64String(pubkey);
-
-
-
-
- using (var x509 = new X509Certificate2(publicKey))
- {
- using (var rsa = x509.GetRSAPublicKey())
- {
- var buff = rsa.Encrypt(Encoding.UTF8.GetBytes(text), RSAEncryptionPadding.OaepSHA1);
- return Convert.ToBase64String(buff);
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
- public string AesGcmDecrypt(string associatedData, string nonce, string ciphertext)
- {
-
-
-
-
-
-
-
-
-
-
-
-
- byte[] key = Encoding.UTF8.GetBytes(ApiKey);
- byte[] nonceByte = Encoding.UTF8.GetBytes(nonce);
- byte[] cipherByte = Convert.FromBase64String(ciphertext);
- byte[] associatedByte = Encoding.UTF8.GetBytes(associatedData);
- GcmBlockCipher cipher = new GcmBlockCipher(new AesEngine());
- AeadParameters parameters = new AeadParameters(new KeyParameter(key), 128, nonceByte, associatedByte);
- cipher.Init(false, parameters);
- byte[] plaintext = new byte[cipher.GetOutputSize(cipherByte.Length)];
- int len = cipher.ProcessBytes(cipherByte, 0, cipherByte.Length, plaintext, 0);
- cipher.DoFinal(plaintext, len);
- return Encoding.UTF8.GetString(plaintext);
- }
- public string postJson(string url, string postData, string method = "POST")
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.Method = method;
- request.ContentType = "application/json;charset=UTF-8";
- request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36";
- request.Accept = "application/json";
- string Authorization = GetAuthorization(url, method, postData, prikey, MchId, serialNo);
- request.Headers.Add("Authorization", Authorization);
- request.Headers.Add("Wechatpay-Serial", getSerialNo);
- if (!string.IsNullOrEmpty(postData))
- {
- byte[] paramJsonBytes;
- paramJsonBytes = System.Text.Encoding.UTF8.GetBytes(postData);
- request.ContentLength = paramJsonBytes.Length;
- Stream writer;
- try
- {
- writer = request.GetRequestStream();
- }
- catch (Exception ex)
- {
- writer = null;
- LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "http请求异常");
- }
- writer.Write(paramJsonBytes, 0, paramJsonBytes.Length);
- writer.Close();
- }
- HttpWebResponse response;
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- }
- catch (WebException ex)
- {
- response = ex.Response as HttpWebResponse;
- }
- Stream resStream = response.GetResponseStream();
- StreamReader reader = new StreamReader(resStream);
- string text = reader.ReadToEnd();
- return text;
-
-
- }
- #endregion
- #region 图片上传
- public string GetMediaId(string imgPath)
- {
- if (string.IsNullOrEmpty(imgPath))
- {
- return "";
- }
- if (imgPath.Contains(","))
- {
- imgPath = imgPath.Split(',')[0];
- }
- string key = "wechatpic:" + function.MD5_16(imgPath);
- string media_id = RedisDbconn.Instance.Get<string>(key);
- if (!string.IsNullOrEmpty(media_id))
- {
- return media_id;
- }
- string filePath = function.getPath(imgPath);
- var filename = Path.GetFileName(filePath);
-
-
-
-
- byte[] imgBytesIn = PublicFunction.GetNetFileData(AppConfig.Base.OssHost + imgPath);
- byte[] hash = SHA256Managed.Create().ComputeHash(imgBytesIn);
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < hash.Length; i++)
- {
- builder.Append(hash[i].ToString("x2"));
- }
- var sha256 = builder.ToString();
- string metaStr = "{\"filename\":\"" + filename + "\",\"sha256\":\"" + sha256 + "\"}";
- media_id = UploadImgApi(metaStr, imgBytesIn, filename);
- RedisDbconn.Instance.Set(key, media_id);
- return media_id;
- }
- public string UploadImgApi(string metaStr, Byte[] imgBytesIn, string filename)
- {
- string url = "https://api.mch.weixin.qq.com/v3/merchant/media/upload";
- string merchantId = MchId;
- string privateKey = prikey;
- #region 定义请求体中的内容 并转成二进制
- string boundary = "lc199aecd61b4653ef";
- string Enter = "\r\n";
- string campaignIDStr1
- = "--" + boundary
- + Enter
- + "Content-Disposition: form-data; name=\"meta\";"
- + Enter
- + "Content-Type:application/json;"
- + Enter
- + Enter
- + metaStr
- + Enter
- + "--" + boundary
- + Enter
- + "Content-Disposition:form-data;name=\"file\";filename=\"" + filename + "\";"
- + Enter
- + "Content-Type:image/jpeg"
- + Enter
- + Enter;
- byte[] byteData2
- = imgBytesIn;
- string campaignIDStr3
- = Enter
- + "--" + boundary
- + Enter;
- var byteData1 = System.Text.Encoding.UTF8.GetBytes(campaignIDStr1);
- var byteData3 = System.Text.Encoding.UTF8.GetBytes(campaignIDStr3);
- #endregion
- string transactionsResponse = UploadImg_postJson(url, byteData1, byteData2, byteData3, metaStr, privateKey, merchantId, serialNo, boundary, "POST");
- var result = JsonMapper.ToObject(transactionsResponse);
- return result["media_id"].ToString();
- }
- public string UploadImg_postJson(string url, byte[] b1, byte[] b2, byte[] b3, string metaStr, string privateKey, string merchantId, string serialNo, string boundary, string method = "POST")
- {
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- request.Method = method;
-
- request.ContentType = "multipart/form-data;boundary=" + boundary;
- request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36";
- request.Accept = "application/json";
- string Authorization = GetAuthorization(url, method, metaStr, privateKey, merchantId, serialNo);
- request.Headers.Add("Authorization", Authorization);
- Stream writer;
- try
- {
- writer = request.GetRequestStream();
- }
- catch (Exception)
- {
- writer = null;
- }
- writer.Write(b1, 0, b1.Length);
- writer.Write(b2, 0, b2.Length);
- writer.Write(b3, 0, b3.Length);
- writer.Close();
- HttpWebResponse response;
- try
- {
- response = (HttpWebResponse)request.GetResponse();
- }
- catch (WebException ex)
- {
- response = ex.Response as HttpWebResponse;
- }
- Stream resStream = response.GetResponseStream();
- StreamReader reader = new StreamReader(resStream);
- string text = reader.ReadToEnd();
- return text;
- }
- protected string GetAuthorization(string url, string method, string jsonParame, string privateKey, string merchantId, string serialNo)
- {
- var uri = new Uri(url);
- string urlPath = uri.PathAndQuery;
- string nonce = Guid.NewGuid().ToString();
- var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
-
- method = string.IsNullOrEmpty(method) ? "" : method;
- string message = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n", method, urlPath, timestamp, nonce, jsonParame);
- string signTxt = Sign(message, privateKey);
-
- string authorzationTxt = string.Format("WECHATPAY2-SHA256-RSA2048 mchid=\"{0}\",nonce_str=\"{1}\",timestamp=\"{2}\",serial_no=\"{3}\",signature=\"{4}\"",
- merchantId,
- nonce,
- timestamp,
- serialNo,
- signTxt
- );
- return authorzationTxt;
- }
- protected string Sign(string message, string privateKey)
- {
- byte[] keyData = Convert.FromBase64String(privateKey);
- byte[] data = System.Text.Encoding.UTF8.GetBytes(message);
- var rsa = RSA.Create();
- rsa.ImportPkcs8PrivateKey(keyData, out _);
- return Convert.ToBase64String(rsa.SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
-
-
-
-
-
- }
- #endregion
- #region 获取文件sha256
- public string GetSha256(FileStream stream)
- {
- using (SHA256 mySHA256 = SHA256.Create())
- {
- byte[] hashValue = mySHA256.ComputeHash(stream);
- return Encoding.UTF8.GetString(hashValue);
- }
- }
- #endregion
- #region 判断长期
- public string CheckForever(DateTime? time)
- {
- if (time == null)
- {
- return "";
- }
- if (time.Value.Year >= 2050)
- {
- return "长期";
- }
- return time.Value.ToString("yyyy-MM-dd");
- }
- #endregion
- }
- }
|