ConsumerOrdersController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. var ledgerModel = ActFlag ? "5" : "1";
  138. 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);
  139. ConsumerOrderForNoUtil.AddConsumerOrderForNo(OrderNo, int.Parse(infos.Data.ToString()));
  140. if (PayMode == 1)
  141. {
  142. string backString = HaoDaHelper.Instance.Alipay(merchantAdd.OutMchtNo, merchantAdd.StoreNo, OrderNo, PayMoney, SpHost + "/api/alipay/hdnotice", openid, function.get_Random(4), ledgerModel);
  143. JsonData obj = JsonMapper.ToObject(backString);
  144. if (obj["resultCode"].ToString() == "1")
  145. {
  146. string tradeNo = obj["prePayId"].ToString();
  147. Obj.Add("respCode", obj["resultCode"].ToString());
  148. Obj.Add("tradeNo", tradeNo);
  149. }
  150. else if (obj["resultCode"].ToString() == "0" && obj["errorCode"].ToString().Contains("OL-6"))
  151. {
  152. return Json(new AppResultJson() { Status = "-1", Info = "错误码:FK011", Data = Obj });
  153. }
  154. else
  155. {
  156. Obj.Add("respCode", obj["resultCode"].ToString());
  157. Obj.Add("tradeNo", "");
  158. return Json(new AppResultJson() { Status = "-1", Info = obj["errorDesc"].ToString(), Data = Obj });
  159. }
  160. }
  161. else if (PayMode == 2)
  162. {
  163. 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));
  164. if (dic["resultCode"].ToString() == "1")
  165. {
  166. Obj.Add("appId", dic["payInfo"]["appId"].ToString()); //微信小程序appid
  167. Obj.Add("timeStamp", dic["payInfo"]["timeStamp"].ToString()); //时间戳
  168. Obj.Add("nonceStr", dic["payInfo"]["nonceStr"].ToString()); //随机字符串
  169. Obj.Add("package", dic["payInfo"]["wxPackage"].ToString()); //统一支付接口返回的prepayid参数值
  170. Obj.Add("paySign", dic["payInfo"]["paySign"].ToString()); //支付签名
  171. Obj.Add("ConsumerId", ConsumerId);
  172. }
  173. else if (dic["resultCode"].ToString() == "0" && dic["errorCode"].ToString().Contains("OL-6"))
  174. {
  175. return Json(new AppResultJson() { Status = "-1", Info = "错误码:FK011", Data = Obj });
  176. }
  177. else
  178. {
  179. Obj.Add("respCode", dic["resultCode"].ToString());
  180. Obj.Add("tradeNo", "");
  181. return Json(new AppResultJson() { Status = "-1", Info = dic["errorDesc"].ToString(), Data = Obj });
  182. }
  183. }
  184. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  185. }
  186. #endregion
  187. #region 经营数据-统计数据-经营详情-取消返现
  188. // [Authorize]
  189. public JsonResult CancelDivi(string value)
  190. {
  191. value = DesDecrypt(value);
  192. JsonData data = JsonMapper.ToObject(value);
  193. AppResultJson result = CancelDiviDo(value);
  194. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  195. }
  196. private AppResultJson CancelDiviDo(string value)
  197. {
  198. JsonData data = JsonMapper.ToObject(value);
  199. Dictionary<string, object> Obj = new Dictionary<string, object>();
  200. int Id = int.Parse(function.CheckInt(data["id"].ToString()));
  201. ConsumerOrders order = maindb.ConsumerOrders.FirstOrDefault(m => m.Id == Id);
  202. if (order != null)
  203. {
  204. List<ConsumerOrders> suborders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId);
  205. if (suborders.Count > 0)
  206. {
  207. ConsumerOrders suborder = suborders.FirstOrDefault(m => m.Id == Id);
  208. if (suborder != null)
  209. {
  210. order.CurDivi = suborder.CurDivi;
  211. maindb.SaveChanges();
  212. RedisDbconn.Instance.RemoveFromList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, suborder);
  213. function.WriteLog(DateTime.Now.ToString() + "\r\n" + "订单Id:" + suborder.Id + "\r\n\r\n", "银联订单取消返现");
  214. order.ReturnFlag = 0;
  215. maindb.SaveChanges();
  216. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  217. }
  218. }
  219. }
  220. return new AppResultJson() { Status = "-1", Info = "取消失败", Data = Obj };
  221. }
  222. #endregion
  223. #region 经营数据-统计数据-经营详情-恢复返现
  224. // [Authorize]
  225. public JsonResult RestoreDivi(string value)
  226. {
  227. value = DesDecrypt(value);
  228. JsonData data = JsonMapper.ToObject(value);
  229. AppResultJson result = RestoreDiviDo(value);
  230. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  231. }
  232. public AppResultJson RestoreDiviDo(string value)
  233. {
  234. JsonData data = JsonMapper.ToObject(value);
  235. Dictionary<string, object> Obj = new Dictionary<string, object>();
  236. int Id = int.Parse(function.CheckInt(data["id"].ToString()));
  237. ConsumerOrders order = maindb.ConsumerOrders.FirstOrDefault(m => m.Id == Id);
  238. if (order != null)
  239. {
  240. JsonData ProfitInfo = JsonMapper.ToObject(order.SeoDescription);
  241. int ProfitDays = int.Parse(function.CheckInt(ProfitInfo["ProfitDays"].ToString())); //活动有效时间
  242. if (order.IsAct == 0) return new AppResultJson() { Status = "-1", Info = "恢复失败,非活动订单不能进行该操作", Data = Obj };
  243. if (DateTime.Parse(order.UpdateDate.ToString()).AddMinutes(10) >= DateTime.Now) return new AppResultJson() { Status = "-1", Info = "恢复失败,订单完成支付10分钟内不能进行该操作", Data = Obj };
  244. if (order.MaxDivi == order.CurDivi) return new AppResultJson() { Status = "-1", Info = "恢复失败,订单已完成所有返现", Data = Obj };
  245. if (DateTime.Parse(order.UpdateDate.ToString()).AddDays(ProfitDays) < DateTime.Now) return new AppResultJson() { Status = "-1", Info = "恢复失败,非活动订单不能进行该操作", Data = Obj };
  246. List<ConsumerOrders> suborders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId);
  247. if (suborders.Count > 0)
  248. {
  249. ConsumerOrders suborder = suborders.FirstOrDefault(m => m.Id == Id);
  250. if (suborder == null)
  251. {
  252. RedisDbconn.Instance.AddRightList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, order);
  253. function.WriteLog(DateTime.Now.ToString() + "\r\n" + "订单Id:" + Id + "\r\n\r\n", "银联订单恢复返现");
  254. order.ReturnFlag = 1;
  255. maindb.SaveChanges();
  256. }
  257. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  258. }
  259. }
  260. return new AppResultJson() { Status = "-1", Info = "恢复失败", Data = Obj };
  261. }
  262. #endregion
  263. }
  264. }