ConsumerOrdersController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. if (merchant.IsAct == 0)
  95. {
  96. return Json(new AppResultJson() { Status = "-1", Info = "支付失败,商户尚未激活,请前往来客吧商户版激活后使用" });
  97. }
  98. if (PayMode == 1)
  99. {
  100. openid = new AlipayFunction(_accessor.HttpContext).GetAlipayUserId(Code);
  101. if (openid.Contains("|"))
  102. {
  103. openid = openid.Split('|')[0];
  104. }
  105. }
  106. else
  107. {
  108. 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");
  109. function.WriteLog(DateTime.Now.ToString() + "\n" + result, "微信小程序获取openid");
  110. JsonData jsonObj = JsonMapper.ToObject(result);
  111. openid = jsonObj["openid"].ToString();
  112. }
  113. int ConsumerId = 0;
  114. // var check = ConsumerOpenIdsService.Exist(openid);
  115. var check = ConsumersService.Exist(openid);
  116. if (check == false)
  117. {
  118. var info = ConsumersUtil.AddConsumers(ConsumerId, openid);
  119. ConsumerId = int.Parse(info.Data.ToString());
  120. // ConsumerOpenIdsUtil.AddConsumerOpenIds(ConsumerId, openid);
  121. }
  122. else
  123. {
  124. // var info = ConsumerOpenIdsService.Query(" OpenId='" + openid + "'");
  125. // ConsumerId = info.ConsumerId;
  126. var info = ConsumersService.Query(" WechatOpenId='" + openid + "'");
  127. ConsumerId = info.Id;
  128. }
  129. Dictionary<string, object> Obj = new Dictionary<string, object>();
  130. string OrderNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  131. bool ActFlag = merchantset.IsAll == 1 ? false : true;
  132. if (PayMoney < merchantset.MinPayMoney) //支付金额小于活动最小金额,则不分账
  133. {
  134. ActFlag = false;
  135. }
  136. var ledgerModel = ActFlag ? "5" : "1";
  137. 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);
  138. ConsumerOrderForNoUtil.AddConsumerOrderForNo(OrderNo, int.Parse(infos.Data.ToString()));
  139. if (PayMode == 1)
  140. {
  141. string backString = HaoDaHelper.Instance.Alipay(merchantAdd.OutMchtNo, merchantAdd.StoreNo, OrderNo, PayMoney, SpHost + "/api/alipay/hdnotice", openid, function.get_Random(4), ledgerModel);
  142. JsonData obj = JsonMapper.ToObject(backString);
  143. if (obj["resultCode"].ToString() == "1")
  144. {
  145. string tradeNo = obj["prePayId"].ToString();
  146. Obj.Add("respCode", obj["resultCode"].ToString());
  147. Obj.Add("tradeNo", tradeNo);
  148. }
  149. else if (obj["resultCode"].ToString() == "0" && obj["errorCode"].ToString().Contains("OL-6"))
  150. {
  151. return Json(new AppResultJson() { Status = "-1", Info = "错误码:FK011", Data = Obj });
  152. }
  153. else
  154. {
  155. Obj.Add("respCode", obj["resultCode"].ToString());
  156. Obj.Add("tradeNo", "");
  157. return Json(new AppResultJson() { Status = "-1", Info = obj["errorDesc"].ToString(), Data = Obj });
  158. }
  159. }
  160. else if (PayMode == 2)
  161. {
  162. 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));
  163. if (dic["resultCode"].ToString() == "1")
  164. {
  165. Obj.Add("appId", dic["payInfo"]["appId"].ToString()); //微信小程序appid
  166. Obj.Add("timeStamp", dic["payInfo"]["timeStamp"].ToString()); //时间戳
  167. Obj.Add("nonceStr", dic["payInfo"]["nonceStr"].ToString()); //随机字符串
  168. Obj.Add("package", dic["payInfo"]["wxPackage"].ToString()); //统一支付接口返回的prepayid参数值
  169. Obj.Add("paySign", dic["payInfo"]["paySign"].ToString()); //支付签名
  170. Obj.Add("ConsumerId", ConsumerId);
  171. }
  172. else if (dic["resultCode"].ToString() == "0" && dic["errorCode"].ToString().Contains("OL-6"))
  173. {
  174. return Json(new AppResultJson() { Status = "-1", Info = "错误码:FK011", Data = Obj });
  175. }
  176. else
  177. {
  178. Obj.Add("respCode", dic["resultCode"].ToString());
  179. Obj.Add("tradeNo", "");
  180. return Json(new AppResultJson() { Status = "-1", Info = dic["errorDesc"].ToString(), Data = Obj });
  181. }
  182. }
  183. return Json(new AppResultJson() { Status = "1", Info = "", Data = Obj });
  184. }
  185. #endregion
  186. #region 经营数据-统计数据-经营详情-取消返现
  187. // [Authorize]
  188. public JsonResult CancelDivi(string value)
  189. {
  190. value = DesDecrypt(value);
  191. JsonData data = JsonMapper.ToObject(value);
  192. AppResultJson result = CancelDiviDo(value);
  193. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  194. }
  195. private AppResultJson CancelDiviDo(string value)
  196. {
  197. JsonData data = JsonMapper.ToObject(value);
  198. Dictionary<string, object> Obj = new Dictionary<string, object>();
  199. int Id = int.Parse(function.CheckInt(data["id"].ToString()));
  200. ConsumerOrders order = maindb.ConsumerOrders.FirstOrDefault(m => m.Id == Id);
  201. if(order != null)
  202. {
  203. List<ConsumerOrders> suborders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId);
  204. if(suborders.Count > 0)
  205. {
  206. ConsumerOrders suborder = suborders.FirstOrDefault(m => m.Id == Id);
  207. if(suborder != null)
  208. {
  209. order.CurDivi = suborder.CurDivi;
  210. maindb.SaveChanges();
  211. RedisDbconn.Instance.RemoveFromList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, suborder);
  212. function.WriteLog(DateTime.Now.ToString() + "\r\n" + "订单Id:" + suborder.Id + "\r\n\r\n", "银联订单取消返现");
  213. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  214. }
  215. }
  216. }
  217. return new AppResultJson() { Status = "-1", Info = "取消失败", Data = Obj };
  218. }
  219. #endregion
  220. #region 经营数据-统计数据-经营详情-恢复返现
  221. // [Authorize]
  222. public JsonResult RestoreDivi(string value)
  223. {
  224. value = DesDecrypt(value);
  225. JsonData data = JsonMapper.ToObject(value);
  226. AppResultJson result = RestoreDiviDo(value);
  227. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  228. }
  229. private AppResultJson RestoreDiviDo(string value)
  230. {
  231. JsonData data = JsonMapper.ToObject(value);
  232. Dictionary<string, object> Obj = new Dictionary<string, object>();
  233. int Id = int.Parse(function.CheckInt(data["id"].ToString()));
  234. ConsumerOrders order = maindb.ConsumerOrders.FirstOrDefault(m => m.Id == Id);
  235. if(order != null)
  236. {
  237. List<ConsumerOrders> suborders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId);
  238. if(suborders.Count > 0)
  239. {
  240. ConsumerOrders suborder = suborders.FirstOrDefault(m => m.Id == Id);
  241. if(suborder == null)
  242. {
  243. RedisDbconn.Instance.AddRightList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, order);
  244. function.WriteLog(DateTime.Now.ToString() + "\r\n" + "订单Id:" + suborder.Id + "\r\n\r\n", "银联订单恢复返现");
  245. }
  246. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  247. }
  248. }
  249. return new AppResultJson() { Status = "-1", Info = "恢复失败", Data = Obj };
  250. }
  251. #endregion
  252. }
  253. }