123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using System.Linq;
- using System.Data;
- using MySystem;
- using Library;
- using LitJson;
- using Aop.Api;
- using Aop.Api.Request;
- using Newtonsoft.Json;
- using Aop.Api.Response;
- using System.Net;
- using System.Security.Cryptography;
- using System.IO;
- using MySystem.MainModels;
- using Microsoft.AspNetCore.Http;
- using Microsoft.Extensions.Logging;
- using MySystem.Areas.Api.Controllers;
- using Microsoft.Extensions.Options;
- /// <summary>
- /// 来客吧支付
- /// </summary>
- public class OrdersPayService
- {
- public readonly static OrdersPayService Instance = new OrdersPayService();
- private OrdersPayService()
- {
- }
- public void StartEverTime()
- {
- Thread th = new Thread(StartEverTimeDo);
- th.IsBackground = true;
- th.Start();
- }
- #region 来客吧支付队列
- private void StartEverTimeDo()
- {
- while (true)
- {
- try
- {
- string data = RedisDbconn.Instance.RPop<string>("OrdersPayQueue");
- if (!string.IsNullOrEmpty(data))
- {
- function.WriteLog("data:" + data, "来客吧支付回调日志");
- JsonData jsonData = JsonMapper.ToObject(data);
- string SnNo = jsonData["Sn"].ToString(); //码牌SN
- int PayMode = int.Parse(function.CheckInt(jsonData["PayMode"].ToString())); //支付方式
- decimal PayMoney = decimal.Parse(function.CheckNum(jsonData["PayMoney"].ToString())); //支付金额
- string Code = jsonData["Code"].ToString(); // 订单唯一标识
- string OnlyFlag = jsonData["OnlyFlag"].ToString();
- string Value = jsonData["Value"].ToString();
- Dictionary<string, object> Obj = new Dictionary<string, object>();
- MySystem.AppResultJson returnObj = new MySystem.AppResultJson();
- MerchantQrCode qrcode = MerchantQrCodeDbconn.Instance.Get(SnNo) ?? new MerchantQrCode();
- PosMachinesTwo pos = PosMachinesTwoDbconn.Instance.Get(SnNo) ?? new PosMachinesTwo();
- PosMachines machine = PosMachinesDbconn.Instance.Get(SnNo) ?? new PosMachines();
- function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "-5", "支付时间监控");
- int MerchantId = pos.BindMerchantId;
- if (machine.DeviceKind == "1")
- {
- MerchantId = machine.BindMerchantId;
- }
- if (pos.OpId == 1 || machine.DeviceKind == "1")
- {
- WebCMSEntities maindb = new WebCMSEntities();
- MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(MerchantId) ?? new MerchantInfo();
- MerchantAddInfo merchantAdd = MerchantAddInfoDbconn.Instance.Get(MerchantId) ?? new MerchantAddInfo();
- MerchantParamSet merchantset = MerchantParamSetDbconn.Instance.Get(MerchantId) ?? new MerchantParamSet();
- function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "-6", "支付时间监控");
- //通过Code获取OpenId
- string openid = GetOpenIdByCodeAndPayMode(Code, PayMode);
- function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "-7", "支付时间监控");
- //通过OpenId获取ConsumerId
- int ConsumerId = GetConsumerIdByOpenId(openid);
- string OrderNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
- bool ActFlag = merchantset.IsAll == 1 ? false : true;
- if (PayMoney < merchantset.MinPayMoney) //支付金额小于活动最小金额,则不分账
- {
- ActFlag = false;
- }
-
- ConsumerOrders order = maindb.ConsumerOrders.Add(new ConsumerOrders()
- {
- // Id = PublicFunction.MakeConsumerOrderId(),
- CreateDate = DateTime.Now, //创建时间
- MerchantId = MerchantId, //商户
- ConsumerId = ConsumerId, //消费者
- UserId = merchant.UserId, //创客
- PayMode = PayMode, //支付方式
- PayMoney = PayMoney, //支付金额
- SnNo = SnNo, //SN号
- OrderNo = OrderNo,
- MaxDivi = ActFlag ? PayMoney * merchantset.DiviPercent / 100 : 0,
- IsAct = ActFlag ? 1u : 0u,
- MerchantActualAmount = ActFlag ? PayMoney * merchantset.GetPercent / 100 : PayMoney,
- SeoDescription = Newtonsoft.Json.JsonConvert.SerializeObject(merchantset),
- }).Entity;
- maindb.SaveChanges();
- maindb.ConsumerOrderForNo.Add(new ConsumerOrderForNo()
- {
- OrderNo = OrderNo,
- OrderIds = order.Id,
- });
- maindb.SaveChanges();
- if (PayMode == 1)
- {
- string backString = new AlipayFunction().CreateTrade(OrderNo, merchant.Name, PayMoney, openid, merchantAdd.AlipayAuthToken, ConfigurationManager.AppSettings["SpHost"].ToString() + "/api/alipay/notice");
- JsonData obj = JsonMapper.ToObject(backString);
- if (obj["alipay_trade_create_response"]["code"].ToString() == "10000")
- {
- string tradeNo = obj["alipay_trade_create_response"]["trade_no"].ToString();
- Obj.Add("respCode", obj["alipay_trade_create_response"]["code"].ToString());
- Obj.Add("tradeNo", tradeNo);
- }
- else
- {
- Obj.Add("respCode", obj["alipay_trade_create_response"]["code"].ToString());
- Obj.Add("tradeNo", "");
- returnObj = new MySystem.AppResultJson() { Status = "-1", Info = obj["alipay_trade_create_response"]["sub_msg"].ToString(), Data = Obj };
- }
- }
- else if (PayMode == 2)
- {
- string SubMchId = merchantAdd.SubMchid;
- string Description = merchant.Name;
- string OpenId = openid;
- string Key = AppConfig.WeChatParam.AesGemKey;
- string NotifyUrl = ConfigurationManager.AppSettings["SpHost"].ToString() + "/api/wechat/notice";
- Dictionary<string, string> dic = new WeChatFunction().Pay(SubMchId, PayMoney, OrderNo, Description, OpenId, Key, NotifyUrl, ActFlag);
- Obj.Add("appId", dic["appId"]); //微信小程序appid
- Obj.Add("timeStamp", dic["timeStamp"]); //时间戳
- Obj.Add("nonceStr", dic["nonceStr"]); //随机字符串
- Obj.Add("package", dic["package"]); //统一支付接口返回的prepayid参数值
- Obj.Add("paySign", dic["paySign"]); //支付签名
- Obj.Add("ConsumerId", ConsumerId);
- }
- RedisDbconn.Instance.AddList("OrdersPayBack:" + OnlyFlag, Newtonsoft.Json.JsonConvert.SerializeObject(returnObj));
- }
- else
- {
- string result = function.PostWebRequest(AppConfig.Base.Host2 + "api/v1/consumerorders/pay", "value=" + Value);
- // return Newtonsoft.Json.JsonConvert.DeserializeObject<AppResultJson>(result);
- RedisDbconn.Instance.AddList("OrdersPayBack:" + OnlyFlag, result);
- }
- }
- }
- catch (Exception ex)
- {
- function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "来客吧支付回调线程异常");
- }
- }
- }
- #endregion
- #region 通过Code和支付方式获取OpenId
- /// <summary>
- /// 通过Code获取OpenId
- /// </summary>
- /// <param name="Code">消费者编码</param>
- /// <param name="PayMode">支付方式(1 支付宝 2 微信)</param>
- /// <returns></returns>
- public string GetOpenIdByCodeAndPayMode(string Code, int PayMode)
- {
- var OpenId = "";
- if (PayMode == 1)
- {
- OpenId = new AlipayFunction().GetAlipayUserId(Code);
- if (OpenId.Contains("|"))
- {
- OpenId = OpenId.Split('|')[0];
- }
- }
- else
- {
- string result = function.GetWebRequest("https://api.weixin.qq.com/sns/jscode2session?appid=" + new WeChatFunction().AppId + "&secret=" + new WeChatFunction().AppSecret + "&js_code=" + Code + "&grant_type=authorization_code");
- function.WriteLog(DateTime.Now.ToString() + "\n" + result, "微信小程序获取openid");
- JsonData jsonObj = JsonMapper.ToObject(result);
- OpenId = jsonObj["openid"].ToString();
- }
- return OpenId;
- }
- #endregion
- #region 通过OpenId获取ConsumerId
- /// <summary>
- /// 通过OpenId获取ConsumerId
- /// </summary>
- /// <param name="OpenId">消费者唯一标识</param>
- /// <returns></returns>
- public int GetConsumerIdByOpenId(string OpenId)
- {
- WebCMSEntities maindb = new WebCMSEntities();
- var ConsumerId = 0;
- Consumers check = maindb.Consumers.FirstOrDefault(m => m.WechatOpenId == OpenId);
- if (check == null)
- {
- // ConsumerId = PublicFunction.MakeConsumerId();
- Consumers consumer = maindb.Consumers.Add(new Consumers()
- {
- Id = ConsumerId,
- CreateDate = DateTime.Now,
- WechatOpenId = OpenId,
- }).Entity;
- maindb.SaveChanges();
- ConsumerId = consumer.Id;
- }
- else
- {
- ConsumerId = check.Id;
- }
- maindb.SaveChanges();
- return ConsumerId;
- }
- #endregion
- }
|