WxMpServicesController.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System.Web.Mvc;
  2. using Senparc.Weixin.MP.Entities.Request;
  3. using EMIS.Utility.OnlinePay;
  4. using System;
  5. using EMIS.Utility;
  6. using EMIS.CommonLogic.SystemServices;
  7. using EMIS.CommonLogic.ExaminationApply;
  8. using EMIS.ViewModel;
  9. using EMIS.ViewModel.WechatModel;
  10. using Senparc.Weixin.TenPay.V3;
  11. using System.Collections.Generic;
  12. using Bowin.Common.Log;
  13. using System.Threading;
  14. using Bowin.Common.JSON;
  15. using Senparc.Weixin.Exceptions;
  16. namespace EMIS.Web.ServiceControllers.WeChat
  17. {
  18. public class WxMpServicesController : Controller
  19. {
  20. public IWechatMPServices WechatMPServices { get; set; }
  21. public IWechatPayServices WechatPayServices { get; set; }
  22. public IExaminationSubjectServices ExaminationSubjectServices { get; set; }
  23. [HttpGet]
  24. public ActionResult EventHandler(PostModel postModel, string echostr)
  25. {
  26. return WechatHelper.EventHandler(postModel, echostr);
  27. }
  28. [HttpPost]
  29. public ActionResult PostEventHandler(PostModel postModel)
  30. {
  31. return WechatHelper.PostEventHandler(postModel);
  32. }
  33. [HttpPost]
  34. public ActionResult GetJSConfigData(string url)
  35. {
  36. try
  37. {
  38. return Json(WechatHelper.GetJSConfigData(url));
  39. }
  40. catch (Exception ex)
  41. {
  42. throw ex;
  43. }
  44. }
  45. [HttpPost]
  46. public ActionResult GetJSPayInfo(Guid examinationRegistrationID, string openID)
  47. {
  48. var examinationRegistration = ExaminationSubjectServices.GetStudentListView(examinationRegistrationID);
  49. if (examinationRegistration == null)
  50. {
  51. return Json(new ReturnMessage()
  52. {
  53. IsSuccess = false,
  54. Message = "支付失败,找不到对应的报名信息。"
  55. });
  56. }
  57. try
  58. {
  59. if (!string.IsNullOrEmpty(examinationRegistration.PayForm))
  60. {
  61. return Json(new ReturnMessage<JSPayView>
  62. {
  63. IsSuccess = true,
  64. Data = examinationRegistration.PayForm.JsonToObject<JSPayView>()
  65. });
  66. }
  67. else
  68. {
  69. var orderID = WechatPayServices.GetProductId();
  70. WechatPayServices.NewPayList(examinationRegistrationID, orderID);
  71. var jsPayView = WechatHelper.JsPay(orderID, examinationRegistration.PayFee ?? 0, examinationRegistration.FeeTypeName, openID);
  72. //var jsPayView = WechatHelper.JsPay(orderID, (decimal)0.01, examinationRegistration.FeeTypeName, openID);
  73. return Json(new ReturnMessage<JSPayView>
  74. {
  75. IsSuccess = true,
  76. Data = jsPayView
  77. });
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. return Json(new ReturnMessage()
  83. {
  84. IsSuccess = false,
  85. Message = "支付失败," + ex.Message
  86. });
  87. }
  88. }
  89. [HttpPost]
  90. public ActionResult PayFail(Guid examinationRegistrationID, string payForm)
  91. {
  92. try
  93. {
  94. WechatPayServices.PayFail(examinationRegistrationID, payForm);
  95. return Json(new ReturnMessage { IsSuccess = true });
  96. }
  97. catch (Exception ex)
  98. {
  99. return Json(new ReturnMessage { IsSuccess = false, Message = ex.Message });
  100. }
  101. }
  102. [HttpPost]
  103. public ActionResult PayConfirm(Guid examinationRegistrationID)
  104. {
  105. try
  106. {
  107. var examinationRegistration = ExaminationSubjectServices.GetStudentListView(examinationRegistrationID);
  108. if (examinationRegistration == null)
  109. {
  110. return Json(new ReturnMessage()
  111. {
  112. IsSuccess = false,
  113. Message = "支付失败,找不到对应的报名信息,请联系系统管理员。"
  114. });
  115. }
  116. //试试延时一下会不会准些……
  117. Thread.Sleep(1000);
  118. //向微信发送请求,根据返回结果修改报名缴费状态
  119. OrderQueryResult query = WechatHelper.OrderQuery(examinationRegistration.OrderID);
  120. List<OrderQueryResult> queryList = new List<OrderQueryResult>();
  121. queryList.Add(query);
  122. WechatPayServices.UpdateWeChatPay(queryList);
  123. LogHelper.WriteDebugLog(LogType.CommmLog, "SUCESS");
  124. return Json(new ReturnMessage
  125. {
  126. IsSuccess = true
  127. });
  128. }
  129. catch (Exception ex)
  130. {
  131. //LogHelper.WriteDebugLog(LogType.CommmLog, ex.Message + "\n" + ex.StackTrace);
  132. return Json(new ReturnMessage
  133. {
  134. IsSuccess = false,
  135. Message = ex.Message
  136. });
  137. }
  138. }
  139. [HttpPost]
  140. public ActionResult UploadBase64Img(string base64Image)
  141. {
  142. string filename = "";
  143.             string base64 = base64Image.Substring(base64Image.IndexOf(",") + 1);
  144. try
  145.             {
  146. var logger = NLog.LogManager.GetCurrentClassLogger();
  147. logger.Debug("开始上传图片");
  148. string inputStr = base64;
  149.                 byte[] arr = Convert.FromBase64String(inputStr);
  150.                 System.IO.MemoryStream ms = new System.IO.MemoryStream(arr);
  151.                 ms.Close();
  152. filename = FileUploadHelper.UploadFile(ms, "png");
  153.             }
  154. catch (Exception ex)
  155. {
  156. var logger = NLog.LogManager.GetCurrentClassLogger();
  157. logger.Debug(ex.Message + ex.StackTrace);
  158. }
  159. return Json(filename);
  160. }
  161. [HttpPost]
  162. public ActionResult UploadMediaID(string mediaID)
  163. {
  164. try
  165. {
  166. var logger = NLog.LogManager.GetCurrentClassLogger();
  167. logger.Debug("开始下载图片" + mediaID);
  168. var downloadMedia = WechatHelper.DownloadMedia(mediaID);
  169. logger.Debug("返回结果" + downloadMedia);
  170. return Json(new ReturnMessage
  171. {
  172. IsSuccess = true,
  173. Message = downloadMedia
  174. });
  175. }
  176. catch (Exception ex)
  177. {
  178. var logger = NLog.LogManager.GetCurrentClassLogger();
  179. logger.Debug(ex.Message + ex.StackTrace);
  180. return Json(new ReturnMessage
  181. {
  182. IsSuccess = false,
  183. Message = ex.Message
  184. });
  185. }
  186. }
  187. [HttpPost]
  188. public ActionResult GetOpenID(string code)
  189. {
  190. try
  191. {
  192. return Json(new ReturnMessage<string>()
  193. {
  194. IsSuccess = true,
  195. Data = WechatHelper.GetOpenID(code)
  196. });
  197. }
  198. catch (ErrorJsonResultException ex)
  199. {
  200. return Json(new ReturnMessage<int>()
  201. {
  202. IsSuccess = false,
  203. ErrorCode = ex.JsonResult.ErrorCodeValue,
  204. Message = ex.Message
  205. });
  206. }
  207. catch (Exception ex)
  208. {
  209. return Json(new ReturnMessage
  210. {
  211. IsSuccess = false,
  212. Message = ex.Message
  213. });
  214. }
  215. }
  216. [HttpPost]
  217. public ActionResult GetUserInfoByOpenID(string openID)
  218. {
  219. return Json(WechatMPServices.GetUserViewByOpenID(openID));
  220. }
  221. [HttpPost]
  222. public ActionResult UnbindOpenID(string openID)
  223. {
  224. try
  225. {
  226. WechatMPServices.UnBindOpenID(openID);
  227. return Json(new ReturnMessage()
  228. {
  229. IsSuccess = true,
  230. Message = "解除绑定成功"
  231. });
  232. }
  233. catch (Exception ex)
  234. {
  235. return Json(new ReturnMessage()
  236. {
  237. IsSuccess = false,
  238. Message = "解除绑定失败," + ex.Message
  239. });
  240. }
  241. }
  242. //public ActionResult UpdateWeChatPayListStatus()
  243. //{
  244. // try
  245. // {
  246. // var wecharPayList = WechatPayServices.SearchWeChatPayList();
  247. // List<OrderQueryResult> orderList = new List<OrderQueryResult>();
  248. // foreach (var wecharPay in wecharPayList)
  249. // {
  250. // OrderQueryResult query = WechatHelper.OrderQuery(wecharPay.OrderID);
  251. // orderList.Add(query);
  252. // }
  253. // WechatPayServices.UpdateWeChatPay(orderList);
  254. // return Json(new ReturnMessage()
  255. // {
  256. // IsSuccess = true,
  257. // Message = "更新成功"
  258. // });
  259. // }
  260. // catch (Exception ex)
  261. // {
  262. // return Json(new ReturnMessage()
  263. // {
  264. // IsSuccess = true,
  265. // Message = "更新失败:" + ex.Message
  266. // });
  267. // }
  268. //}
  269. }
  270. }