123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- 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<OrderQueryResult> { 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<OrderQueryResult> queryList = new List<OrderQueryResult>();
- 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
- }
- }
|