WeixinController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using EMIS.CommonLogic.SystemServices;
  10. using EMIS.Utility;
  11. using EMIS.ViewModel.WechatModel;
  12. using EMIS.Utility.OnlinePay;
  13. using Senparc.Weixin;
  14. using Senparc.Weixin.TenPay;
  15. using Senparc.Weixin.TenPay.V3;
  16. using Senparc.Weixin.MP.Entities.Request;
  17. //using Senparc.Weixin.Exceptions;
  18. namespace EMIS.Web.Controllers
  19. {
  20. [Authorization]
  21. public class WeixinController : Controller
  22. {
  23. public IWechatPayServices WechatPayServices { get; set; }
  24. [AllowAnonymous]
  25. public ActionResult Index()
  26. {
  27. string url = new Uri(this.Request.Url, "../" + ConfigurationManager.AppSettings["MobileAppPath"]).AbsoluteUri + "?v=" + DateTime.Now.Ticks.ToString();
  28. return Redirect(WechatHelper.GetBaseAuthorizeUrl(url));
  29. }
  30. #region 支付
  31. public ActionResult NativePay(Guid examinationRegistrationID, decimal fee, string feeTypeName)
  32. {
  33. var nativePayView = new NativePayView();
  34. nativePayView.ExaminationRegistrationID = examinationRegistrationID;
  35. nativePayView.Fee = fee;
  36. nativePayView.FeeTypeName = feeTypeName;
  37. var lastOrder = this.WechatPayServices.GetLastOrder(examinationRegistrationID);
  38. if (lastOrder != null && lastOrder.OrderID != null)
  39. {
  40. this.WechatPayServices.UpdateWeChatPay(new List<OrderQueryResult> { WechatHelper.OrderQuery(lastOrder.OrderID) });
  41. }
  42. lastOrder = this.WechatPayServices.GetLastOrder(examinationRegistrationID);
  43. if (lastOrder != null && lastOrder.OrderID != null)
  44. {
  45. nativePayView.ProductID = lastOrder.OrderID;
  46. }
  47. else
  48. {
  49. nativePayView.ProductID = WechatPayServices.GetProductId();
  50. }
  51. return View(nativePayView);
  52. }
  53. public ActionResult PayQRCode(Guid examinationRegistrationID, string productId, decimal fee, string feeTypeName)
  54. {
  55. var lastOrder = this.WechatPayServices.GetLastOrder(examinationRegistrationID);
  56. if (lastOrder != null && lastOrder.OrderID != null)
  57. {
  58. try
  59. {
  60. if (lastOrder.PayUrl != null)
  61. {
  62. var memoryStream = new MemoryStream();
  63. var qrcodeImage = QRCodeHelper.GenerateQRCode(lastOrder.PayUrl);
  64. qrcodeImage.Save(memoryStream, ImageFormat.Png);
  65. return new FileContentResult(memoryStream.GetBuffer(), "image/png");
  66. }
  67. else
  68. {
  69. WechatHelper.CloseOrder(lastOrder.OrderID);
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. return new FileContentResult(new byte[0], "image/png");
  75. }
  76. }
  77. WechatPayServices.NewPayList(examinationRegistrationID, productId);
  78. try
  79. {
  80. string payUrl;
  81. var content = WechatHelper.GetPayQRCode(productId, fee, feeTypeName, out payUrl);
  82. WechatPayServices.SaveUrl(examinationRegistrationID, payUrl);
  83. return content;
  84. }
  85. catch (Exception ex)
  86. {
  87. return new FileContentResult(new byte[0], "image/png");
  88. }
  89. }
  90. public ActionResult PayNotify()
  91. {
  92. try
  93. {
  94. var payNotifyView = WechatHelper.PayNotify();
  95. try
  96. {
  97. //向微信发送请求,根据返回结果修改报名缴费状态
  98. OrderQueryResult query = WechatHelper.OrderQuery(payNotifyView.OrderID);
  99. List<OrderQueryResult> queryList = new List<OrderQueryResult>();
  100. queryList.Add(query);
  101. WechatPayServices.UpdateWeChatPay(queryList);
  102. //WechatPayServices.UpdateTransactionID(payNotifyView.OrderID, payNotifyView.WechatOrderID);
  103. //给财付通系统发送成功信息,财付通系统收到此结果后不再进行后续通知
  104. return WechatHelper.PayNotifySuccess;
  105. }
  106. catch (Exception ex)
  107. {
  108. return WechatHelper.PayNotifyFail("支付失败,商户内部数据库更新出错。");
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. return WechatHelper.PayNotifyFail(ex.Message);
  114. }
  115. }
  116. [HttpPost]
  117. public ActionResult CheckOrderScanned(string productId)
  118. {
  119. try
  120. {
  121. var order = WechatHelper.OrderQuery(productId);
  122. string[] payingStatus = new string[] { "NOTPAY" };
  123. if (!payingStatus.Contains(order.trade_state))
  124. {
  125. return Json(true);
  126. }
  127. else
  128. {
  129. return Json(false);
  130. }
  131. }
  132. catch
  133. {
  134. return Json(false);
  135. }
  136. }
  137. #endregion
  138. #region 退款
  139. public ActionResult RefundNotify()
  140. {
  141. try
  142. {
  143. var refundNotifyView = WechatHelper.RefundNotify();
  144. WechatPayServices.UpdateRefund(refundNotifyView.OrderID, refundNotifyView.WechatRefundOrderID, refundNotifyView.Fee);
  145. //给财付通系统发送成功信息,财付通系统收到此结果后不再进行后续通知
  146. return WechatHelper.PayNotifySuccess;
  147. }
  148. catch (Exception ex)
  149. {
  150. return WechatHelper.PayNotifyFail("支付失败,商户内部数据库更新出错。");
  151. }
  152. }
  153. #endregion
  154. }
  155. }