ConsumerOrdersController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.Extensions.Logging;
  7. using Microsoft.Extensions.Options;
  8. using Microsoft.AspNetCore.Authorization;
  9. using System.Web;
  10. using MySystem.Models.Main;
  11. using LitJson;
  12. using Library;
  13. using System.Data;
  14. using MySystem.Service.Main;
  15. namespace MySystem.Areas.Api.Controllers.v1
  16. {
  17. [Area("Api")]
  18. [Route("Api/v1/[controller]/[action]")]
  19. public class ConsumerOrdersController : BaseController
  20. {
  21. public ConsumerOrdersController(IHttpContextAccessor accessor) : base(accessor)
  22. {
  23. }
  24. #region 消费者-支付接口
  25. // [Authorize]
  26. public JsonResult Pay(string value)
  27. {
  28. if (string.IsNullOrEmpty(value))
  29. {
  30. System.IO.StreamReader sr = new System.IO.StreamReader(Request.Body);
  31. value = sr.ReadToEnd();
  32. value = value.Split('=')[1];
  33. }
  34. // value = DesDecrypt(value);
  35. value = value.Replace("null", "\"\"");
  36. JsonData data = JsonMapper.ToObject(value);
  37. string SnNo = data.getItem("Sn").ToString(); //商户
  38. string Machine = data.getItem("Machine").ToString();
  39. int PayMode = int.Parse(function.CheckInt(data.getItem("PayMode").ToString())); //支付方式(1 支付宝 2 微信)
  40. decimal PayMoney = decimal.Parse(function.CheckNum(data.getItem("PayMoney").ToString())); //支付金额
  41. string Code = data.getItem("Code").ToString();
  42. if (string.IsNullOrEmpty(data["PayMode"].ToString()))
  43. {
  44. return Json(new AppResultJson() { Status = "-1", Info = "请填写支付方式" });
  45. }
  46. if (string.IsNullOrEmpty(data["PayMoney"].ToString()))
  47. {
  48. return Json(new AppResultJson() { Status = "-1", Info = "请填写支付金额" });
  49. }
  50. if (!function.IsNum(data["PayMoney"].ToString()))
  51. {
  52. return Json(new AppResultJson() { Status = "-1", Info = "请填写正确的支付金额" });
  53. }
  54. if (SnNo.Length > 20)
  55. {
  56. // SnNo = System.Web.HttpUtility.UrlDecode(SnNo);
  57. if (!SnNo.EndsWith("="))
  58. {
  59. SnNo += "=";
  60. }
  61. SnNo = dbconn.Decrypt3DES(SnNo, "l2k0b2#3");
  62. SnNo = SnNo.TrimEnd('\0');
  63. SnNo = SnNo.Substring(0, 20);
  64. }
  65. if (!string.IsNullOrEmpty(Machine))
  66. {
  67. if (Machine.Length > 20)
  68. {
  69. // Machine = System.Web.HttpUtility.UrlDecode(Machine);
  70. if (!Machine.EndsWith("="))
  71. {
  72. Machine += "=";
  73. }
  74. Machine = dbconn.Decrypt3DES(Machine, "l2k0b2#3");
  75. Machine = Machine.TrimEnd('\0');
  76. Machine = Machine.Substring(0, 20);
  77. }
  78. }
  79. int MerchantId = 0;
  80. if (Machine == SnNo)
  81. {
  82. var qrcode = MerchantQrCodeService.Query(" SnNo='" + SnNo + "'");
  83. MerchantId = qrcode.MerchantId;
  84. }
  85. else
  86. {
  87. var machine = PosMachinesService.Query(" PosSn='" + SnNo + "'");
  88. MerchantId = machine.BindMerchantId;
  89. }
  90. var merchant = MerchantInfoService.Query(MerchantId);
  91. var merchantAdd = MerchantAddInfoService.Query(MerchantId);
  92. var merchantset = MerchantParamSetService.Query(MerchantId);
  93. string openid = "";
  94. //商户未激活也能支付(365服务费)
  95. // if (merchant.IsAct == 0)
  96. // {
  97. // return Json(new AppResultJson() { Status = "-1", Info = "支付失败,商户尚未激活,请前往来客吧商户版激活后使用" });
  98. // }
  99. if (PayMode == 1)
  100. {
  101. openid = new AlipayFunction(_accessor.HttpContext).GetAlipayUserId(Code);
  102. if (openid.Contains("|"))
  103. {
  104. openid = openid.Split('|')[0];
  105. }
  106. }
  107. else
  108. {
  109. 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");
  110. function.WriteLog(DateTime.Now.ToString() + "\n" + result, "微信小程序获取openid");
  111. JsonData jsonObj = JsonMapper.ToObject(result);
  112. openid = jsonObj["openid"].ToString();
  113. }
  114. int ConsumerId = 0;
  115. // var check = ConsumerOpenIdsService.Exist(openid);
  116. var check = ConsumersService.Exist(openid);
  117. if (check == false)
  118. {
  119. var info = ConsumersUtil.AddConsumers(ConsumerId, openid);
  120. ConsumerId = int.Parse(info.Data.ToString());
  121. // ConsumerOpenIdsUtil.AddConsumerOpenIds(ConsumerId, openid);
  122. }
  123. else
  124. {
  125. // var info = ConsumerOpenIdsService.Query(" OpenId='" + openid + "'");
  126. // ConsumerId = info.ConsumerId;
  127. var info = ConsumersService.Query(" WechatOpenId='" + openid + "'");
  128. ConsumerId = info.Id;
  129. }
  130. Dictionary<string, object> Obj = new Dictionary<string, object>();
  131. string OrderNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  132. bool ActFlag = merchantset.IsAll == 1 ? false : true;
  133. if (PayMoney < merchantset.MinPayMoney) //支付金额小于活动最小金额,则不分账
  134. {
  135. ActFlag = false;
  136. }
  137. if (merchant.IsAct == 0) ActFlag = false;
  138. var ledgerModel = ActFlag ? "5" : "1";
  139. var infos = ConsumerOrdersUtil.AddConsumerOrders(MerchantId, ConsumerId, merchant.UserId, PayMode, PayMoney, SnNo, OrderNo, ActFlag ? (PayMoney * 0.99M) * merchantset.DiviPercent / 100 : 0, ActFlag ? 1u : 0u, ActFlag ? PayMoney * merchantset.GetPercent / 100 : PayMoney, Newtonsoft.Json.JsonConvert.SerializeObject(merchantset), merchantset.Version);
  140. ConsumerOrderForNoUtil.AddConsumerOrderForNo(OrderNo, int.Parse(infos.Data.ToString()));
  141. if (PayMode == 1)
  142. {
  143. string backString = HaoDaHelper.Instance.Alipay(merchantAdd.OutMchtNo, merchantAdd.StoreNo, OrderNo, PayMoney, SpHost + "/api/alipay/hdnotice", openid, function.get_Random(4), ledgerModel, merchant.BrandId);
  144. JsonData obj = JsonMapper.ToObject(backString);
  145. if (obj["resultCode"].ToString() == "1")
  146. {
  147. string tradeNo = obj["prePayId"].ToString();
  148. Obj.Add("respCode", obj["resultCode"].ToString());
  149. Obj.Add("tradeNo", tradeNo);
  150. }
  151. else if (obj["resultCode"].ToString() == "0" && obj["errorCode"].ToString().Contains("OL-6"))
  152. {
  153. return Json(new AppResultJson() { Status = "-1", Info = "错误码:FK011", Data = Obj });
  154. }
  155. else
  156. {
  157. Obj.Add("respCode", obj["resultCode"].ToString());
  158. Obj.Add("tradeNo", "");
  159. return Json(new AppResultJson() { Status = "-1", Info = obj["errorDesc"].ToString(), Data = Obj });
  160. }
  161. }
  162. else if (PayMode == 2)
  163. {
  164. var dic = JsonMapper.ToObject(HaoDaHelper.Instance.WeChatPay(merchantAdd.OutMchtNo, merchantAdd.StoreNo, OrderNo, PayMoney, SpHost + "/api/wechat/hdnotice", openid, function.get_Random(4), AppConfig.WeChat.AppId, "MINI_PROGRAM", ledgerModel, merchant.BrandId));
  165. if (dic["resultCode"].ToString() == "1")
  166. {
  167. Obj.Add("appId", dic["payInfo"]["appId"].ToString()); //微信小程序appid
  168. Obj.Add("timeStamp", dic["payInfo"]["timeStamp"].ToString()); //时间戳
  169. Obj.Add("nonceStr", dic["payInfo"]["nonceStr"].ToString()); //随机字符串
  170. Obj.Add("package", dic["payInfo"]["wxPackage"].ToString()); //统一支付接口返回的prepayid参数值
  171. Obj.Add("paySign", dic["payInfo"]["paySign"].ToString()); //支付签名
  172. Obj.Add("ConsumerId", ConsumerId);
  173. }
  174. else if (dic["resultCode"].ToString() == "0" && dic["errorCode"].ToString().Contains("OL-6"))
  175. {
  176. return Json(new AppResultJson() { Status = "-1", Info = "错误码:FK011", Data = Obj });
  177. }
  178. else
  179. {
  180. Obj.Add("respCode", dic["resultCode"].ToString());
  181. Obj.Add("tradeNo", "");
  182. return Json(new AppResultJson() { Status = "-1", Info = dic["errorDesc"].ToString(), Data = Obj });
  183. }
  184. }
  185. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  186. }
  187. #endregion
  188. #region 经营数据-统计数据-经营详情-取消返现
  189. // [Authorize]
  190. public JsonResult CancelDivi(string value)
  191. {
  192. value = DesDecrypt(value);
  193. JsonData data = JsonMapper.ToObject(value);
  194. AppResultJson result = CancelDiviDo(value);
  195. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  196. }
  197. private AppResultJson CancelDiviDo(string value)
  198. {
  199. JsonData data = JsonMapper.ToObject(value);
  200. Dictionary<string, object> Obj = new Dictionary<string, object>();
  201. int Id = int.Parse(function.CheckInt(data["id"].ToString()));
  202. ConsumerOrders order = maindb.ConsumerOrders.FirstOrDefault(m => m.Id == Id) ?? new ConsumerOrders();
  203. if (order.Id > 0)
  204. {
  205. List<ConsumerOrders> suborders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, 1, 999999);
  206. if (suborders.Count > 0)
  207. {
  208. ConsumerOrders suborder = suborders.FirstOrDefault(m => m.Id == Id);
  209. if (suborder != null)
  210. {
  211. order.CurDivi = suborder.CurDivi;
  212. maindb.SaveChanges();
  213. RedisDbconn.Instance.RemoveFromList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, suborder);
  214. function.WriteLog(DateTime.Now.ToString() + "\r\n" + "订单Id:" + suborder.Id + "\r\n\r\n", "银联订单取消返现");
  215. order.ReturnFlag = 0;
  216. maindb.SaveChanges();
  217. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  218. }
  219. }
  220. }
  221. else
  222. {
  223. return new AppResultJson() { Status = "-1", Info = "取消失败", Data = Obj };
  224. }
  225. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  226. }
  227. #endregion
  228. #region 经营数据-统计数据-经营详情-恢复返现
  229. // [Authorize]
  230. public JsonResult RestoreDivi(string value)
  231. {
  232. value = DesDecrypt(value);
  233. JsonData data = JsonMapper.ToObject(value);
  234. AppResultJson result = RestoreDiviDo(value);
  235. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  236. }
  237. public AppResultJson RestoreDiviDo(string value)
  238. {
  239. JsonData data = JsonMapper.ToObject(value);
  240. Dictionary<string, object> Obj = new Dictionary<string, object>();
  241. int Id = int.Parse(function.CheckInt(data["id"].ToString()));
  242. ConsumerOrders order = maindb.ConsumerOrders.FirstOrDefault(m => m.Id == Id) ?? new ConsumerOrders();
  243. if (order.Id > 0)
  244. {
  245. JsonData ProfitInfo = JsonMapper.ToObject(order.SeoDescription);
  246. int ProfitDays = int.Parse(function.CheckInt(ProfitInfo["ProfitDays"].ToString())); //活动有效时间
  247. if (order.IsAct == 0) return new AppResultJson() { Status = "-1", Info = "恢复失败,非活动订单不能进行该操作", Data = Obj };
  248. if (DateTime.Parse(order.UpdateDate.ToString()).AddMinutes(10) >= DateTime.Now) return new AppResultJson() { Status = "-1", Info = "恢复失败,订单完成支付10分钟内不能进行该操作", Data = Obj };
  249. if (order.MaxDivi == order.CurDivi) return new AppResultJson() { Status = "-1", Info = "恢复失败,订单已完成所有返现", Data = Obj };
  250. if (DateTime.Parse(order.UpdateDate.ToString()).AddDays(ProfitDays) < DateTime.Now) return new AppResultJson() { Status = "-1", Info = "恢复失败,非活动订单不能进行该操作", Data = Obj };
  251. List<ConsumerOrders> suborders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId);
  252. if (suborders.Count > 0)
  253. {
  254. ConsumerOrders suborder = suborders.FirstOrDefault(m => m.Id == Id);
  255. if (suborder == null)
  256. {
  257. RedisDbconn.Instance.AddRightList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, order);
  258. function.WriteLog(DateTime.Now.ToString() + "\r\n" + "订单Id:" + Id + "\r\n\r\n", "银联订单恢复返现");
  259. order.ReturnFlag = 1;
  260. maindb.SaveChanges();
  261. }
  262. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  263. }
  264. }
  265. else
  266. {
  267. return new AppResultJson() { Status = "-1", Info = "恢复失败", Data = Obj };
  268. }
  269. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  270. }
  271. #endregion
  272. }
  273. }