PayController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using Microsoft.AspNetCore.Authorization;
  10. using System.Web;
  11. using MySystem.Models.KxsMainModels;
  12. using LitJson;
  13. using Library;
  14. namespace MySystem.Areas.Api.Controllers.v1.pos
  15. {
  16. [Area("Api")]
  17. [Route("Api/v1/[controller]/[action]")]
  18. public class PayController : BaseController
  19. {
  20. public PayController(IHttpContextAccessor accessor) : base(accessor)
  21. {
  22. }
  23. #region 商户激活-确认支付
  24. // [Authorize]
  25. public JsonResult Pay(string value)
  26. {
  27. value = DesDecrypt(value);
  28. JsonData data = JsonMapper.ToObject(value);
  29. AppResultJson result = PayDo(value);
  30. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  31. }
  32. public AppResultJson PayDo(string value)
  33. {
  34. JsonData data = JsonMapper.ToObject(value);
  35. int PayMode = int.Parse(function.CheckInt(data["PayMode"].ToString())); //支付方式(1 支付宝)
  36. Dictionary<string, object> Obj = new Dictionary<string, object>();
  37. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString()));
  38. int Id = int.Parse(function.CheckInt(data["Id"].ToString()));
  39. Orders query = kxsdb.Orders.FirstOrDefault(m => m.Id == Id);
  40. if (query != null)
  41. {
  42. query.PayMode = PayMode; //支付方式
  43. kxsdb.SaveChanges();
  44. string PayData = "";
  45. PublicAccountSet set = new AlipayFunctionForKxs(_accessor.HttpContext).SetData(MerchantId);
  46. if (PayMode == 1)
  47. {
  48. string TotalPrice = query.TotalPrice.ToString();
  49. function.WriteLog(query.OrderNo, "支付宝支付日志");
  50. function.WriteLog(TotalPrice.ToString(), "支付宝支付日志");
  51. function.WriteLog("商户激活—确认订单", "支付宝支付日志");
  52. string ProductName = "商户激活—确认订单";
  53. PayData = new Alipay.AlipayPublicClass(_accessor.HttpContext).GetAlipayInfo(query.OrderNo, TotalPrice, ProductName, set.AlipayAppId, set.AlipayPrivateKey, SourceHost + "/Api/Alipay/NoticePay");
  54. function.WriteLog(PayData, "支付宝支付日志");
  55. }
  56. Obj.Add("PayData", PayData); //支付宝微信SDK所需数据
  57. }
  58. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  59. }
  60. #endregion
  61. #region 商户激活—确认订单
  62. // [Authorize]
  63. public JsonResult ConfirmOrder(string value)
  64. {
  65. value = DesDecrypt(value);
  66. JsonData data = JsonMapper.ToObject(value);
  67. AppResultJson result = ConfirmOrderDo(value);
  68. return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
  69. }
  70. public AppResultJson ConfirmOrderDo(string value)
  71. {
  72. function.WriteLog(value, "商户激活—确认订单");
  73. JsonData data = JsonMapper.ToObject(value);
  74. int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户Id
  75. Dictionary<string, object> Obj = new Dictionary<string, object>();
  76. string OrderNo = "SHJH" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  77. string check = RedisDbconn.Instance.Get<string>("ConfirmOrder:" + MerchantId + "_" + OrderNo);
  78. if (!string.IsNullOrEmpty(check))
  79. {
  80. return new AppResultJson() { Status = "-1", Info = "请勿重复下单!" };
  81. }
  82. RedisDbconn.Instance.Set("ConfirmOrder:" + MerchantId + "_" + OrderNo, "1");
  83. RedisDbconn.Instance.SetExpire("ConfirmOrder:" + MerchantId + "_" + OrderNo, 60);
  84. decimal ActPayPrice = 365; //服务费总额
  85. // MerchantActOrders query = maindb.MerchantActOrders.Add(new MerchantActOrders()
  86. // {
  87. // OrderNo = OrderNo,
  88. // UserId = UserId, //创客
  89. // MerchantId = MerchantId, //商户Id
  90. // ActPayPrice = ActPayPrice, //服务费总额
  91. // }).Entity;
  92. // maindb.SaveChanges();
  93. // Obj.Add("Id", query.Id); //Id
  94. // Obj.Add("CreateDate", query.CreateDate); //创建时间
  95. return new AppResultJson() { Status = "1", Info = "", Data = Obj };
  96. }
  97. #endregion
  98. #region 检查签名是否合法,合法返回1,不合法返回提示信息
  99. /// <summary>
  100. /// 检查签名是否合法,合法返回1,不合法返回提示信息
  101. /// </summary>
  102. /// <param name="value">请求的参数(json字符串)</param>
  103. /// <param name="signField">要签名的字段</param>
  104. /// <returns></returns>
  105. private string CheckSign(string value, string[] signField)
  106. {
  107. JsonData json = JsonMapper.ToObject(value);
  108. Dictionary<string, string> dic = new Dictionary<string, string>();
  109. for (int i = 0; i < signField.Length; i++)
  110. {
  111. dic.Add(signField[i], json[signField[i]].ToString());
  112. }
  113. string sign = json["sign"].ToString(); //客户端签名字符串
  114. return new Sign().sign(dic, sign);
  115. }
  116. #endregion
  117. }
  118. }