WxPayApi.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. using System;
  2. using System.Security.Cryptography.X509Certificates;
  3. using System.Threading.Tasks;
  4. using WayneCloud.Models.Models;
  5. namespace Wechat.PayAPI
  6. {
  7. public class WxPayApi
  8. {
  9. private NLog.Logger logger = NLog.LogManager.GetLogger("Main");
  10. /**
  11. * 提交被扫支付API
  12. * 收银员使用扫码设备读取微信用户刷卡授权码以后,二维码或条码信息传送至商户收银台,
  13. * 由商户收银台或者商户后台调用该接口发起支付。
  14. * @param WxPayData inputObj 提交给被扫支付API的参数
  15. * @param int timeOut 超时时间
  16. * @throws WxPayException
  17. * @return 成功时返回调用结果,其他抛异常
  18. */
  19. public static async Task<WxPayData> Micropay(WxPayData inputObj, WxPayConfig config, int timeOut = 10)
  20. {
  21. string url = "https://api.mch.weixin.qq.com/pay/micropay";
  22. if (config == null)
  23. {
  24. throw new WxPayException("微信支付,缺少WxPayConfig!");
  25. }
  26. //检测必填参数
  27. if (!inputObj.IsSet("body"))
  28. {
  29. throw new WxPayException("提交被扫支付API接口中,缺少必填参数body!");
  30. }
  31. else if (!inputObj.IsSet("out_trade_no"))
  32. {
  33. throw new WxPayException("提交被扫支付API接口中,缺少必填参数out_trade_no!");
  34. }
  35. else if (!inputObj.IsSet("total_fee"))
  36. {
  37. throw new WxPayException("提交被扫支付API接口中,缺少必填参数total_fee!");
  38. }
  39. else if (!inputObj.IsSet("auth_code"))
  40. {
  41. throw new WxPayException("提交被扫支付API接口中,缺少必填参数auth_code!");
  42. }
  43. inputObj.SetValue("spbill_create_ip", config.IP);//终端ip
  44. inputObj.SetValue("appid", config.APPID);//公众账号ID
  45. inputObj.SetValue("mch_id", config.MCHID);//商户号
  46. if (!string.IsNullOrEmpty(config.SUBAPPID) &&
  47. !string.IsNullOrEmpty(config.SUBMCHID))
  48. {
  49. inputObj.SetValue("sub_appid", config.SUBAPPID);//公众账号ID
  50. inputObj.SetValue("sub_mch_id", config.SUBMCHID);//商户号
  51. }
  52. inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));//随机字符串
  53. inputObj.SetValue("sign", inputObj.MakeSign(config));//签名
  54. string xml = inputObj.ToXml();
  55. var start = DateTime.Now;//请求开始时间
  56. Log.Debug("WxPayApi", "MicroPay request : " + xml);
  57. string response = await HttpService.PostAsync(xml, url, config, null, timeOut);//调用HTTP通信接口以提交数据到API
  58. Log.Debug("WxPayApi", "MicroPay response : " + response);
  59. var end = DateTime.Now;
  60. int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时
  61. //将xml格式的结果转换为对象以返回
  62. WxPayData result = new WxPayData();
  63. result.FromXml(config, response);
  64. ReportCostTime(url, timeCost, result, config);//测速上报
  65. return result;
  66. }
  67. /**
  68. *
  69. * 查询订单
  70. * @param WxPayData inputObj 提交给查询订单API的参数
  71. * @param int timeOut 超时时间
  72. * @throws WxPayException
  73. * @return 成功时返回订单查询结果,其他抛异常
  74. */
  75. public static async Task<WxPayData> OrderQuery(WxPayData inputObj, WxPayConfig config, bool miniProgramPay = false, int timeOut = 6)
  76. {
  77. string url = "https://api.mch.weixin.qq.com/pay/orderquery";
  78. //检测必填参数
  79. if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
  80. {
  81. throw new WxPayException("订单查询接口中,out_trade_no、transaction_id至少填一个!");
  82. }
  83. inputObj.SetValue("appid", config.APPID);//公众账号ID
  84. inputObj.SetValue("mch_id", config.MCHID);//商户号
  85. if (!string.IsNullOrEmpty(config.SUBAPPID) &&
  86. !string.IsNullOrEmpty(config.SUBMCHID))
  87. {
  88. if (!string.IsNullOrEmpty(config.MINI_PROGRAM_APPID) && miniProgramPay)
  89. {
  90. inputObj.SetValue("sub_appid", config.MINI_PROGRAM_APPID);//小程序AppID
  91. }
  92. else
  93. {
  94. inputObj.SetValue("sub_appid", config.SUBAPPID);//公众账号ID
  95. }
  96. inputObj.SetValue("sub_mch_id", config.SUBMCHID);//商户号
  97. }
  98. inputObj.SetValue("nonce_str", WxPayApi.GenerateNonceStr());//随机字符串
  99. inputObj.SetValue("sign", inputObj.MakeSign(config));//签名
  100. string xml = inputObj.ToXml();
  101. var start = DateTime.Now;
  102. Log.Info("WxPayApi", "OrderQuery request : " + xml);
  103. string response = await HttpService.PostAsync(xml, url, config, null, timeOut);//调用HTTP通信接口提交数据
  104. Log.Debug("WxPayApi", "OrderQuery response : " + response);
  105. var end = DateTime.Now;
  106. int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时
  107. //将xml格式的数据转化为对象以返回
  108. WxPayData result = new WxPayData();
  109. result.FromXml(config, response);
  110. ReportCostTime(url, timeCost, result, config);//测速上报
  111. return result;
  112. }
  113. /**
  114. *
  115. * 撤销订单API接口
  116. * @param WxPayData inputObj 提交给撤销订单API接口的参数,out_trade_no和transaction_id必填一个
  117. * @param int timeOut 接口超时时间
  118. * @throws WxPayException
  119. * @return 成功时返回API调用结果,其他抛异常
  120. */
  121. public static async Task<WxPayData> Reverse(WxPayData inputObj, WxPayConfig config, X509Certificate2 certification, int timeOut = 6)
  122. {
  123. string url = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
  124. //检测必填参数
  125. if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
  126. {
  127. throw new WxPayException("撤销订单API接口中,参数out_trade_no和transaction_id必须填写一个!");
  128. }
  129. inputObj.SetValue("appid", config.APPID);//公众账号ID
  130. inputObj.SetValue("mch_id", config.MCHID);//商户号
  131. if (!string.IsNullOrEmpty(config.SUBAPPID) &&
  132. !string.IsNullOrEmpty(config.SUBMCHID))
  133. {
  134. inputObj.SetValue("sub_appid", config.SUBAPPID);//公众账号ID
  135. inputObj.SetValue("sub_mch_id", config.SUBMCHID);//商户号
  136. }
  137. inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串
  138. inputObj.SetValue("sign", inputObj.MakeSign(config));//签名
  139. string xml = inputObj.ToXml();
  140. var start = DateTime.Now;//请求开始时间
  141. Log.Debug("WxPayApi", "Reverse request : " + xml);
  142. string response = await HttpService.PostAsync(xml, url, config, certification, timeOut);
  143. Log.Debug("WxPayApi", "Reverse response : " + response);
  144. var end = DateTime.Now;
  145. int timeCost = (int)((end - start).TotalMilliseconds);
  146. WxPayData result = new WxPayData();
  147. result.FromXml(config, response);
  148. ReportCostTime(url, timeCost, result, config);//测速上报
  149. return result;
  150. }
  151. /**
  152. *
  153. * 申请退款
  154. * @param WxPayData inputObj 提交给申请退款API的参数
  155. * @param int timeOut 超时时间
  156. * @throws WxPayException
  157. * @return 成功时返回接口调用结果,其他抛异常
  158. */
  159. public static async Task<WxPayData> Refund(WxPayData inputObj, WxPayConfig config,
  160. X509Certificate2 certification, int timeOut = 6)
  161. {
  162. string url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
  163. //检测必填参数
  164. if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
  165. {
  166. throw new WxPayException("退款申请接口中,out_trade_no、transaction_id至少填一个!");
  167. }
  168. else if (!inputObj.IsSet("out_refund_no"))
  169. {
  170. throw new WxPayException("退款申请接口中,缺少必填参数out_refund_no!");
  171. }
  172. else if (!inputObj.IsSet("total_fee"))
  173. {
  174. throw new WxPayException("退款申请接口中,缺少必填参数total_fee!");
  175. }
  176. else if (!inputObj.IsSet("refund_fee"))
  177. {
  178. throw new WxPayException("退款申请接口中,缺少必填参数refund_fee!");
  179. }
  180. else if (!inputObj.IsSet("op_user_id"))
  181. {
  182. throw new WxPayException("退款申请接口中,缺少必填参数op_user_id!");
  183. }
  184. inputObj.SetValue("appid", config.APPID);//公众账号ID
  185. inputObj.SetValue("mch_id", config.MCHID);//商户号
  186. if (!string.IsNullOrEmpty(config.SUBAPPID) &&
  187. !string.IsNullOrEmpty(config.SUBMCHID))
  188. {
  189. inputObj.SetValue("sub_appid", config.SUBAPPID);//公众账号ID
  190. inputObj.SetValue("sub_mch_id", config.SUBMCHID);//商户号
  191. }
  192. inputObj.SetValue("nonce_str", Guid.NewGuid().ToString().Replace("-", ""));//随机字符串
  193. inputObj.SetValue("sign", inputObj.MakeSign(config));//签名
  194. string xml = inputObj.ToXml();
  195. var start = DateTime.Now;
  196. Log.Debug("WxPayApi", "Refund request : " + xml);
  197. string response = await HttpService.PostAsync(xml, url, config, certification, timeOut);//调用HTTP通信接口提交数据到API
  198. Log.Debug("WxPayApi", "Refund response : " + response);
  199. var end = DateTime.Now;
  200. int timeCost = (int)((end - start).TotalMilliseconds);//获得接口耗时
  201. //将xml格式的结果转换为对象以返回
  202. WxPayData result = new WxPayData();
  203. result.FromXml(config, response);
  204. ReportCostTime(url, timeCost, result, config);//测速上报
  205. return result;
  206. }
  207. /**
  208. *
  209. * 查询退款
  210. * 提交退款申请后,通过该接口查询退款状态。退款有一定延时,
  211. * 用零钱支付的退款20分钟内到账,银行卡支付的退款3个工作日后重新查询退款状态。
  212. * out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个
  213. * @param WxPayData inputObj 提交给查询退款API的参数
  214. * @param int timeOut 接口超时时间
  215. * @throws WxPayException
  216. * @return 成功时返回,其他抛异常
  217. */
  218. public static async Task<WxPayData> RefundQuery(WxPayData inputObj, WxPayConfig config, int timeOut = 6)
  219. {
  220. string url = "https://api.mch.weixin.qq.com/pay/refundquery";
  221. //检测必填参数
  222. if(!inputObj.IsSet("out_refund_no") && !inputObj.IsSet("out_trade_no") &&
  223. !inputObj.IsSet("transaction_id") && !inputObj.IsSet("refund_id"))
  224. {
  225. throw new WxPayException("退款查询接口中,out_refund_no、out_trade_no、transaction_id、refund_id四个参数必填一个!");
  226. }
  227. inputObj.SetValue("appid", config.APPID);//公众账号ID
  228. inputObj.SetValue("mch_id", config.MCHID);//商户号
  229. if (!string.IsNullOrEmpty(config.SUBAPPID) &&
  230. !string.IsNullOrEmpty(config.SUBMCHID))
  231. {
  232. inputObj.SetValue("sub_appid", config.SUBAPPID);//公众账号ID
  233. inputObj.SetValue("sub_mch_id", config.SUBMCHID);//商户号
  234. }
  235. inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串
  236. inputObj.SetValue("sign",inputObj.MakeSign(config));//签名
  237. string xml = inputObj.ToXml();
  238. var start = DateTime.Now;//请求开始时间
  239. Log.Debug("WxPayApi", "RefundQuery request : " + xml);
  240. string response = await HttpService.PostAsync(xml, url, config, null, timeOut);//调用HTTP通信接口以提交数据到API
  241. Log.Debug("WxPayApi", "RefundQuery response : " + response);
  242. var end = DateTime.Now;
  243. int timeCost = (int)((end-start).TotalMilliseconds);//获得接口耗时
  244. //将xml格式的结果转换为对象以返回
  245. WxPayData result = new WxPayData();
  246. result.FromXml(config, response);
  247. ReportCostTime(url, timeCost, result, config);//测速上报
  248. return result;
  249. }
  250. /**
  251. * 下载对账单
  252. * @param WxPayData inputObj 提交给下载对账单API的参数
  253. * @param int timeOut 接口超时时间
  254. * @throws WxPayException
  255. * @return 成功时返回,其他抛异常
  256. */
  257. public static async Task<WxPayData> DownloadBill(WxPayData inputObj, WxPayConfig config, int timeOut = 6)
  258. {
  259. string url = "https://api.mch.weixin.qq.com/pay/downloadbill";
  260. //检测必填参数
  261. if (!inputObj.IsSet("bill_date"))
  262. {
  263. throw new WxPayException("对账单接口中,缺少必填参数bill_date!");
  264. }
  265. inputObj.SetValue("appid", config.APPID);//公众账号ID
  266. inputObj.SetValue("mch_id", config.MCHID);//商户号
  267. if (!string.IsNullOrEmpty(config.SUBAPPID) &&
  268. !string.IsNullOrEmpty(config.SUBMCHID))
  269. {
  270. inputObj.SetValue("sub_appid", config.SUBAPPID);//公众账号ID
  271. inputObj.SetValue("sub_mch_id", config.SUBMCHID);//商户号
  272. }
  273. inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串
  274. inputObj.SetValue("sign", inputObj.MakeSign(config));//签名
  275. string xml = inputObj.ToXml();
  276. Log.Debug("WxPayApi", "DownloadBill request : " + xml);
  277. string response = await HttpService.PostAsync(xml, url, config, null, timeOut);//调用HTTP通信接口以提交数据到API
  278. Log.Debug("WxPayApi", "DownloadBill result : " + response);
  279. WxPayData result = new WxPayData();
  280. //若接口调用失败会返回xml格式的结果
  281. if (response.Substring(0, 5) == "<xml>")
  282. {
  283. result.FromXml(config, response);
  284. }
  285. //接口调用成功则返回非xml格式的数据
  286. else
  287. result.SetValue("result", response);
  288. return result;
  289. }
  290. /**
  291. *
  292. * 转换短链接
  293. * 该接口主要用于扫码原生支付模式一中的二维码链接转成短链接(weixin://wxpay/s/XXXXXX),
  294. * 减小二维码数据量,提升扫描速度和精确度。
  295. * @param WxPayData inputObj 提交给转换短连接API的参数
  296. * @param int timeOut 接口超时时间
  297. * @throws WxPayException
  298. * @return 成功时返回,其他抛异常
  299. */
  300. public static async Task<WxPayData> ShortUrl(WxPayData inputObj, WxPayConfig config, int timeOut = 6)
  301. {
  302. string url = "https://api.mch.weixin.qq.com/tools/shorturl";
  303. //检测必填参数
  304. if(!inputObj.IsSet("long_url"))
  305. {
  306. throw new WxPayException("需要转换的URL,签名用原串,传输需URL encode!");
  307. }
  308. inputObj.SetValue("appid", config.APPID);//公众账号ID
  309. inputObj.SetValue("mch_id", config.MCHID);//商户号
  310. inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串
  311. inputObj.SetValue("sign",inputObj.MakeSign(config));//签名
  312. string xml = inputObj.ToXml();
  313. var start = DateTime.Now;//请求开始时间
  314. Log.Debug("WxPayApi", "ShortUrl request : " + xml);
  315. string response = await HttpService.PostAsync(xml, url, config, null, timeOut);
  316. Log.Debug("WxPayApi", "ShortUrl response : " + response);
  317. var end = DateTime.Now;
  318. int timeCost = (int)((end - start).TotalMilliseconds);
  319. WxPayData result = new WxPayData();
  320. result.FromXml(config, response);
  321. ReportCostTime(url, timeCost, result, config);//测速上报
  322. return result;
  323. }
  324. /**
  325. *
  326. * 统一下单
  327. * @param WxPaydata inputObj 提交给统一下单API的参数
  328. * @param int timeOut 超时时间
  329. * @throws WxPayException
  330. * @return 成功时返回,其他抛异常
  331. */
  332. public static async Task<WxPayData> UnifiedOrder(WxPayData inputObj, WxPayConfig config, int timeOut = 6)
  333. {
  334. string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  335. //检测必填参数
  336. if (!inputObj.IsSet("out_trade_no"))
  337. {
  338. throw new WxPayException("缺少统一支付接口必填参数out_trade_no!");
  339. }
  340. else if (!inputObj.IsSet("body"))
  341. {
  342. throw new WxPayException("缺少统一支付接口必填参数body!");
  343. }
  344. else if (!inputObj.IsSet("total_fee"))
  345. {
  346. throw new WxPayException("缺少统一支付接口必填参数total_fee!");
  347. }
  348. else if (!inputObj.IsSet("trade_type"))
  349. {
  350. throw new WxPayException("缺少统一支付接口必填参数trade_type!");
  351. }
  352. //关联参数
  353. if (inputObj.GetValue("trade_type").ToString() == "JSAPI" && !inputObj.IsSet("openid"))
  354. {
  355. throw new WxPayException("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
  356. }
  357. if (inputObj.GetValue("trade_type").ToString() == "NATIVE" && !inputObj.IsSet("product_id"))
  358. {
  359. throw new WxPayException("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
  360. }
  361. //异步通知url未设置,则使用配置文件中的url
  362. if (!inputObj.IsSet("notify_url"))
  363. {
  364. inputObj.SetValue("notify_url", config.NOTIFY_URL);//异步通知url
  365. }
  366. if (!inputObj.IsSet("openid")) // native pay
  367. {
  368. inputObj.SetValue("appid", config.APPID);//公众账号ID
  369. inputObj.SetValue("mch_id", config.MCHID);//商户号
  370. if (!string.IsNullOrEmpty(config.SUBAPPID) &&
  371. !string.IsNullOrEmpty(config.SUBMCHID))
  372. {
  373. inputObj.SetValue("sub_appid", config.SUBAPPID);//公众账号ID
  374. inputObj.SetValue("sub_mch_id", config.SUBMCHID);//商户号
  375. }
  376. }
  377. else // mini program pay
  378. {
  379. if (!string.IsNullOrEmpty(config.SUBAPPID) &&
  380. !string.IsNullOrEmpty(config.SUBMCHID))
  381. {
  382. var openId = inputObj.GetValue("openid").ToString();
  383. inputObj.SetValue("openid", null);
  384. inputObj.SetValue("sub_openid", openId);
  385. inputObj.SetValue("mch_id", config.MCHID);//商户号
  386. inputObj.SetValue("appid", config.APPID);//公众账号ID
  387. inputObj.SetValue("sub_appid", config.MINI_PROGRAM_APPID);//公众账号ID
  388. inputObj.SetValue("sub_mch_id", config.SUBMCHID);//商户号
  389. }
  390. else
  391. {
  392. inputObj.SetValue("mch_id", config.MCHID);//商户号
  393. inputObj.SetValue("appid", config.MINI_PROGRAM_APPID);//公众账号ID
  394. }
  395. }
  396. inputObj.SetValue("spbill_create_ip", config.IP);//终端ip
  397. inputObj.SetValue("nonce_str", GenerateNonceStr());//随机字符串
  398. //签名
  399. inputObj.SetValue("sign", inputObj.MakeSign(config));
  400. string xml = inputObj.ToXml();
  401. var start = DateTime.Now;
  402. Log.Debug("WxPayApi", "UnfiedOrder request : " + xml);
  403. string response = await HttpService.PostAsync(xml, url, config, null, timeOut);
  404. Log.Debug("WxPayApi", "UnfiedOrder response : " + response);
  405. var end = DateTime.Now;
  406. int timeCost = (int)((end - start).TotalMilliseconds);
  407. WxPayData result = new WxPayData();
  408. result.FromXml(config, response);
  409. ReportCostTime(url, timeCost, result, config);//测速上报
  410. return result;
  411. }
  412. /**
  413. *
  414. * 关闭订单
  415. * @param WxPayData inputObj 提交给关闭订单API的参数
  416. * @param int timeOut 接口超时时间
  417. * @throws WxPayException
  418. * @return 成功时返回,其他抛异常
  419. */
  420. public static async Task<WxPayData> CloseOrder(WxPayData inputObj, WxPayConfig config, int timeOut = 6)
  421. {
  422. string url = "https://api.mch.weixin.qq.com/pay/closeorder";
  423. //检测必填参数
  424. if(!inputObj.IsSet("out_trade_no"))
  425. {
  426. throw new WxPayException("关闭订单接口中,out_trade_no必填!");
  427. }
  428. inputObj.SetValue("appid", config.APPID);//公众账号ID
  429. inputObj.SetValue("mch_id", config.MCHID);//商户号
  430. if (!string.IsNullOrEmpty(config.SUBAPPID) &&
  431. !string.IsNullOrEmpty(config.SUBMCHID))
  432. {
  433. inputObj.SetValue("sub_appid", config.SUBAPPID);//公众账号ID
  434. inputObj.SetValue("sub_mch_id", config.SUBMCHID);//商户号
  435. }
  436. inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串
  437. inputObj.SetValue("sign",inputObj.MakeSign(config));//签名
  438. string xml = inputObj.ToXml();
  439. var start = DateTime.Now;//请求开始时间
  440. string response = await HttpService.PostAsync(xml, url, config, null, timeOut);
  441. var end = DateTime.Now;
  442. int timeCost = (int)((end - start).TotalMilliseconds);
  443. WxPayData result = new WxPayData();
  444. result.FromXml(config, response);
  445. ReportCostTime(url, timeCost, result, config);//测速上报
  446. return result;
  447. }
  448. /**
  449. *
  450. * 测速上报
  451. * @param string interface_url 接口URL
  452. * @param int timeCost 接口耗时
  453. * @param WxPayData inputObj参数数组
  454. */
  455. private static void ReportCostTime(string interface_url, int timeCost, WxPayData inputObj, WxPayConfig config)
  456. {
  457. //如果不需要进行上报
  458. if(config.REPORT_LEVENL == 0)
  459. {
  460. return;
  461. }
  462. //如果仅失败上报
  463. if(config.REPORT_LEVENL == 1 && inputObj.IsSet("return_code") && inputObj.GetValue("return_code").ToString() == "SUCCESS" &&
  464. inputObj.IsSet("result_code") && inputObj.GetValue("result_code").ToString() == "SUCCESS")
  465. {
  466. return;
  467. }
  468. //上报逻辑
  469. WxPayData data = new WxPayData();
  470. data.SetValue("interface_url",interface_url);
  471. data.SetValue("execute_time_",timeCost);
  472. //返回状态码
  473. if(inputObj.IsSet("return_code"))
  474. {
  475. data.SetValue("return_code",inputObj.GetValue("return_code"));
  476. }
  477. //返回信息
  478. if(inputObj.IsSet("return_msg"))
  479. {
  480. data.SetValue("return_msg",inputObj.GetValue("return_msg"));
  481. }
  482. //业务结果
  483. if(inputObj.IsSet("result_code"))
  484. {
  485. data.SetValue("result_code",inputObj.GetValue("result_code"));
  486. }
  487. //错误代码
  488. if(inputObj.IsSet("err_code"))
  489. {
  490. data.SetValue("err_code",inputObj.GetValue("err_code"));
  491. }
  492. //错误代码描述
  493. if(inputObj.IsSet("err_code_des"))
  494. {
  495. data.SetValue("err_code_des",inputObj.GetValue("err_code_des"));
  496. }
  497. //商户订单号
  498. if(inputObj.IsSet("out_trade_no"))
  499. {
  500. data.SetValue("out_trade_no",inputObj.GetValue("out_trade_no"));
  501. }
  502. //设备号
  503. if(inputObj.IsSet("device_info"))
  504. {
  505. data.SetValue("device_info",inputObj.GetValue("device_info"));
  506. }
  507. try
  508. {
  509. Report(data, config);
  510. }
  511. catch (WxPayException ex)
  512. {
  513. //不做任何处理
  514. }
  515. }
  516. /**
  517. *
  518. * 测速上报接口实现
  519. * @param WxPayData inputObj 提交给测速上报接口的参数
  520. * @param int timeOut 测速上报接口超时时间
  521. * @throws WxPayException
  522. * @return 成功时返回测速上报接口返回的结果,其他抛异常
  523. */
  524. public static async Task<WxPayData> Report(WxPayData inputObj, WxPayConfig config,int timeOut = 1)
  525. {
  526. string url = "https://api.mch.weixin.qq.com/payitil/report";
  527. //检测必填参数
  528. if(!inputObj.IsSet("interface_url"))
  529. {
  530. throw new WxPayException("接口URL,缺少必填参数interface_url!");
  531. }
  532. if(!inputObj.IsSet("return_code"))
  533. {
  534. throw new WxPayException("返回状态码,缺少必填参数return_code!");
  535. }
  536. if(!inputObj.IsSet("result_code"))
  537. {
  538. throw new WxPayException("业务结果,缺少必填参数result_code!");
  539. }
  540. if(!inputObj.IsSet("user_ip"))
  541. {
  542. throw new WxPayException("访问接口IP,缺少必填参数user_ip!");
  543. }
  544. if(!inputObj.IsSet("execute_time_"))
  545. {
  546. throw new WxPayException("接口耗时,缺少必填参数execute_time_!");
  547. }
  548. inputObj.SetValue("appid",config.APPID);//公众账号ID
  549. inputObj.SetValue("mch_id",config.MCHID);//商户号
  550. if (!string.IsNullOrEmpty(config.SUBAPPID) &&
  551. !string.IsNullOrEmpty(config.SUBMCHID))
  552. {
  553. inputObj.SetValue("sub_appid", config.SUBAPPID);//公众账号ID
  554. inputObj.SetValue("sub_mch_id", config.SUBMCHID);//商户号
  555. }
  556. inputObj.SetValue("user_ip",config.IP);//终端ip
  557. inputObj.SetValue("time",DateTime.Now.ToString("yyyyMMddHHmmss"));//商户上报时间
  558. inputObj.SetValue("nonce_str",GenerateNonceStr());//随机字符串
  559. inputObj.SetValue("sign",inputObj.MakeSign(config));//签名
  560. string xml = inputObj.ToXml();
  561. Log.Info("WxPayApi", "Report request : " + xml);
  562. string response = await HttpService.PostAsync(xml, url, config, null, timeOut);
  563. Log.Info("WxPayApi", "Report response : " + response);
  564. WxPayData result = new WxPayData();
  565. result.FromXml(config, response);
  566. return result;
  567. }
  568. /**
  569. * 根据当前系统时间加随机序列来生成订单号
  570. * @return 订单号
  571. */
  572. public static string GenerateOutTradeNo(WxPayConfig config)
  573. {
  574. var ran = new Random();
  575. return string.Format("{0}{1}{2}", config.MCHID, DateTime.Now.ToString("yyyyMMddHHmmss"), ran.Next(999));
  576. }
  577. /**
  578. * 生成时间戳,标准北京时间,时区为东八区,自1970年1月1日 0点0分0秒以来的秒数
  579. * @return 时间戳
  580. */
  581. public static string GenerateTimeStamp()
  582. {
  583. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  584. return Convert.ToInt64(ts.TotalSeconds).ToString();
  585. }
  586. /**
  587. * 生成随机串,随机串包含字母或数字
  588. * @return 随机串
  589. */
  590. public static string GenerateNonceStr()
  591. {
  592. return Guid.NewGuid().ToString().Replace("-", "");
  593. }
  594. internal static async Task<WxPayData> DownloadBill(WxPayData data, WxPayConfig config)
  595. {
  596. throw new NotImplementedException();
  597. }
  598. }
  599. }