123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Transactions;
- using EMIS.DataLogic.SystemDAL;
- using EMIS.Entities;
- using EMIS.Utility.OnlinePay;
- using EMIS.ViewModel;
- using Senparc.Weixin.TenPay.V3;
- namespace EMIS.CommonLogic.SystemServices
- {
- public class WechatPayServices : BaseServices, IWechatPayServices
- {
- public WechatPayListDAL WechatPayListDAL { get; set; }
- public Lazy<IParameterServices> ParameterServices { get; set; }
- public string GetProductId()
- {
- DateTime today = DateTime.Today;
- string productId = ParameterServices.Value.GetParameterValue(CF_ParameterType.WechatProductId);
- int seed = 1;
- if (productId != null)
- {
- if (productId.Substring(0, 8) == today.ToString("yyyyMMdd"))
- {
- seed = Convert.ToInt32(productId.Substring(8, productId.Length - 8)) + 1;
- }
- }
- productId = today.ToString("yyyyMMdd") + seed.ToString().PadLeft(6, '0');
- ParameterServices.Value.SaveTo(CF_ParameterType.WechatProductId, productId);
- return productId;
- }
- public void NewPayList(Guid examinationRegistrationID, string orderID)
- {
- using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions { IsolationLevel = IsolationLevel.Serializable }))
- {
- var dupOrder = WechatPayListDAL.WechatPayListRepository.GetSingle(x => x.OrderID == orderID);
- var dbOrder = WechatPayListDAL.WechatPayListRepository.GetSingle(x => x.ExaminationRegistrationID == examinationRegistrationID);
- var examinationRegistration = WechatPayListDAL.ExaminationRegistrationRepository.GetSingle(x => x.ExaminationRegistrationID == examinationRegistrationID);
- if (dbOrder != null && dbOrder.CreateTime != null)
- {
- string minutes = Math.Round((5 - DateTime.Now.Subtract(dbOrder.CreateTime.Value).TotalMinutes),1).ToString();
- throw new Exception("上一次创建订单未完成,请于" + minutes + "分钟之后再操作");
- }
- if (dupOrder != null)
- {
- throw new Exception("商家订单号重复,不能下单。");
- }
- var wechatPayList = new CF_WechatPayList();
- wechatPayList.WechatPayListID = Guid.NewGuid();
- wechatPayList.ExaminationRegistrationID = examinationRegistrationID;
- wechatPayList.OrderID = orderID;
- wechatPayList.RecordStatus = (int)CF_WechatPayRefundStatus.NotRefund;
- wechatPayList.CreateTime = DateTime.Now;
- examinationRegistration.RecordStatus = (int)EX_ExaminationRegistrationStatus.Paying;
- //UnitOfWork.Remove<CF_WechatPayList>(x => x.ExaminationRegistrationID == examinationRegistrationID);
- //UnitOfWork.Update(examinationRegistration);
- UnitOfWork.Add(wechatPayList);
- UnitOfWork.Commit();
- ts.Complete();
- }
- }
- public void PayFail(Guid examinationRegistrationID, string payForm)
- {
- var examinationRegistration = WechatPayListDAL.ExaminationRegistrationRepository.GetSingle(x => x.ExaminationRegistrationID == examinationRegistrationID);
- var dbOrder = WechatPayListDAL.WechatPayListRepository.GetSingle(x => x.ExaminationRegistrationID == examinationRegistrationID);
- if (dbOrder != null)
- {
- dbOrder.PayForm = payForm;
- }
- if (examinationRegistration != null)
- {
- examinationRegistration.RecordStatus = (int)EX_ExaminationRegistrationStatus.Paying;
- }
- this.UnitOfWork.Commit();
- }
- public void SaveUrl(Guid examinationRegistrationID, string payUrl)
- {
- var dbOrder = WechatPayListDAL.WechatPayListRepository.GetSingle(x => x.ExaminationRegistrationID == examinationRegistrationID);
- if (dbOrder != null)
- {
- dbOrder.PayUrl = payUrl;
- }
- this.UnitOfWork.Commit();
- }
- public void UpdateTransactionID(string orderID, string wechatOrderID)
- {
- var wechatPay = WechatPayListDAL.WechatPayListRepository.GetSingle(x => x.OrderID == orderID, (x => x.EX_ExaminationRegistration));
- if (wechatPay == null)
- {
- throw new Exception("找不到对应的商家订单号,收款失败。");
- }
- wechatPay.WechatOrderID = wechatOrderID;
- if (wechatPay.EX_ExaminationRegistration == null)
- {
- throw new Exception("无法匹配对应的考试报名记录,收款失败。");
- }
- if (wechatPay.EX_ExaminationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.ForPay)
- {
- return;
- }
- wechatPay.EX_ExaminationRegistration.PayTime = DateTime.Now;
- wechatPay.EX_ExaminationRegistration.RecordStatus = (int)EX_ExaminationRegistrationStatus.Paid;
- UnitOfWork.Commit();
- }
- public void UpdateRefund(string orderID, string refundOrderID, decimal fee)
- {
- var wechatPay = WechatPayListDAL.WechatPayListRepository.GetSingle(x => x.OrderID == orderID, (x => x.EX_ExaminationRegistration));
- if (wechatPay == null)
- {
- throw new Exception("找不到对应的商家订单号,退款失败。");
- }
- wechatPay.RefundOrderID = refundOrderID;
- wechatPay.RefundTotal = (wechatPay.RefundTotal ?? 0) + fee;
- if (wechatPay.EX_ExaminationRegistration == null)
- {
- throw new Exception("无法匹配对应的考试报名记录,收款失败。");
- }
- if (wechatPay.EX_ExaminationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.OnRefund)
- {
- return;
- }
- wechatPay.EX_ExaminationRegistration.RecordStatus = (int)EX_ExaminationRegistrationStatus.Refunded;
- UnitOfWork.Commit();
- }
- public CF_WechatPayList GetLastOrder(Guid examinationRegistrationID)
- {
- var wechatPayList = WechatPayListDAL.WechatPayListRepository.GetSingle(x => x.ExaminationRegistrationID == examinationRegistrationID);
- return wechatPayList;
- }
- public List<CF_WechatPayList> SearchWeChatPayList(Guid? userID = null)
- {
- //查询已有商家订单号,但未有微信订单号的数据(这些数据是未交费,或者缴费中数据)
- var wechatpayList = WechatPayListDAL.GetWechatPayListQueryable().Where(x => x.WechatOrderID == null).ToList();
- return wechatpayList;
- }
- /// <summary>
- /// 根据向微信商户号查询的订单信息更新报名名单的状态
- /// </summary>
- /// <param name="orderQueryResultList"></param>
- public void UpdateWeChatPay(List<OrderQueryResult> orderQueryResultList)
- {
- List<CF_WechatPayList> updateWechatPayLists = new List<CF_WechatPayList>();
- List<EX_ExaminationRegistration> updateExaminationRegistrationList = new List<EX_ExaminationRegistration>();
- List<CF_WechatPayList> deleteWechatPayList = new List<CF_WechatPayList>();
- var outnoList = orderQueryResultList.Select(x => x.out_trade_no).ToList();
- var dbwechatPayLists = WechatPayListDAL.WechatPayListRepository.Entities.Where(x => outnoList.Contains(x.OrderID)).ToList();
- var examinationRegistrationIDList = dbwechatPayLists.Select(x => x.ExaminationRegistrationID).ToList();
- var dbexaminationRegistrationList = WechatPayListDAL.ExaminationRegistrationRepository.Entities.Where(x => examinationRegistrationIDList.Contains(x.ExaminationRegistrationID)).ToList();
- try
- {
- foreach (var order in orderQueryResultList)
- {
- var dborder = dbwechatPayLists.Where(x => x.OrderID == order.out_trade_no).FirstOrDefault();
- if (dborder != null)
- {
- //var wechatPay = dbwechatPayLists.Where(x => x.ExaminationRegistrationID == dborder.ExaminationRegistrationID).ToList();
- var examinationRegistration = dbexaminationRegistrationList.Where(x => x.ExaminationRegistrationID == dborder.ExaminationRegistrationID).FirstOrDefault();
- if (order.trade_state == "SUCCESS")
- {
- if (examinationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.ForPay && examinationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.Paying)
- {
- continue;
- }
- dborder.Total = decimal.Parse(order.total_fee)/100;
- dborder.WechatOrderID = order.transaction_id;
- dborder.PayForm = null;
- dborder.PayUrl = null;
- examinationRegistration.RecordStatus = (int)EX_ExaminationRegistrationStatus.Paid;
- examinationRegistration.PayTime = DateTime.ParseExact(order.time_end, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
- examinationRegistration.ModifyTime = DateTime.Now;
- updateWechatPayLists.Add(dborder);
- updateExaminationRegistrationList.Add(examinationRegistration);
- }
- if (order.trade_state == "CLOSED" || order.trade_state == "PAYERROR")
- {
- if (examinationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.ForPay && examinationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.Paying)
- {
- continue;
- }
- if (WechatHelper.CloseOrder(dborder.OrderID))
- {
- deleteWechatPayList.Add(dborder);
- examinationRegistration.RecordStatus = (int)EX_ExaminationRegistrationStatus.ForPay;
- examinationRegistration.ModifyTime = DateTime.Now;
- updateExaminationRegistrationList.Add(examinationRegistration);
- }
- }
- if (order.trade_state == "REVOKED")
- {
- if (examinationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.ForPay && examinationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.Paying)
- {
- continue;
- }
- if (WechatHelper.CloseOrder(dborder.OrderID))
- {
- deleteWechatPayList.Add(dborder);
- examinationRegistration.RecordStatus = (int)EX_ExaminationRegistrationStatus.ForPay;
- examinationRegistration.ModifyTime = DateTime.Now;
- updateExaminationRegistrationList.Add(examinationRegistration);
- }
- }
- if (order.trade_state == "REFUND")
- {
- examinationRegistration.RecordStatus = (int)EX_ExaminationRegistrationStatus.Refunded;
- examinationRegistration.ModifyTime = DateTime.Now;
- updateExaminationRegistrationList.Add(examinationRegistration);
- }
- if (order.trade_state == "NOTPAY" || order.trade_state == "USERPAYING")
- {
- if (dborder.CreateTime != null)
- {
- if (DateTime.Now.Subtract(dborder.CreateTime.Value).TotalSeconds > 60 * 6)
- {
- if (examinationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.ForPay && examinationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.Paying)
- {
- continue;
- }
- if (WechatHelper.CloseOrder(dborder.OrderID))
- {
- deleteWechatPayList.Add(dborder);
- examinationRegistration.RecordStatus = (int)EX_ExaminationRegistrationStatus.ForPay;
- examinationRegistration.ModifyTime = DateTime.Now;
- updateExaminationRegistrationList.Add(examinationRegistration);
- }
-
- }
- }
- else {
- if (examinationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.ForPay && examinationRegistration.RecordStatus != (int)EX_ExaminationRegistrationStatus.Paying)
- {
- continue;
- }
- examinationRegistration.RecordStatus = (int)EX_ExaminationRegistrationStatus.Paying;
- examinationRegistration.ModifyTime = DateTime.Now;
- updateExaminationRegistrationList.Add(examinationRegistration);
- }
- }
- }
- else
- {
- continue;
- }
- }
- using (TransactionScope ts = new TransactionScope())
- {
- UnitOfWork.Delete(deleteWechatPayList);
- UnitOfWork.BatchUpdate(updateWechatPayLists);
- UnitOfWork.BatchUpdate(updateExaminationRegistrationList);
- ts.Complete();
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- public void DeleteCanCloseWeChatPay(List<String> OrderIDList)
- {
- UnitOfWork.Delete<CF_WechatPayList>(x => OrderIDList.Contains(x.OrderID));
- }
- }
- }
|