HaoDaHelper.cs 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. using System.Net.Mime;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using Library;
  8. using System.Net;
  9. using System.IO;
  10. using Aop.Api.Util;
  11. using LitJson;
  12. namespace MySystem
  13. {
  14. /// <summary>
  15. /// 好哒相关接口
  16. /// </summary>
  17. public class HaoDaHelper
  18. {
  19. public readonly static HaoDaHelper Instance = new HaoDaHelper();
  20. private HaoDaHelper()
  21. {
  22. }
  23. #region 盒易付
  24. //测试环境
  25. // string BoxRequestUrl = "https://openapi-test.iboxpay.com";
  26. // string BoxAppId = "AT7317781468267548672";
  27. // string BoxAppSecret = "OfWuHQdhQfnE8NaF4xdtgk4B9CYqnbri";
  28. // string BoxPublicKey = "";
  29. // string BrhCode = "001040";
  30. //生产环境
  31. // string BoxRequestUrl = "https://openapi.iboxpay.com";
  32. // string BoxAppId = "AP7175619323825451008";
  33. // string BoxAppSecret = "hUfR53gdjyfiZTBR5bILanMqRD1dXD7y";
  34. // string BoxPublicKey = "";
  35. // public static string BrhCode = "030145";
  36. // string BoxRequestUrl = "https://openapi.iboxpay.com";
  37. // string BoxAppId = "AP8322645764893728768";
  38. // string BoxAppSecret = "je1TElprp3jzWETsG3FcWqtmpreEHqZS";
  39. // string BoxPublicKey = "";
  40. // public static string BrhCode = "";
  41. /// <summary>
  42. /// 新创建商户
  43. /// </summary>
  44. /// <param name="request"></param>
  45. /// <returns></returns>
  46. public string AddNewMerchant(AddNewMerchant request, int BrandId = 0, string AgentTag = "1")
  47. {
  48. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  49. if (BrandId == 1)
  50. {
  51. BoxRequestUrl = HaodaParam.BoxRequestUrl;
  52. string oprInvitedCode = "";
  53. if (BrandId == 1)
  54. {
  55. if(string.IsNullOrEmpty(RedisDbconn.Instance.Get<string>("AgentInvitedCode:" + AgentTag)))
  56. {
  57. oprInvitedCode = RedisDbconn.Instance.Get<string>("AgentInvitedCode:1");
  58. }
  59. else
  60. {
  61. oprInvitedCode = RedisDbconn.Instance.Get<string>("AgentInvitedCode:" + AgentTag);
  62. }
  63. }
  64. request.oprInvitedCode = oprInvitedCode;
  65. }
  66. // request.brh = BrhCode;
  67. string logString = "";
  68. logString += DateTime.Now.ToString();
  69. string req = Newtonsoft.Json.JsonConvert.SerializeObject(request);
  70. logString += req;
  71. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  72. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  73. logString += "请求头\n" + head;
  74. logString += "请求参数\n" + req;
  75. logString += "请求地址:" + BoxRequestUrl + "/api/inst/mcht/new_create_mcht";
  76. string result = PostWebRequest(BoxRequestUrl + "/api/inst/mcht/new_create_mcht", req, headdic);
  77. logString += "返回\n" + result + "\n\n";
  78. LogHelper.Instance.WriteLog(logString, "好哒新创建商户", true);
  79. return result;
  80. }
  81. /// <summary>
  82. /// 查询商户审核状态
  83. /// </summary>
  84. /// <param name="instId">机构编号</param>
  85. /// <param name="mchtNo">收款商户号</param>
  86. /// <returns></returns>
  87. public string QueryMerchantStatus(string instId, string mchtNo, int BrandId = 0, string AgentTag = "1")
  88. {
  89. if(string.IsNullOrEmpty(mchtNo))
  90. {
  91. return "{}";
  92. }
  93. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  94. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  95. string logString = "";
  96. logString += DateTime.Now.ToString();
  97. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  98. reqdic.Add("instId", instId);
  99. reqdic.Add("mchtNo", mchtNo);
  100. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  101. logString += req;
  102. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  103. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  104. logString += "请求头\n" + head;
  105. logString += "请求参数\n" + req;
  106. logString += "请求地址:" + BoxRequestUrl + "/api/inst/mcht/query_mcht_audit";
  107. string result = PostWebRequest(BoxRequestUrl + "/api/inst/mcht/query_mcht_audit", req, headdic);
  108. logString += "返回\n" + result + "\n\n";
  109. LogHelper.Instance.WriteLog(logString, "好哒查询商户审核状态", true);
  110. return result;
  111. }
  112. /// <summary>
  113. /// 查询实名认证状态
  114. /// </summary>
  115. /// <param name="mchtNo">慧掌柜商户号</param>
  116. /// <param name="storeNo">慧掌柜门店号</param>
  117. /// <returns></returns>
  118. public string QueryAuthStatus(string mchtNo, string storeNo, int BrandId = 0, string AgentTag = "1")
  119. {
  120. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  121. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  122. string logString = "";
  123. logString += DateTime.Now.ToString();
  124. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  125. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  126. reqdic.Add("storeNo", storeNo); // 慧掌柜门店号
  127. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  128. logString += req;
  129. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  130. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  131. logString += "请求头\n" + head;
  132. logString += "请求参数\n" + req;
  133. logString += "请求地址:" + BoxRequestUrl + "/api/hzg/v2/wechat/queryVerifiedResult.json";
  134. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/wechat/queryVerifiedResult.json", req, headdic);
  135. logString += "返回\n" + result + "\n\n";
  136. LogHelper.Instance.WriteLog(logString, "好哒查询实名认证状态", true);
  137. return result;
  138. }
  139. /// <summary>
  140. /// 查询实名认证二维码
  141. /// </summary>
  142. /// <param name="mchtNo">慧掌柜商户号</param>
  143. /// <param name="storeNo">慧掌柜门店号</param>
  144. /// <returns></returns>
  145. public string QueryAuthQRcode(string mchtNo, string storeNo, int BrandId = 0, string AgentTag = "1")
  146. {
  147. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  148. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  149. string logString = "";
  150. logString += DateTime.Now.ToString();
  151. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  152. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  153. reqdic.Add("storeNo", storeNo); // 慧掌柜门店号
  154. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  155. logString += req;
  156. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  157. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  158. logString += "请求头\n" + head;
  159. logString += "请求参数\n" + req;
  160. logString += "请求地址:" + BoxRequestUrl + "/api/hzg/v2/wechat/queryVerifiedQrcode.json";
  161. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/wechat/queryVerifiedQrcode.json", req, headdic);
  162. logString += "返回\n" + result + "\n\n";
  163. LogHelper.Instance.WriteLog(logString, "好哒查询实名认证二维码", true);
  164. return result;
  165. }
  166. /// <summary>
  167. /// 微信公众号和小程序支付接口
  168. /// </summary>
  169. /// <param name="mchtNo">盒子的商户编号</param>
  170. /// <param name="storeNo">盒子的门店编码</param>
  171. /// <param name="outOrderNo">商户订单号</param>
  172. /// <param name="transAmount">交易金额,单位:分</param>
  173. /// <param name="callbackUrl">交易完成之后,商户接收交易结果通知的地址</param>
  174. /// <param name="subOpenId">用户子标识</param>
  175. /// <param name="confirmCode">支付确认码</param>
  176. /// <param name="subAppId">公众号或者小程序appId</param>
  177. /// <param name="subAppIdType">对应的类型,默认为公众号, SUBSCRIPTION: 公众号 MINI_PROGRAM: 小程序</param>
  178. /// <param name="ledgerModel">分账类型:1不分账 2分账,注意只有开通了分账才能传这个字段</param>
  179. /// <returns></returns>
  180. public string WeChatPay(string mchtNo, string storeNo, string outOrderNo, string transAmount, string callbackUrl, string subOpenId, string confirmCode, string subAppId, string subAppIdType, string ledgerModel, int BrandId = 0, string AgentTag = "1")
  181. {
  182. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  183. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  184. string logString = "";
  185. logString += DateTime.Now.ToString();
  186. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  187. reqdic.Add("mchtNo", mchtNo); //盒子的商户编号
  188. reqdic.Add("storeNo", storeNo); //盒子的门店编码
  189. reqdic.Add("outOrderNo", outOrderNo); //商户订单号
  190. reqdic.Add("transAmount", transAmount); //交易金额,单位:分
  191. reqdic.Add("callbackUrl", callbackUrl); //交易完成之后,商户接收交易结果通知的地址
  192. reqdic.Add("subOpenId", subOpenId); //用户子标识
  193. reqdic.Add("confirmCode", confirmCode); //支付确认码,由4位纯数字组成,每次请求时随机生成
  194. reqdic.Add("subAppId", subAppId); //公众号或者小程序appId
  195. reqdic.Add("subAppIdType", subAppIdType); //subAppId 对应的类型,默认为公众号, SUBSCRIPTION: 公众号 MINI_PROGRAM: 小程序
  196. reqdic.Add("ledgerModel", ledgerModel); //分账类型:1不分账 2分账,注意只有开通了分账才能传这个字段
  197. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  198. logString += req;
  199. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  200. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  201. logString += "请求头\n" + head;
  202. logString += "请求参数\n" + req;
  203. logString += "请求地址:" + BoxRequestUrl + "/api/hzg/v2/unitedtrade/wechat_js_pay";
  204. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/unitedtrade/wechat_js_pay", req, headdic);
  205. logString += "返回\n" + result + "\n\n";
  206. LogHelper.Instance.WriteLog(logString, "好哒微信公众号和小程序支付", true);
  207. return result;
  208. }
  209. /// <summary>
  210. /// 支付宝服务窗支付接口
  211. /// </summary>
  212. /// <param name="mchtNo">盒子的商户编号</param>
  213. /// <param name="storeNo">盒子的门店编码</param>
  214. /// <param name="outOrderNo">商户订单号</param>
  215. /// <param name="transAmount">交易金额,单位:分</param>
  216. /// <param name="callbackUrl">交易完成之后,商户接收交易结果通知的地址</param>
  217. /// <param name="subOpenId">支付宝用户标识</param>
  218. /// <param name="confirmCode">支付确认码,由4位纯数字组成,每次请求时随机生成</param>
  219. /// <returns></returns>
  220. public string Alipay(string mchtNo, string storeNo, string outOrderNo, string transAmount, string callbackUrl, string subOpenId, string confirmCode, int BrandId = 0, string AgentTag = "1")
  221. {
  222. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  223. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  224. string logString = "";
  225. logString += DateTime.Now.ToString();
  226. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  227. reqdic.Add("mchtNo", mchtNo); //盒子的商户编号
  228. reqdic.Add("storeNo", storeNo); //盒子的门店编码
  229. reqdic.Add("outOrderNo", outOrderNo); //商户订单号
  230. reqdic.Add("transAmount", transAmount); //交易金额,单位:分
  231. reqdic.Add("callbackUrl", callbackUrl); //交易完成之后,商户接收交易结果通知的地址
  232. reqdic.Add("subOpenId", subOpenId); //支付宝用户标识
  233. reqdic.Add("confirmCode", confirmCode); //支付确认码,由4位纯数字组成,每次请求时随机生成
  234. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  235. logString += req;
  236. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  237. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  238. logString += "请求头\n" + head;
  239. logString += "请求参数\n" + req;
  240. logString += "请求地址:" + BoxRequestUrl + "/api/hzg/v2/unitedtrade/wechat_js_pay";
  241. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/unitedtrade/wechat_js_pay", req, headdic);
  242. logString += "返回\n" + result + "\n\n";
  243. LogHelper.Instance.WriteLog(logString, "好哒新创建商户", true);
  244. return result;
  245. }
  246. /// <summary>
  247. /// 聚合支付退款接口
  248. /// </summary>
  249. /// <param name="mchtNo">盒子的商户编号</param>
  250. /// <param name="storeNo">盒子的门店编号</param>
  251. /// <param name="outRefundNo">商户退款订单号</param>
  252. /// <param name="outOrderNo">商户支付订单号</param>
  253. /// <param name="refundAmount">退款金额,单位:分</param>
  254. /// <returns></returns>
  255. public string AggregatedPayRefund(string mchtNo, string storeNo, string outRefundNo, string outOrderNo, string refundAmount, int BrandId = 0, string AgentTag = "1")
  256. {
  257. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  258. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  259. string logString = "";
  260. logString += DateTime.Now.ToString();
  261. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  262. reqdic.Add("mchtNo", mchtNo); // 盒子的商户编号
  263. reqdic.Add("storeNo", storeNo); // 盒子的门店编号
  264. reqdic.Add("outRefundNo", outRefundNo); //32 商户退款订单号
  265. reqdic.Add("outOrderNo", outOrderNo); // 商户支付订单号
  266. reqdic.Add("refundAmount", refundAmount); //10 退款金额,单位:分
  267. reqdic.Add("refundReason", "门店消费补贴"); //退款原因
  268. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  269. logString += req;
  270. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  271. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  272. logString += "请求头\n" + head;
  273. logString += "请求参数\n" + req;
  274. logString += "请求地址:" + BoxRequestUrl + "/api/hzg/v2/unitedtrade/refund";
  275. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/unitedtrade/refund", req, headdic);
  276. logString += "返回\n" + result + "\n\n";
  277. LogHelper.Instance.WriteLog(logString, "好哒聚合支付退款", true);
  278. return result;
  279. }
  280. /// <summary>
  281. /// 开通分账功能
  282. /// </summary>
  283. /// <param name="mchtNo">慧掌柜商户号(进件接口返回的outMchtNo字段)</param>
  284. /// <param name="storeNo">慧掌柜门店号</param>
  285. /// <returns></returns>
  286. public string OpenDivideAccounts(string mchtNo, string storeNo, int BrandId = 0, string AgentTag = "1")
  287. {
  288. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  289. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  290. string logString = "";
  291. logString += DateTime.Now.ToString();
  292. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  293. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  294. reqdic.Add("storeNo", storeNo); // 慧掌柜门店号
  295. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  296. logString += req;
  297. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  298. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  299. logString += "请求头\n" + head;
  300. logString += "请求参数\n" + req;
  301. logString += "请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/mcht/reg";
  302. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/mcht/reg", req, headdic);
  303. logString += "返回\n" + result + "\n\n";
  304. LogHelper.Instance.WriteLog(logString, "好哒开通分账", true);
  305. return result;
  306. }
  307. /// <summary>
  308. /// 分账接收方签约
  309. /// </summary>
  310. /// <param name="acctNo">账户号</param>
  311. /// <param name="mchtNo">慧掌柜商户号</param>
  312. /// <param name="storeNo">慧掌柜门店号</param>
  313. /// <returns></returns>
  314. public string DivideAccountsSign(string acctNo, string mchtNo, string storeNo, int BrandId = 0, string AgentTag = "1")
  315. {
  316. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  317. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  318. string logString = "";
  319. logString += DateTime.Now.ToString();
  320. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  321. reqdic.Add("acctNo", acctNo); // 账户号
  322. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  323. reqdic.Add("storeNo", storeNo); // 慧掌柜门店号
  324. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  325. logString += req;
  326. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  327. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  328. logString += "请求头\n" + head;
  329. logString += "请求参数\n" + req;
  330. logString += "请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/acct/bind_mcht";
  331. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/acct/bind_mcht", req, headdic);
  332. logString += "返回\n" + result + "\n\n";
  333. LogHelper.Instance.WriteLog(logString, "好哒分账接收方签约", true);
  334. return result;
  335. }
  336. /// <summary>
  337. /// 添加分账接收方账户
  338. /// </summary>
  339. /// <param name="request"></param>
  340. /// <returns></returns>
  341. public string AddOpenDivideAccounts(AddOpenDivideAccounts request, int BrandId = 0, string AgentTag = "1")
  342. {
  343. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  344. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  345. string logString = "";
  346. logString += DateTime.Now.ToString();
  347. string req = Newtonsoft.Json.JsonConvert.SerializeObject(request);
  348. logString += req;
  349. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  350. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  351. logString += "请求头\n" + head;
  352. logString += "请求参数\n" + req;
  353. logString += "请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/acct/register";
  354. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/acct/register", req, headdic);
  355. logString += "返回\n" + result + "\n\n";
  356. LogHelper.Instance.WriteLog(logString, "好哒添加分账接收方账户", true);
  357. return result;
  358. }
  359. /// <summary>
  360. /// 分账交易详情查询
  361. /// </summary>
  362. /// <param name="mchtNo">慧掌柜商户号</param>
  363. /// <param name="orderNo">交易订单号 </param>
  364. /// <returns></returns>
  365. public string QueryOrderDivideAccounts(string mchtNo, string orderNo, int BrandId = 0, string AgentTag = "1")
  366. {
  367. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  368. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  369. string logString = "";
  370. logString += DateTime.Now.ToString();
  371. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  372. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  373. reqdic.Add("orderNo", orderNo); // 交易订单号
  374. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  375. logString += req;
  376. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  377. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  378. logString += "请求头\n" + head;
  379. logString += "请求参数\n" + req;
  380. logString += "请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/acct/bind_mcht";
  381. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/acct/bind_mcht", req, headdic);
  382. logString += "返回\n" + result + "\n\n";
  383. LogHelper.Instance.WriteLog(logString, "好哒分账交易详情查询", true);
  384. return result;
  385. }
  386. /// <summary>
  387. /// 分账交易详情查询
  388. /// </summary>
  389. /// <param name="mchtNo">慧掌柜商户号</param>
  390. /// <param name="orderNo">交易订单号 </param>
  391. /// <returns></returns>
  392. public string QueryOrderDivideDetail(string mchtNo, string orderNo, int BrandId = 0, string AgentTag = "1")
  393. {
  394. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  395. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  396. string logString = "";
  397. logString += DateTime.Now.ToString();
  398. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  399. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  400. reqdic.Add("orderNo", orderNo); // 交易订单号
  401. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  402. logString += req;
  403. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  404. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  405. logString += "请求头\n" + head;
  406. logString += "请求参数\n" + req;
  407. logString += "请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/trade/clear/get";
  408. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/trade/clear/get", req, headdic);
  409. logString += "返回\n" + result + "\n\n";
  410. LogHelper.Instance.WriteLog(logString, "好哒分账交易详情查询", true);
  411. return result;
  412. }
  413. /// <summary>
  414. /// 订单分账申请
  415. /// </summary>
  416. /// <param name="request"></param>
  417. /// <returns></returns>
  418. ///
  419. public string OrderDivideAccounts(OrderDivideAccounts request, int BrandId = 0, string AgentTag = "1")
  420. {
  421. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  422. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  423. string logString = "";
  424. logString += DateTime.Now.ToString();
  425. string req = Newtonsoft.Json.JsonConvert.SerializeObject(request);
  426. logString += req;
  427. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  428. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  429. logString += "请求头\n" + head;
  430. logString += "请求参数\n" + req;
  431. logString += "请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/order/apply";
  432. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/order/apply", req, headdic);
  433. logString += "返回\n" + result + "\n\n";
  434. LogHelper.Instance.WriteLog(logString, "好哒订单分账申请", true);
  435. return result;
  436. }
  437. /// <summary>
  438. /// 订单分账结果查询
  439. /// </summary>
  440. /// <param name="applyNo">分账批次号</param>
  441. /// <param name="mchtNo">慧掌柜商户号</param>
  442. /// <returns></returns>
  443. public string OrderDivideAccountsQuery(string applyNo, string mchtNo, int BrandId = 0, string AgentTag = "1")
  444. {
  445. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  446. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  447. string logString = "";
  448. logString += DateTime.Now.ToString();
  449. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  450. reqdic.Add("applyNo", applyNo); // 账户号
  451. reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
  452. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  453. logString += req;
  454. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  455. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  456. logString += "请求头\n" + head;
  457. logString += "请求参数\n" + req;
  458. logString += "请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/apply/query";
  459. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/apply/query", req, headdic);
  460. logString += "返回\n" + result + "\n\n";
  461. LogHelper.Instance.WriteLog(logString, "好哒订单分账结果查询", true);
  462. return result;
  463. }
  464. /// <summary>
  465. /// 商户修改银行卡
  466. /// </summary>
  467. /// <param name="request"></param>
  468. /// <returns></returns>
  469. public string ChangeMerchantCard(ChangeMerchantCard request, int BrandId = 0, string AgentTag = "1")
  470. {
  471. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  472. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  473. string logString = "";
  474. logString += DateTime.Now.ToString();
  475. string req = Newtonsoft.Json.JsonConvert.SerializeObject(request);
  476. logString += req;
  477. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  478. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  479. logString += "请求头\n" + head;
  480. logString += "请求参数\n" + req;
  481. logString += "请求地址:" + BoxRequestUrl + "/api/inst/mcht/update_acct";
  482. string result = PostWebRequest(BoxRequestUrl + "/api/inst/mcht/update_acct", req, headdic);
  483. logString += "返回\n" + result + "\n\n";
  484. LogHelper.Instance.WriteLog(logString, "好哒商户修改银行卡", true);
  485. return result;
  486. }
  487. /// <summary>
  488. /// 查询修改银行卡任务审核状态
  489. /// </summary>
  490. /// <param name="mchtNo">收款商户号</param>
  491. /// <param name="taskId">任务id</param>
  492. /// <returns></returns>
  493. public string QueryChangeCardStatus(string mchtNo, string taskId, int BrandId = 0, string AgentTag = "1")
  494. {
  495. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  496. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  497. string logString = "";
  498. logString += DateTime.Now.ToString();
  499. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  500. reqdic.Add("mchtNo", mchtNo);
  501. reqdic.Add("taskId", taskId);
  502. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  503. logString += req;
  504. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  505. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  506. logString += "请求头\n" + head;
  507. logString += "请求参数\n" + req;
  508. logString += "请求地址:" + BoxRequestUrl + "/api/inst/mcht/query_acct_task";
  509. string result = PostWebRequest(BoxRequestUrl + "/api/inst/mcht/query_acct_task", req, headdic);
  510. logString += "返回\n" + result + "\n\n";
  511. LogHelper.Instance.WriteLog(logString, "好哒查询修改银行卡任务审核状态", true);
  512. return result;
  513. }
  514. /// <summary>
  515. /// 绑定微信appid接口
  516. /// </summary>
  517. /// <param name="mchtNo">盒子的商户编号</param>
  518. /// <param name="storeNo">盒子的门店编号</param>
  519. /// <param name="subAppId">公众号appid或者小程序appid</param>
  520. /// <returns></returns>
  521. public string BindWeChatAppId(string mchtNo, string storeNo, string subAppId, int BrandId = 0, string AgentTag = "1")
  522. {
  523. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  524. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  525. string logString = "";
  526. logString += DateTime.Now.ToString();
  527. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  528. reqdic.Add("mchtNo", mchtNo); // 盒子商户号
  529. reqdic.Add("storeNo", storeNo); // 盒子门店号
  530. reqdic.Add("subAppId", subAppId); // 公众号appid或者小程序appid
  531. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  532. logString += req;
  533. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  534. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  535. logString += "请求头\n" + head;
  536. logString += "请求参数\n" + req;
  537. logString += "请求地址:" + BoxRequestUrl + "/api/hzg/v2/wechat/add_sub_config";
  538. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/wechat/add_sub_config", req, headdic);
  539. logString += "返回\n" + result + "\n\n";
  540. LogHelper.Instance.WriteLog(logString, "好哒绑定微信appid接口", true);
  541. return result;
  542. }
  543. /// <summary>
  544. /// 到账记录查询
  545. /// </summary>
  546. /// <param name="mchtNo">商户号</param>
  547. /// <param name="startTime">查询开始时间(yyyy-MM-dd)</param>
  548. /// <param name="endTime">查询结束时间(yyyy-MM-dd),最大查询时间区间31天</param>
  549. /// <param name="tradeMchtNo">收单商户号(15位)</param>
  550. /// <param name="cardNo">结算卡号</param>
  551. /// <param name="settleStatus">出款状态(0:入账成功、1:入账失败、2:结算超时、3:入账受理成功、4:已提交结算请求)</param>
  552. /// <param name="page">当前页数,默认1</param>
  553. /// <param name="rows">每页大小,默认10,最大500</param>
  554. /// <returns></returns>
  555. public string QueryAccountList(string mchtNo, string startTime, string endTime, string tradeMchtNo, string cardNo, string settleStatus, string page = "1", string rows = "10", int BrandId = 0, string AgentTag = "1")
  556. {
  557. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  558. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  559. string logString = "";
  560. logString += DateTime.Now.ToString();
  561. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  562. reqdic.Add("mchtNo", mchtNo); // 盒子商户号
  563. reqdic.Add("startTime", startTime); // 查询开始时间(yyyy-MM-dd)
  564. reqdic.Add("endTime", endTime); // 查询结束时间(yyyy-MM-dd),最大查询时间区间31天
  565. reqdic.Add("tradeMchtNo", tradeMchtNo); // 收单商户号(15位)
  566. reqdic.Add("cardNo", cardNo); // 结算卡号
  567. reqdic.Add("settleStatus", settleStatus); // 出款状态(0:入账成功、1:入账失败、2:结算超时、3:入账受理成功、4:已提交结算请求)
  568. reqdic.Add("page", page); // 当前页数,默认1
  569. reqdic.Add("rows", rows); // 每页大小,默认10,最大500
  570. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  571. logString += req;
  572. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  573. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  574. logString += "请求头\n" + head;
  575. logString += "请求参数\n" + req;
  576. logString += "请求地址:" + BoxRequestUrl + "/api/v3/hzg/accb/to_account/query";
  577. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/accb/to_account/query", req, headdic);
  578. logString += "返回\n" + result + "\n\n";
  579. LogHelper.Instance.WriteLog(logString, "好哒到账记录查询", true);
  580. return result;
  581. }
  582. /// <summary>
  583. /// 到账记录详情
  584. /// </summary>
  585. /// <param name="mchtNo">商户号</param>
  586. /// <param name="id"></param>
  587. /// <returns></returns>
  588. public string QueryAccountDetail(string mchtNo, string id, int BrandId = 0, string AgentTag = "1")
  589. {
  590. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  591. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  592. string logString = "";
  593. logString += DateTime.Now.ToString();
  594. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  595. reqdic.Add("mchtNo", mchtNo); // 商户号
  596. reqdic.Add("id", id); // id
  597. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  598. logString += req;
  599. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  600. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  601. logString += "请求头\n" + head;
  602. logString += "请求参数\n" + req;
  603. logString += "请求地址:" + BoxRequestUrl + "/api/v3/hzg/accb/to_account/get";
  604. string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/accb/to_account/get", req, headdic);
  605. logString += "返回\n" + result + "\n\n";
  606. LogHelper.Instance.WriteLog(logString, "好哒到账记录详情", true);
  607. return result;
  608. }
  609. /// <summary>
  610. /// 聚合支付查询接口
  611. /// </summary>
  612. /// <param name="mchtNo">商户号</param>
  613. /// <param name="outOrderNo">商户订单号</param>
  614. /// <returns></returns>
  615. public string QueryTradeDetail(string mchtNo, string outOrderNo, int BrandId = 0, string AgentTag = "1")
  616. {
  617. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  618. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  619. string logString = "";
  620. logString += DateTime.Now.ToString();
  621. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  622. reqdic.Add("mchtNo", mchtNo); // 商户号
  623. reqdic.Add("outOrderNo", outOrderNo); // 商户订单号
  624. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  625. logString += req;
  626. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  627. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  628. logString += "请求头\n" + head;
  629. logString += "请求参数\n" + req;
  630. logString += "请求地址:" + BoxRequestUrl + "/api/hzg/v2/unitedtrade/query_status";
  631. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/unitedtrade/query_status", req, headdic);
  632. logString += "返回\n" + result + "\n\n";
  633. LogHelper.Instance.WriteLog(logString, "好哒聚合支付查询", true);
  634. return result;
  635. }
  636. /// <summary>
  637. /// 聚合支付退款查询接口
  638. /// </summary>
  639. /// <param name="mchtNo">商户号</param>
  640. /// <param name="storeNo">门店编号</param>
  641. /// <param name="outRefundNo">退款订单号</param>
  642. /// <returns></returns>
  643. public string QueryRefundDetail(string mchtNo, string storeNo, string outRefundNo, int BrandId = 0, string AgentTag = "1")
  644. {
  645. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  646. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  647. string logString = "";
  648. logString += DateTime.Now.ToString();
  649. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  650. reqdic.Add("mchtNo", mchtNo); // 商户号
  651. reqdic.Add("storeNo", storeNo); // 商户订单号
  652. reqdic.Add("outRefundNo", outRefundNo);
  653. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  654. logString += req;
  655. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  656. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  657. logString += "请求头\n" + head;
  658. logString += "请求参数\n" + req;
  659. logString += "请求地址:" + BoxRequestUrl + "/api/hzg/v2/unitedtrade/refund_query";
  660. string result = PostWebRequest(BoxRequestUrl + "/api/hzg/v2/unitedtrade/refund_query", req, headdic);
  661. logString += "返回\n" + result + "\n\n";
  662. LogHelper.Instance.WriteLog(logString, "好哒退款查询", true);
  663. return result;
  664. }
  665. /// <summary>
  666. /// 修改商户费率
  667. /// </summary>
  668. /// <param name="request"></param>
  669. /// <returns></returns>
  670. public string SetFee(string mchtNo, string feeRate, int BrandId = 0, string AgentTag = "1")
  671. {
  672. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  673. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  674. string logString = "";
  675. logString += DateTime.Now.ToString();
  676. Dictionary<string, object> request = new Dictionary<string, object>();
  677. request.Add("mchtNo", mchtNo); // 商户号
  678. request.Add("alipayRate", feeRate);
  679. request.Add("wechatPayRate", feeRate);
  680. request.Add("unionPayRate", feeRate);
  681. string req = Newtonsoft.Json.JsonConvert.SerializeObject(request);
  682. logString += req;
  683. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  684. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  685. logString += "请求头\n" + head;
  686. logString += "请求参数\n" + req;
  687. logString += "请求地址:" + BoxRequestUrl + "/api/inst/mcht/update_disc";
  688. string result = PostWebRequest(BoxRequestUrl + "/api/inst/mcht/update_disc", req, headdic);
  689. logString += "返回\n" + result + "\n\n";
  690. LogHelper.Instance.WriteLog(logString, "好哒商户修改费率", true);
  691. return result;
  692. }
  693. public Dictionary<string, string> GetHeader(string req, int BrandId = 0, string Agent = "1")
  694. {
  695. var BoxAppId = HaodaUnionParam.BoxAppId;
  696. var BoxAppSecret = HaodaUnionParam.BoxAppSecret;
  697. if (BrandId == 1)
  698. {
  699. if(string.IsNullOrEmpty(RedisDbconn.Instance.Get<string>("AgentAppId:" + Agent)))
  700. {
  701. BoxAppId = RedisDbconn.Instance.Get<string>("AgentAppId:1");
  702. BoxAppSecret = RedisDbconn.Instance.Get<string>("AgentAppSecret:1");
  703. }
  704. else
  705. {
  706. BoxAppId = RedisDbconn.Instance.Get<string>("AgentAppId:" + Agent);
  707. BoxAppSecret = RedisDbconn.Instance.Get<string>("AgentAppSecret:" + Agent);
  708. }
  709. }
  710. Dictionary<string, string> headdic = new Dictionary<string, string>();
  711. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  712. // headdic.Add("appid", BoxAppId);//开发者id
  713. // headdic.Add("appsecret", BoxAppSecret);//开发者密码
  714. headdic.Add("X-Up-AppId", BoxAppId);
  715. headdic.Add("X-Timestamp", timestamp);
  716. headdic.Add("X-Sign-Type", "SHA-256");
  717. headdic.Add("X-Sign", SHA256Sign(timestamp + BoxAppSecret + req));
  718. return headdic;
  719. }
  720. public string SHA256Sign(string toSignStr)
  721. {
  722. byte[] toSignByte = Encoding.UTF8.GetBytes(toSignStr);
  723. SHA256 sha256 = SHA256.Create();
  724. byte[] signByte = sha256.ComputeHash(toSignByte);
  725. string sign = Convert.ToBase64String(signByte);
  726. return sign;
  727. }
  728. public bool VerifySign(string toSignStr, string signStr, int BrandId = 0, string AgentTag = "1")
  729. {
  730. var BoxPublicKey = HaodaUnionParam.BoxPublicKey;
  731. if (BrandId == 1) BoxPublicKey = HaodaParam.BoxPublicKey;
  732. byte[] toSignByte = Encoding.Default.GetBytes(toSignStr);
  733. byte[] signByte = Convert.FromBase64String(signStr);
  734. var toKey = Convert.FromBase64String(BoxPublicKey);
  735. var rsaroot = RSA.Create();
  736. rsaroot.ImportSubjectPublicKeyInfo(toKey, out _);
  737. var publicKeyParameters = rsaroot.ExportParameters(false);
  738. using (var rsa = RSA.Create())
  739. {
  740. rsa.ImportParameters(publicKeyParameters);
  741. var sha256 = SHA256.Create();
  742. var hash = sha256.ComputeHash(toSignByte);
  743. return rsa.VerifyHash(hash, signByte, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  744. }
  745. }
  746. public string SignData(string toSignStr, int BrandId = 0, string AgentTag = "1")
  747. {
  748. var BoxPublicKey = HaodaUnionParam.BoxPublicKey;
  749. if (BrandId == 1) BoxPublicKey = HaodaParam.BoxPublicKey;
  750. byte[] toSignByte = Encoding.UTF8.GetBytes(toSignStr);
  751. var toKey = Convert.FromBase64String(BoxPublicKey);
  752. var rsaroot = RSA.Create();
  753. rsaroot.ImportSubjectPublicKeyInfo(toKey, out _);
  754. var publicKeyParameters = rsaroot.ExportParameters(false);
  755. using (var rsa = RSA.Create())
  756. {
  757. rsa.ImportParameters(publicKeyParameters);
  758. var sha256 = SHA256.Create();
  759. var hash = sha256.ComputeHash(toSignByte);
  760. byte[] endByte = rsa.SignData(toSignByte, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  761. return Convert.ToBase64String(endByte);
  762. }
  763. }
  764. private string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
  765. {
  766. string ret = string.Empty;
  767. try
  768. {
  769. string logString = "";
  770. logString += DateTime.Now.ToString();
  771. logString += postUrl;
  772. logString += paramData;
  773. byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
  774. // 设置提交的相关参数
  775. System.Net.HttpWebRequest request = System.Net.WebRequest.Create(postUrl) as System.Net.HttpWebRequest;
  776. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
  777. request.Method = "POST";
  778. request.KeepAlive = false;
  779. request.AllowAutoRedirect = true;
  780. request.ContentType = "application/json";
  781. foreach (string key in headers.Keys)
  782. {
  783. request.Headers.Add(key, headers[key]);
  784. }
  785. request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
  786. request.ContentLength = postData.Length;
  787. // 提交请求数据
  788. System.IO.Stream outputStream = request.GetRequestStream();
  789. outputStream.Write(postData, 0, postData.Length);
  790. outputStream.Close();
  791. System.Net.HttpWebResponse response;
  792. System.IO.Stream responseStream;
  793. System.IO.StreamReader reader;
  794. string srcString;
  795. response = request.GetResponse() as System.Net.HttpWebResponse;
  796. responseStream = response.GetResponseStream();
  797. reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.UTF8);
  798. srcString = reader.ReadToEnd();
  799. ret = srcString; //返回值赋值
  800. reader.Close();
  801. logString += srcString;
  802. LogHelper.Instance.WriteLog(logString, "请求好哒API日志", true);
  803. }
  804. catch (System.Net.WebException ex)
  805. {
  806. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)ex.Response;
  807. System.IO.Stream myResponseStream = response.GetResponseStream();
  808. //获取响应内容
  809. System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myResponseStream);
  810. ret = myStreamReader.ReadToEnd();
  811. myResponseStream.Close();
  812. }
  813. catch (Exception ex)
  814. {
  815. ret = "fail";
  816. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "请求好哒API异常", true);
  817. }
  818. return ret;
  819. }
  820. /// <summary>
  821. /// 获取文件令牌接口
  822. /// </summary>
  823. /// <param name="fileName">文件名</param>
  824. /// <param name="action">操作类型 0 上传图片</param>
  825. /// <returns></returns>
  826. public string GetToken(string fileName, string action, int BrandId = 0, string AgentTag = "1")
  827. {
  828. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  829. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  830. string logString = "";
  831. logString += DateTime.Now.ToString();
  832. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  833. reqdic.Add("fileName", fileName); // 文件名
  834. reqdic.Add("action", action); // 操作类型 0 上传图片
  835. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  836. logString += req;
  837. Dictionary<string, string> headdic = GetHeader(req, BrandId, AgentTag);
  838. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  839. logString += "请求头\n" + head;
  840. logString += "请求参数\n" + req;
  841. logString += "请求地址:" + BoxRequestUrl + "/api/auth/v1/get_file_token";
  842. string result = PostWebRequest(BoxRequestUrl + "/api/auth/v1/get_file_token", req, headdic);
  843. logString += "返回\n" + result + "\n\n";
  844. LogHelper.Instance.WriteLog(logString, "好哒获取文件令牌接口", true);
  845. return result;
  846. }
  847. /// <summary>
  848. /// 执行带文件上传的HTTP POST请求。
  849. /// </summary>
  850. /// <param name="url">请求地址</param>
  851. /// <param name="textParams">请求文本参数</param>
  852. /// <param name="fileParams">请求文件参数</param>
  853. /// <param name="charset">编码字符集</param>
  854. /// <returns>HTTP响应</returns>
  855. public string DoPost(string url, string token, IDictionary<string, string> textParams, IDictionary<string, FileItem> fileParams, string charset = "utf-8")
  856. {
  857. // 如果没有文件参数,则走普通POST请求
  858. if (fileParams == null || fileParams.Count == 0)
  859. {
  860. return "";
  861. }
  862. string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
  863. HttpWebRequest req = GetWebRequest(url, "POST");
  864. req.Headers.Add("X-File-Token", token);
  865. req.ContentType = "multipart/form-data;charset=" + charset + ";boundary=" + boundary;
  866. Stream reqStream = req.GetRequestStream();
  867. byte[] itemBoundaryBytes = Encoding.GetEncoding(charset).GetBytes("\r\n--" + boundary + "\r\n");
  868. byte[] endBoundaryBytes = Encoding.GetEncoding(charset).GetBytes("\r\n--" + boundary + "--\r\n");
  869. // 组装文本请求参数
  870. string textTemplate = "Content-Disposition:form-data;name=\"{0}\"\r\nContent-Type:text/plain\r\n\r\n{1}";
  871. IEnumerator<KeyValuePair<string, string>> textEnum = textParams.GetEnumerator();
  872. while (textEnum.MoveNext())
  873. {
  874. string textEntry = string.Format(textTemplate, textEnum.Current.Key, textEnum.Current.Value);
  875. byte[] itemBytes = Encoding.GetEncoding(charset).GetBytes(textEntry);
  876. reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
  877. reqStream.Write(itemBytes, 0, itemBytes.Length);
  878. }
  879. // 组装文件请求参数
  880. string fileTemplate = "Content-Disposition:form-data;name=\"{0}\";filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n";
  881. IEnumerator<KeyValuePair<string, FileItem>> fileEnum = fileParams.GetEnumerator();
  882. while (fileEnum.MoveNext())
  883. {
  884. string key = fileEnum.Current.Key;
  885. FileItem fileItem = fileEnum.Current.Value;
  886. string fileEntry = string.Format(fileTemplate, key, fileItem.GetFileName(), fileItem.GetMimeType());
  887. byte[] itemBytes = Encoding.GetEncoding(charset).GetBytes(fileEntry);
  888. reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
  889. reqStream.Write(itemBytes, 0, itemBytes.Length);
  890. byte[] fileBytes = fileItem.GetContent();
  891. reqStream.Write(fileBytes, 0, fileBytes.Length);
  892. }
  893. reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
  894. reqStream.Close();
  895. HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
  896. Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
  897. return GetResponseAsString(rsp, encoding);
  898. }
  899. public HttpWebRequest GetWebRequest(string url, string method)
  900. {
  901. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  902. req.ServicePoint.Expect100Continue = false;
  903. req.Method = method;
  904. req.KeepAlive = true;
  905. req.Timeout = 100000;
  906. return req;
  907. }
  908. /// <summary>
  909. /// 把响应流转换为文本。
  910. /// </summary>
  911. /// <param name="rsp">响应流对象</param>
  912. /// <param name="encoding">编码方式</param>
  913. /// <returns>响应文本</returns>
  914. public string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
  915. {
  916. StringBuilder result = new StringBuilder();
  917. Stream stream = null;
  918. StreamReader reader = null;
  919. try
  920. {
  921. // 以字符流的方式读取HTTP响应
  922. stream = rsp.GetResponseStream();
  923. reader = new StreamReader(stream, encoding);
  924. // 按字符读取并写入字符串缓冲
  925. int ch = -1;
  926. while ((ch = reader.Read()) > -1)
  927. {
  928. // 过滤结束符
  929. char c = (char)ch;
  930. if (c != '\0')
  931. {
  932. result.Append(c);
  933. }
  934. }
  935. }
  936. finally
  937. {
  938. // 释放资源
  939. if (reader != null) reader.Close();
  940. if (stream != null) stream.Close();
  941. if (rsp != null) rsp.Close();
  942. }
  943. return result.ToString();
  944. }
  945. /// <summary>
  946. /// 获取好哒开放平台文件名
  947. /// </summary>
  948. /// <param name="token">上传图片凭证token</param>
  949. /// <param name="value">图片路径</param>
  950. /// <returns></returns>
  951. public string ReturnFileName(string token, string value, int BrandId = 0, string AgentTag = "1")
  952. {
  953. var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
  954. if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
  955. Dictionary<string, string> file = new Dictionary<string, string>();
  956. file.Add("fileName", value.Substring(value.LastIndexOf("/")).Trim('/'));
  957. if (string.IsNullOrEmpty(value.Split(',')[0]))
  958. {
  959. return "";
  960. }
  961. FileItem item = PublicFunction.GetNetFileItem(AppConfig.Base.OssHost + value.Split(',')[0]);
  962. // FileItem item = new FileItem(function.getPath(value.Split(',')[0]));
  963. Dictionary<string, FileItem> files = new Dictionary<string, FileItem>();
  964. files.Add("file", item);
  965. var upLoadInfo = JsonMapper.ToObject(HaoDaHelper.Instance.DoPost(BoxRequestUrl + "/api/file/v1/upload_file", token, file, files, "utf-8"));
  966. if (upLoadInfo["code"].ToString() == "1")
  967. {
  968. return upLoadInfo["data"]["fileName"].ToString();
  969. }
  970. else
  971. {
  972. return "false";
  973. }
  974. }
  975. #endregion
  976. }
  977. }