123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System;
- using System.Collections.Generic;
- using Library;
- using LitJson;
- using System.Linq;
- using MySystem.Models.Main;
- using Org.BouncyCastle.Crypto.Modes;
- using Org.BouncyCastle.Crypto.Engines;
- using Org.BouncyCastle.Crypto.Parameters;
- using System.Text;
- using System.Threading;
- namespace MySystem
- {
- public class WeChatPayBackService
- {
- public readonly static WeChatPayBackService Instance = new WeChatPayBackService();
- private WeChatPayBackService()
- { }
- public void Start()
- {
- Thread th = new Thread(Listen);
- th.IsBackground = true;
- th.Start();
- }
- public void Listen()
- {
- bool op = true;
- while (op)
- {
- string content = RedisDbconn.Instance.RPop<string>("WeChatPayBack");
- if (!string.IsNullOrEmpty(content))
- {
- try
- {
- dosomething(content);
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信支付回调异常");
- }
- }
- else
- {
- Thread.Sleep(2000);
- }
- }
- }
- public void dosomething(string content)
- {
- JsonData jsonObj = JsonMapper.ToObject(content);
- if (jsonObj.Count > 0)
- {
- // if (jsonObj["event_type"].ToString() == "TRANSACTION.SUCCESS")
- // {
- // JsonData resource = jsonObj["resource"];
- //开始解密
- // string WxPayResourceDecryptModel = AesGcmDecrypt(resource["associated_data"].ToString(), resource["nonce"].ToString(), resource["ciphertext"].ToString());
- // {\"sp_mchid\":\"1611167423\",\"sub_mchid\":\"1622024882\",\"sp_appid\":\"wxe2c051b3e46c0f6f\",\"out_trade_no\":\"2022022621562926396898863\",\"transaction_id\":\"4200001412202202267619496496\",\"trade_type\":\"JSAPI\",\"trade_state\":\"SUCCESS\",\"trade_state_desc\":\"支付成功\",\"bank_type\":\"OTHERS\",\"attach\":\"\",\"success_time\":\"2022-02-26T21:56:42+08:00\",\"payer\":{\"sp_openid\":\"omawy5W6jb0pgPfuKUVs6K3bEhzk\",\"sub_openid\":\"\"},\"amount\":{\"total\":1,\"payer_total\":1,\"currency\":\"CNY\",\"payer_currency\":\"CNY\"}}
- // JsonData orderObj = JsonMapper.ToObject(WxPayResourceDecryptModel);
- string OrderNo = jsonObj["outOrderNo"].ToString();
- string TradeNo = jsonObj["orderNo"].ToString();
- WebCMSEntities db = new WebCMSEntities();
- ConsumerOrderForNo forNo = db.ConsumerOrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
- if (forNo != null)
- {
- ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == forNo.OrderIds && m.Status == 0);
- if (order != null)
- {
- order.Status = 1;
- order.UpdateDate = DateTime.Now;
- order.PayMoney = order.PayMoney;
- order.MaxDivi = order.MaxDivi;
- order.SeoTitle = TradeNo;
- MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
- order.UserId = merchant.UserId;
- ConsumerOrders check = db.ConsumerOrders.FirstOrDefault(m => m.Id < order.Id && m.Status > 0 && m.PayMode == order.PayMode && m.MerchantId == order.MerchantId);
- if(check != null)
- {
- order.QueryCount = check.QueryCount + 1;
- }
- else
- {
- order.QueryCount = 1;
- }
- db.SaveChanges();
- }
- }
- db.Dispose();
- // }
- }
- }
- // public string AesGcmDecrypt(string associatedData, string nonce, string ciphertext)
- // {
- // GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesEngine());
- // AeadParameters aeadParameters = new AeadParameters(
- // new KeyParameter(Encoding.UTF8.GetBytes(AppConfig.WeChatParam.AesGemKey)),
- // 128,
- // Encoding.UTF8.GetBytes(nonce),
- // Encoding.UTF8.GetBytes(associatedData));
- // gcmBlockCipher.Init(false, aeadParameters);
- // byte[] data = Convert.FromBase64String(ciphertext);
- // byte[] plaintext = new byte[gcmBlockCipher.GetOutputSize(data.Length)];
- // int length = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0);
- // gcmBlockCipher.DoFinal(plaintext, length);
- // return Encoding.UTF8.GetString(plaintext);
- // }
- //分账队列
- public void StartProfitShare()
- {
- Thread th = new Thread(StartProfitShareListen);
- th.IsBackground = true;
- th.Start();
- }
- public void StartProfitShareListen()
- {
- bool op = true;
- while (op)
- {
- try
- {
- WebCMSEntities db = new WebCMSEntities();
- DateTime checkDate = DateTime.Now.AddMinutes(-1);
- var list = db.ConsumerOrders.Select(m => new { m.Id, m.CreateDate, m.Status, m.IsAct, m.PayMode }).Where(m => m.Status == 1 && m.IsAct == 1 && m.CreateDate < checkDate).OrderBy(m => m.Id).Take(10).ToList();
- foreach(var sub in list)
- {
- ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
- if(order != null)
- {
- order.Status = 2;
- MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
- MerchantParamSet set = db.MerchantParamSet.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantParamSet();
- if(order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
- {
- MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
- //添加分账接收方
- // if(merchant.Version == 0)
- // {
- // AddOpenDivideAccounts addOpenDivideAccounts =
- // HaoDaHelper.Instance.AddOpenDivideAccounts();
- // merchant.Version = 1;
- // db.SaveChanges();
- // }
- //发起分账
- decimal fee = order.PayMoney; //单位:分
- if(fee >= 1)
- {
- string TradeNo = order.SeoTitle;
- string OrderNo = order.OrderNo;
- var info = OrderDivideAccountsUtil.SetValue("mchtNo",OrderNo,fee);
- HaoDaHelper.Instance.OrderDivideAccounts(info);
- //开始监听分账状态
- Dictionary<string, object> req = new Dictionary<string, object>();
- req.Add("SubMchid", merchantAdd.SubMchid); //子商户号
- req.Add("TradeNo", TradeNo); //微信订单号
- req.Add("OrderNo", OrderNo); //商户订单号
- RedisDbconn.Instance.AddList("ProfitShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
- }
- else
- {
- RedisDbconn.Instance.AddList("ConsumerOrders:Divi:2:List", order.Id.ToString());
- // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:2:" + order.MerchantId, order);
- }
- }
- }
- }
- db.SaveChanges();
- db.Dispose();
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "微信分账队列异常");
- }
- Thread.Sleep(2000);
- }
- }
- }
- }
|