AlipayController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using EMIS.CommonLogic.SystemServices;
  2. using EMIS.Utility;
  3. using EMIS.ViewModel.AlipayModel;
  4. using EMIS.Utility.OnlinePay;
  5. using EMIS.Utility.OnlinePay.Alipay;
  6. using EMIS.Utility.OnlinePay.Alipay.Models;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.Specialized;
  10. using System.Linq;
  11. using System.Web;
  12. using System.Web.Mvc;
  13. namespace EMIS.Web.Controllers
  14. {
  15. [Authorization]
  16. public class AlipayController : Controller
  17. {
  18. public IWechatPayServices WechatPayServices { get; set; }
  19. public IAlipayServices AlipayServices { get; set; }
  20. public ActionResult NativePay(Guid examinationRegistrationID, decimal fee, string feeTypeName)
  21. {
  22. var nativePayView = new NativePayView();
  23. nativePayView.ExaminationRegistrationID = examinationRegistrationID;
  24. nativePayView.Fee = fee;
  25. nativePayView.FeeTypeName = feeTypeName;
  26. nativePayView.ProductID = AlipayServices.GetProductId();
  27. return View(nativePayView);
  28. }
  29. public ActionResult PayQRCode(Guid examinationRegistrationID, string productId, decimal fee, string feeTypeName)
  30. {
  31. var lastOrder = this.WechatPayServices.GetLastOrder(examinationRegistrationID);
  32. if (lastOrder != null || lastOrder.OrderID != null)
  33. {
  34. try
  35. {
  36. AlipayHelper.CloseOrder(lastOrder.OrderID);
  37. }
  38. catch (Exception ex)
  39. {
  40. }
  41. }
  42. WechatPayServices.NewPayList(examinationRegistrationID, productId);
  43. try
  44. {
  45. return AlipayHelper.GetPayQRCode(productId, fee, feeTypeName);
  46. }
  47. catch (Exception ex)
  48. {
  49. return new FileContentResult(new byte[0], "image/png");
  50. }
  51. }
  52. /// <summary>
  53. /// 对支付宝异步通知的关键参数进行校验
  54. /// </summary>
  55. /// <returns></returns>
  56. private bool CheckParams()
  57. {
  58. bool ret = true;
  59. string app_id = Request.Form["app_id"];
  60. if (app_id != Config.appId)
  61. {
  62. ret = false;
  63. }
  64. return ret;
  65. }
  66. public ActionResult PayNotify()
  67. {
  68. try
  69. {
  70. var logger = NLog.LogManager.GetCurrentClassLogger();
  71. logger.Debug("支付宝:返回订单通知。");
  72. SortedDictionary<string, string> sPara = HttpHelper.GetRequestPost();
  73. if (sPara.Count > 0)//判断是否有带返回参数
  74. {
  75. Notify aliNotify = new Notify(Config.charset, Config.sign_type, Config.pid, Config.mapiUrl, Config.alipay_public_key);
  76. bool verifyResult = aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"]);
  77. if (verifyResult && CheckParams())
  78. {
  79. string out_trade_no = Request.Form["out_trade_no"];
  80. string trade_no = Request.Form["trade_no"];
  81. //交易状态
  82. //在支付宝的业务通知中,只有交易通知状态为TRADE_SUCCESS或TRADE_FINISHED时,才是买家付款成功。
  83. string trade_status = Request.Form["trade_status"];
  84. if (trade_status == "TRADE_SUCCESS" || trade_status == "TRADE_FINISHED")
  85. {
  86. WechatPayServices.UpdateTransactionID(out_trade_no, trade_no);
  87. }
  88. return Content("success");
  89. }
  90. else//验证失败
  91. {
  92. return Content("fail");
  93. }
  94. }
  95. else
  96. {
  97. return Content("无通知参数");
  98. }
  99. }
  100. catch (Exception ex)
  101. {
  102. var logger = NLog.LogManager.GetCurrentClassLogger();
  103. logger.Debug(ex.Message + ex.StackTrace);
  104. return Content("fail");
  105. }
  106. }
  107. [HttpPost]
  108. public ActionResult CheckOrderScanned(string productId)
  109. {
  110. try
  111. {
  112. var order = AlipayHelper.OrderQuery(productId);
  113. return Json(AlipayHelper.IsTradeFinished(order.TradeStatus));
  114. }
  115. catch
  116. {
  117. return Json(false);
  118. }
  119. }
  120. }
  121. }