WxPayApi.cs 25 KB

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