123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- using System.Web.Mvc;
- using Senparc.Weixin.MP.Entities.Request;
- using EMIS.Utility.OnlinePay;
- using System;
- using EMIS.Utility;
- using EMIS.CommonLogic.SystemServices;
- using EMIS.CommonLogic.ExaminationApply;
- using EMIS.ViewModel;
- using EMIS.ViewModel.WechatModel;
- using Senparc.Weixin.TenPay.V3;
- using System.Collections.Generic;
- using Bowin.Common.Log;
- using System.Threading;
- using Bowin.Common.JSON;
- using Senparc.Weixin.Exceptions;
- namespace EMIS.Web.ServiceControllers.WeChat
- {
- public class WxMpServicesController : Controller
- {
- public IWechatMPServices WechatMPServices { get; set; }
- public IWechatPayServices WechatPayServices { get; set; }
- public IExaminationSubjectServices ExaminationSubjectServices { get; set; }
- [HttpGet]
- public ActionResult EventHandler(PostModel postModel, string echostr)
- {
- return WechatHelper.EventHandler(postModel, echostr);
- }
- [HttpPost]
- public ActionResult PostEventHandler(PostModel postModel)
- {
- return WechatHelper.PostEventHandler(postModel);
- }
- [HttpPost]
- public ActionResult GetJSConfigData(string url)
- {
- try
- {
- return Json(WechatHelper.GetJSConfigData(url));
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- [HttpPost]
- public ActionResult GetJSPayInfo(Guid examinationRegistrationID, string openID)
- {
- var examinationRegistration = ExaminationSubjectServices.GetStudentListView(examinationRegistrationID);
- if (examinationRegistration == null)
- {
- return Json(new ReturnMessage()
- {
- IsSuccess = false,
- Message = "支付失败,找不到对应的报名信息。"
- });
- }
- try
- {
- if (!string.IsNullOrEmpty(examinationRegistration.PayForm))
- {
- return Json(new ReturnMessage<JSPayView>
- {
- IsSuccess = true,
- Data = examinationRegistration.PayForm.JsonToObject<JSPayView>()
- });
- }
- else
- {
- var orderID = WechatPayServices.GetProductId();
- WechatPayServices.NewPayList(examinationRegistrationID, orderID);
- var jsPayView = WechatHelper.JsPay(orderID, examinationRegistration.PayFee ?? 0, examinationRegistration.FeeTypeName, openID);
- //var jsPayView = WechatHelper.JsPay(orderID, (decimal)0.01, examinationRegistration.FeeTypeName, openID);
- return Json(new ReturnMessage<JSPayView>
- {
- IsSuccess = true,
- Data = jsPayView
- });
- }
- }
- catch (Exception ex)
- {
- return Json(new ReturnMessage()
- {
- IsSuccess = false,
- Message = "支付失败," + ex.Message
- });
- }
- }
- [HttpPost]
- public ActionResult PayFail(Guid examinationRegistrationID, string payForm)
- {
- try
- {
- WechatPayServices.PayFail(examinationRegistrationID, payForm);
- return Json(new ReturnMessage { IsSuccess = true });
- }
- catch (Exception ex)
- {
- return Json(new ReturnMessage { IsSuccess = false, Message = ex.Message });
- }
- }
- [HttpPost]
- public ActionResult PayConfirm(Guid examinationRegistrationID)
- {
- try
- {
- var examinationRegistration = ExaminationSubjectServices.GetStudentListView(examinationRegistrationID);
- if (examinationRegistration == null)
- {
- return Json(new ReturnMessage()
- {
- IsSuccess = false,
- Message = "支付失败,找不到对应的报名信息,请联系系统管理员。"
- });
- }
- //试试延时一下会不会准些……
- Thread.Sleep(1000);
- //向微信发送请求,根据返回结果修改报名缴费状态
- OrderQueryResult query = WechatHelper.OrderQuery(examinationRegistration.OrderID);
- List<OrderQueryResult> queryList = new List<OrderQueryResult>();
- queryList.Add(query);
- WechatPayServices.UpdateWeChatPay(queryList);
- LogHelper.WriteDebugLog(LogType.CommmLog, "SUCESS");
- return Json(new ReturnMessage
- {
- IsSuccess = true
- });
- }
- catch (Exception ex)
- {
- //LogHelper.WriteDebugLog(LogType.CommmLog, ex.Message + "\n" + ex.StackTrace);
- return Json(new ReturnMessage
- {
- IsSuccess = false,
- Message = ex.Message
- });
- }
- }
- [HttpPost]
- public ActionResult UploadBase64Img(string base64Image)
- {
- string filename = "";
- string base64 = base64Image.Substring(base64Image.IndexOf(",") + 1);
- try
- {
- var logger = NLog.LogManager.GetCurrentClassLogger();
- logger.Debug("开始上传图片");
- string inputStr = base64;
- byte[] arr = Convert.FromBase64String(inputStr);
- System.IO.MemoryStream ms = new System.IO.MemoryStream(arr);
- ms.Close();
- filename = FileUploadHelper.UploadFile(ms, "png");
- }
- catch (Exception ex)
- {
- var logger = NLog.LogManager.GetCurrentClassLogger();
- logger.Debug(ex.Message + ex.StackTrace);
- }
- return Json(filename);
- }
- [HttpPost]
- public ActionResult UploadMediaID(string mediaID)
- {
- try
- {
- var logger = NLog.LogManager.GetCurrentClassLogger();
- logger.Debug("开始下载图片" + mediaID);
- var downloadMedia = WechatHelper.DownloadMedia(mediaID);
- logger.Debug("返回结果" + downloadMedia);
- return Json(new ReturnMessage
- {
- IsSuccess = true,
- Message = downloadMedia
- });
- }
- catch (Exception ex)
- {
- var logger = NLog.LogManager.GetCurrentClassLogger();
- logger.Debug(ex.Message + ex.StackTrace);
- return Json(new ReturnMessage
- {
- IsSuccess = false,
- Message = ex.Message
- });
- }
- }
- [HttpPost]
- public ActionResult GetOpenID(string code)
- {
- try
- {
- return Json(new ReturnMessage<string>()
- {
- IsSuccess = true,
- Data = WechatHelper.GetOpenID(code)
- });
- }
- catch (ErrorJsonResultException ex)
- {
- return Json(new ReturnMessage<int>()
- {
- IsSuccess = false,
- ErrorCode = ex.JsonResult.ErrorCodeValue,
- Message = ex.Message
- });
- }
- catch (Exception ex)
- {
- return Json(new ReturnMessage
- {
- IsSuccess = false,
- Message = ex.Message
- });
- }
- }
- [HttpPost]
- public ActionResult GetUserInfoByOpenID(string openID)
- {
- return Json(WechatMPServices.GetUserViewByOpenID(openID));
- }
- [HttpPost]
- public ActionResult UnbindOpenID(string openID)
- {
- try
- {
- WechatMPServices.UnBindOpenID(openID);
- return Json(new ReturnMessage()
- {
- IsSuccess = true,
- Message = "解除绑定成功"
- });
- }
- catch (Exception ex)
- {
- return Json(new ReturnMessage()
- {
- IsSuccess = false,
- Message = "解除绑定失败," + ex.Message
- });
- }
- }
- //public ActionResult UpdateWeChatPayListStatus()
- //{
- // try
- // {
- // var wecharPayList = WechatPayServices.SearchWeChatPayList();
- // List<OrderQueryResult> orderList = new List<OrderQueryResult>();
- // foreach (var wecharPay in wecharPayList)
- // {
- // OrderQueryResult query = WechatHelper.OrderQuery(wecharPay.OrderID);
- // orderList.Add(query);
- // }
- // WechatPayServices.UpdateWeChatPay(orderList);
- // return Json(new ReturnMessage()
- // {
- // IsSuccess = true,
- // Message = "更新成功"
- // });
- // }
- // catch (Exception ex)
- // {
- // return Json(new ReturnMessage()
- // {
- // IsSuccess = true,
- // Message = "更新失败:" + ex.Message
- // });
- // }
- //}
- }
- }
|