HaoDaHelper.cs 53 KB

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