using System; using System.Collections.Generic; using System.Configuration; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; using EMIS.CommonLogic.SystemServices; using EMIS.Utility; using EMIS.ViewModel.WechatModel; using EMIS.Utility.OnlinePay; using Senparc.Weixin; using Senparc.Weixin.TenPay; using Senparc.Weixin.TenPay.V3; using Senparc.Weixin.MP.Entities.Request; //using Senparc.Weixin.Exceptions; namespace EMIS.Web.Controllers { [Authorization] public class WeixinController : Controller { public IWechatPayServices WechatPayServices { get; set; } [AllowAnonymous] public ActionResult Index() { string url = new Uri(this.Request.Url, "../" + ConfigurationManager.AppSettings["MobileAppPath"]).AbsoluteUri + "?v=" + DateTime.Now.Ticks.ToString(); return Redirect(WechatHelper.GetBaseAuthorizeUrl(url)); } #region 支付 public ActionResult NativePay(Guid examinationRegistrationID, decimal fee, string feeTypeName) { var nativePayView = new NativePayView(); nativePayView.ExaminationRegistrationID = examinationRegistrationID; nativePayView.Fee = fee; nativePayView.FeeTypeName = feeTypeName; var lastOrder = this.WechatPayServices.GetLastOrder(examinationRegistrationID); if (lastOrder != null && lastOrder.OrderID != null) { this.WechatPayServices.UpdateWeChatPay(new List { WechatHelper.OrderQuery(lastOrder.OrderID) }); } lastOrder = this.WechatPayServices.GetLastOrder(examinationRegistrationID); if (lastOrder != null && lastOrder.OrderID != null) { nativePayView.ProductID = lastOrder.OrderID; } else { nativePayView.ProductID = WechatPayServices.GetProductId(); } return View(nativePayView); } public ActionResult PayQRCode(Guid examinationRegistrationID, string productId, decimal fee, string feeTypeName) { var lastOrder = this.WechatPayServices.GetLastOrder(examinationRegistrationID); if (lastOrder != null && lastOrder.OrderID != null) { try { if (lastOrder.PayUrl != null) { var memoryStream = new MemoryStream(); var qrcodeImage = QRCodeHelper.GenerateQRCode(lastOrder.PayUrl); qrcodeImage.Save(memoryStream, ImageFormat.Png); return new FileContentResult(memoryStream.GetBuffer(), "image/png"); } else { WechatHelper.CloseOrder(lastOrder.OrderID); } } catch (Exception ex) { return new FileContentResult(new byte[0], "image/png"); } } WechatPayServices.NewPayList(examinationRegistrationID, productId); try { string payUrl; var content = WechatHelper.GetPayQRCode(productId, fee, feeTypeName, out payUrl); WechatPayServices.SaveUrl(examinationRegistrationID, payUrl); return content; } catch (Exception ex) { return new FileContentResult(new byte[0], "image/png"); } } public ActionResult PayNotify() { try { var payNotifyView = WechatHelper.PayNotify(); try { //向微信发送请求,根据返回结果修改报名缴费状态 OrderQueryResult query = WechatHelper.OrderQuery(payNotifyView.OrderID); List queryList = new List(); queryList.Add(query); WechatPayServices.UpdateWeChatPay(queryList); //WechatPayServices.UpdateTransactionID(payNotifyView.OrderID, payNotifyView.WechatOrderID); //给财付通系统发送成功信息,财付通系统收到此结果后不再进行后续通知 return WechatHelper.PayNotifySuccess; } catch (Exception ex) { return WechatHelper.PayNotifyFail("支付失败,商户内部数据库更新出错。"); } } catch (Exception ex) { return WechatHelper.PayNotifyFail(ex.Message); } } [HttpPost] public ActionResult CheckOrderScanned(string productId) { try { var order = WechatHelper.OrderQuery(productId); string[] payingStatus = new string[] { "NOTPAY" }; if (!payingStatus.Contains(order.trade_state)) { return Json(true); } else { return Json(false); } } catch { return Json(false); } } #endregion #region 退款 public ActionResult RefundNotify() { try { var refundNotifyView = WechatHelper.RefundNotify(); WechatPayServices.UpdateRefund(refundNotifyView.OrderID, refundNotifyView.WechatRefundOrderID, refundNotifyView.Fee); //给财付通系统发送成功信息,财付通系统收到此结果后不再进行后续通知 return WechatHelper.PayNotifySuccess; } catch (Exception ex) { return WechatHelper.PayNotifyFail("支付失败,商户内部数据库更新出错。"); } } #endregion } }