AppUtil.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace PaymentGateway.GatewayApp
  9. {
  10. public class AllInPayAppConstants
  11. {
  12. public static string APPID = "00144379";
  13. public static string CUSID = "990440648166000";
  14. public static string APPKEY = "e10adc3949ba59abbe56e057f20f883e";
  15. public static string APIVERSION = "11";
  16. public static string API_URL = "https://vsp.allinpay.com/apiweb/unitorder";
  17. public static string NOTIFY_URL = "";
  18. }
  19. public class AllInPayAppUtil
  20. {
  21. /// <summary>
  22. /// 将参数排序组装
  23. /// </summary>
  24. /// <param name="param"></param>
  25. /// <returns></returns>
  26. public static String BuildParamStr(Dictionary<String, String> param)
  27. {
  28. if (param == null || param.Count == 0)
  29. {
  30. return "";
  31. }
  32. Dictionary<String, String> ascDic = param.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  33. StringBuilder sb = new StringBuilder();
  34. foreach (var item in ascDic)
  35. {
  36. if (!string.IsNullOrEmpty(item.Value))
  37. {
  38. sb.Append(item.Key).Append("=").Append(item.Value).Append("&");
  39. }
  40. }
  41. return sb.ToString().Substring(0, sb.ToString().Length - 1);
  42. }
  43. public static String BuildParamStr(Dictionary<String, object> param)
  44. {
  45. if (param == null || param.Count == 0)
  46. {
  47. return "";
  48. }
  49. Dictionary<String, object> ascDic = param.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  50. StringBuilder sb = new StringBuilder();
  51. foreach (var item in ascDic)
  52. {
  53. if (item.Value != null)
  54. {
  55. if (item.Value is string)
  56. {
  57. sb.Append(item.Key).Append("=").Append(item.Value).Append("&");
  58. }
  59. else if (item.Value is List<ConsumeType>)
  60. {
  61. sb.Append(item.Key).Append("=").Append(JsonConvert.SerializeObject(item.Value)).Append("&");
  62. }
  63. else if (item.Value is OilStation)
  64. {
  65. sb.Append(item.Key).Append("=").Append(JsonConvert.SerializeObject(item.Value)).Append("&");
  66. }
  67. else
  68. {
  69. sb.Append(item.Key).Append("=").Append(JsonConvert.SerializeObject(item.Value)).Append("&");
  70. // unexpected type
  71. }
  72. }
  73. }
  74. return sb.ToString().Substring(0, sb.ToString().Length - 1);
  75. }
  76. public static String signParam(Dictionary<String, String> param, String appkey)
  77. {
  78. if (param == null || param.Count == 0)
  79. {
  80. return "";
  81. }
  82. param.Add("key", appkey);
  83. String blankStr = BuildParamStr(param);
  84. return MD5Encrypt(blankStr);
  85. }
  86. public static String signParam(Dictionary<String, object> param, String appkey, bool lowerCase = false)
  87. {
  88. if (param == null || param.Count == 0)
  89. {
  90. return "";
  91. }
  92. param.Add("key", appkey);
  93. string blankStr = BuildParamStr(param);
  94. return lowerCase ? MD5Encrypt(blankStr).ToLower() : MD5Encrypt(blankStr);
  95. }
  96. public static bool validSign(Dictionary<String, String> param, String appkey)
  97. {
  98. String signRsp = param["sign"];
  99. param.Remove("sign");
  100. String sign = signParam(param, AllInPayAppConstants.APPKEY);
  101. return sign.ToLower().Equals(signRsp.ToLower());
  102. }
  103. /// <summary>
  104. /// 将实体转化为json
  105. /// </summary>
  106. /// <param name="o"></param>
  107. /// <returns></returns>
  108. public static string ObjectToJson(object o)
  109. {
  110. string json = JsonConvert.SerializeObject(o);
  111. return json;
  112. }
  113. /// <summary>
  114. /// md5加签
  115. /// </summary>
  116. /// <param name="strText"></param>
  117. /// <returns></returns>
  118. public static string MD5Encrypt(string strText)
  119. {
  120. MD5 md5 = new MD5CryptoServiceProvider();
  121. //var str1 = "appId=test&bizContent={\"amount\":1,\"backUrl\":\"http://www.baidu.com\",\"bizOrderNo\":\"1607592344575\",\"consumeTypes\":[{\"amount\":1,\"type\":\"C0001\"}],\"limitPay\":\"no_credit\",\"oilStation\":{\"oilGunNo\":\"1\",\"oilStationNo\":\"S0012\",\"oilStationPersonNo\":\"\"},\"payType\":\"SCAN_WEIXIN\",\"remark\":\"\",\"shiftsMask\":\"S0012001\",\"shiftsTime\":\"20201210\",\"sysId\":\"1902271423530473681\"}&charset=utf-8&key=123456&service=oil.pay.apply&signType=MD5&timestamp=20201210172545&version=1.0";
  122. //byte[] result1 = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str1));
  123. //var s = BitConverter.ToString(result1).Replace("-", "");
  124. byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strText));
  125. var resultStr = BitConverter.ToString(result).Replace("-", "");
  126. return resultStr;
  127. }
  128. }
  129. }