HaoDaHelper.cs 54 KB

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