WeChatPayBackService.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using MySystem.PxcModels;
  7. using Org.BouncyCastle.Crypto.Modes;
  8. using Org.BouncyCastle.Crypto.Engines;
  9. using Org.BouncyCastle.Crypto.Parameters;
  10. using System.Text;
  11. namespace MySystem
  12. {
  13. public class WeChatPayBackService
  14. {
  15. public readonly static WeChatPayBackService Instance = new WeChatPayBackService();
  16. private WeChatPayBackService()
  17. { }
  18. public void Start(JobMqMsg jobInfo)
  19. {
  20. string content = "";
  21. try
  22. {
  23. dosomething();
  24. // string Msg = "success";
  25. // jobInfo.Status = Msg == "success" ? 1 : 0;
  26. // jobInfo.Msg = Msg == "success" ? "执行完成" : Msg;
  27. // RabbitMQClient.Instance.SendMsg(Newtonsoft.Json.JsonConvert.SerializeObject(jobInfo), "PublicBack");
  28. }
  29. catch (Exception ex)
  30. {
  31. if (!string.IsNullOrEmpty(content))
  32. {
  33. Dictionary<string, string> data = new Dictionary<string, string>();
  34. data.Add("ErrTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  35. data.Add("ErrMsg", ex.ToString());
  36. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(data), "public_err");
  37. }
  38. else
  39. {
  40. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
  41. }
  42. }
  43. }
  44. public void dosomething()
  45. {
  46. bool op = true;
  47. while (op)
  48. {
  49. string content = RedisDbconn.Instance.RPop<string>("WeChatPayBack");
  50. if (!string.IsNullOrEmpty(content))
  51. {
  52. JsonData jsonObj = JsonMapper.ToObject(content);
  53. if (jsonObj.Count > 0)
  54. {
  55. if (jsonObj["event_type"].ToString() == "TRANSACTION.SUCCESS")
  56. {
  57. JsonData resource = jsonObj["resource"];
  58. //开始解密
  59. string WxPayResourceDecryptModel = AesGcmDecrypt(resource["associated_data"].ToString(), resource["nonce"].ToString(), resource["ciphertext"].ToString());
  60. // {\"sp_mchid\":\"1611167423\",\"sub_mchid\":\"1622024882\",\"sp_appid\":\"wxe2c051b3e46c0f6f\",\"out_trade_no\":\"2022022621562926396898863\",\"transaction_id\":\"4200001412202202267619496496\",\"trade_type\":\"JSAPI\",\"trade_state\":\"SUCCESS\",\"trade_state_desc\":\"支付成功\",\"bank_type\":\"OTHERS\",\"attach\":\"\",\"success_time\":\"2022-02-26T21:56:42+08:00\",\"payer\":{\"sp_openid\":\"omawy5W6jb0pgPfuKUVs6K3bEhzk\",\"sub_openid\":\"\"},\"amount\":{\"total\":1,\"payer_total\":1,\"currency\":\"CNY\",\"payer_currency\":\"CNY\"}}
  61. JsonData orderObj = JsonMapper.ToObject(WxPayResourceDecryptModel);
  62. string OrderNo = orderObj["out_trade_no"].ToString();
  63. WebCMSEntities db = new WebCMSEntities();
  64. ConsumerOrderForNo forNo = db.ConsumerOrderForNo.FirstOrDefault(m => m.OrderNo == OrderNo);
  65. if (forNo != null)
  66. {
  67. ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == forNo.OrderIds && m.Status == 0);
  68. if (order != null)
  69. {
  70. order.Status = 1;
  71. order.UpdateDate = DateTime.Now;
  72. order.PayMoney = order.PayMoney;
  73. order.MaxDivi = order.MaxDivi;
  74. db.SaveChanges();
  75. RedisDbconn.Instance.AddList("ConsumerOrdersStat", order.Id);
  76. RedisDbconn.Instance.AddList("ConsumerOrders:Divi:List", order.Id.ToString());
  77. RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:" + order.MerchantId, order);
  78. // ConsumerOrdersStatService.Instance.Stat(order);
  79. }
  80. }
  81. db.Dispose();
  82. }
  83. }
  84. }
  85. else
  86. {
  87. op = false;
  88. }
  89. }
  90. }
  91. private string ALGORITHM = "AES/GCM/NoPadding";
  92. private int TAG_LENGTH_BIT = 128;
  93. private int NONCE_LENGTH_BYTE = 12;
  94. private string AES_KEY = "VyULlqfAW2gBfCNfLdupcL7zlha7d93F";//你的v3秘钥
  95. public string AesGcmDecrypt(string associatedData, string nonce, string ciphertext)
  96. {
  97. GcmBlockCipher gcmBlockCipher = new GcmBlockCipher(new AesEngine());
  98. AeadParameters aeadParameters = new AeadParameters(
  99. new KeyParameter(Encoding.UTF8.GetBytes(AES_KEY)),
  100. 128,
  101. Encoding.UTF8.GetBytes(nonce),
  102. Encoding.UTF8.GetBytes(associatedData));
  103. gcmBlockCipher.Init(false, aeadParameters);
  104. byte[] data = Convert.FromBase64String(ciphertext);
  105. byte[] plaintext = new byte[gcmBlockCipher.GetOutputSize(data.Length)];
  106. int length = gcmBlockCipher.ProcessBytes(data, 0, data.Length, plaintext, 0);
  107. gcmBlockCipher.DoFinal(plaintext, length);
  108. return Encoding.UTF8.GetString(plaintext);
  109. }
  110. }
  111. }