using EMIS.CommonLogic.SystemServices;
using EMIS.Utility;
using EMIS.ViewModel.AlipayModel;
using EMIS.Utility.OnlinePay;
using EMIS.Utility.OnlinePay.Alipay;
using EMIS.Utility.OnlinePay.Alipay.Models;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace EMIS.Web.Controllers
{
[Authorization]
public class AlipayController : Controller
{
public IWechatPayServices WechatPayServices { get; set; }
public IAlipayServices AlipayServices { get; set; }
public ActionResult NativePay(Guid examinationRegistrationID, decimal fee, string feeTypeName)
{
var nativePayView = new NativePayView();
nativePayView.ExaminationRegistrationID = examinationRegistrationID;
nativePayView.Fee = fee;
nativePayView.FeeTypeName = feeTypeName;
nativePayView.ProductID = AlipayServices.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
{
AlipayHelper.CloseOrder(lastOrder.OrderID);
}
catch (Exception ex)
{
}
}
WechatPayServices.NewPayList(examinationRegistrationID, productId);
try
{
return AlipayHelper.GetPayQRCode(productId, fee, feeTypeName);
}
catch (Exception ex)
{
return new FileContentResult(new byte[0], "image/png");
}
}
///
/// 对支付宝异步通知的关键参数进行校验
///
///
private bool CheckParams()
{
bool ret = true;
string app_id = Request.Form["app_id"];
if (app_id != Config.appId)
{
ret = false;
}
return ret;
}
public ActionResult PayNotify()
{
try
{
var logger = NLog.LogManager.GetCurrentClassLogger();
logger.Debug("支付宝:返回订单通知。");
SortedDictionary sPara = HttpHelper.GetRequestPost();
if (sPara.Count > 0)//判断是否有带返回参数
{
Notify aliNotify = new Notify(Config.charset, Config.sign_type, Config.pid, Config.mapiUrl, Config.alipay_public_key);
bool verifyResult = aliNotify.Verify(sPara, Request.Form["notify_id"], Request.Form["sign"]);
if (verifyResult && CheckParams())
{
string out_trade_no = Request.Form["out_trade_no"];
string trade_no = Request.Form["trade_no"];
//交易状态
//在支付宝的业务通知中,只有交易通知状态为TRADE_SUCCESS或TRADE_FINISHED时,才是买家付款成功。
string trade_status = Request.Form["trade_status"];
if (trade_status == "TRADE_SUCCESS" || trade_status == "TRADE_FINISHED")
{
WechatPayServices.UpdateTransactionID(out_trade_no, trade_no);
}
return Content("success");
}
else//验证失败
{
return Content("fail");
}
}
else
{
return Content("无通知参数");
}
}
catch (Exception ex)
{
var logger = NLog.LogManager.GetCurrentClassLogger();
logger.Debug(ex.Message + ex.StackTrace);
return Content("fail");
}
}
[HttpPost]
public ActionResult CheckOrderScanned(string productId)
{
try
{
var order = AlipayHelper.OrderQuery(productId);
return Json(AlipayHelper.IsTradeFinished(order.TradeStatus));
}
catch
{
return Json(false);
}
}
}
}