You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
6.7 KiB

2 years ago
#region Apache License Version 2.0
/*----------------------------------------------------------------
Copyright 2023 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions
and limitations under the License.
Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
----------------------------------------------------------------*/
#endregion Apache License Version 2.0
/*----------------------------------------------------------------
Copyright (C) 2022 Senparc
TenPayV3Util.cs
V3
Senparc - 20150211
Senparc - 20150303
Senparc - 20161014
TenPayUtil.BuildRandomStr()
Senparc - 20170516
v14.4.8 1TenPayLibV3.GetNoncestr()
2BuildRandomStr()
Senparc - 20170522
v14.4.9 TenPayUtil.GetNoncestr()GBKUTF8
Senparc - 20180331
v14.4.9 TenPayUtil.GetNoncestr()GBKUTF8
----------------------------------------------------------------*/
using System;
using System.Text;
using System.Net;
using Senparc.Weixin.Helpers;
using Senparc.CO2NET.Helpers;
namespace Senparc.Weixin.TenPayV3
{
/// <summary>
/// 微信支付工具类
/// </summary>
public class TenPayV3Util
{
public static Random random = new Random();
/// <summary>
/// 随机生成Noncestr
/// </summary>
/// <returns></returns>
public static string GetNoncestr()
{
return EncryptHelper.GetMD5(Guid.NewGuid().ToString(), "UTF-8");
}
/// <summary>
/// 获取微信时间格式
/// </summary>
/// <returns></returns>
public static string GetTimestamp()
{
TimeSpan ts = SystemTime.Now - new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
/// <summary>
/// 对字符串进行URL编码
/// </summary>
/// <param name="instr"></param>
/// <param name="charset"></param>
/// <returns></returns>
public static string UrlEncode(string instr, string charset)
{
//return instr;
if (instr == null || instr.Trim() == "")
return "";
else
{
//string res;
try
{
return WebUtility.UrlEncode(instr);
}
catch (Exception ex)
{
return WebUtility.UrlEncode(instr);
}
//return res;
}
}
/// <summary>
/// 对字符串进行URL解码
/// </summary>
/// <param name="instr"></param>
/// <param name="charset"></param>
/// <returns></returns>
public static string UrlDecode(string instr, string charset)
{
if (instr == null || instr.Trim() == "")
return "";
else
{
//string res;
try
{
return WebUtility.UrlDecode(instr);
}
catch (Exception ex)
{
return WebUtility.UrlDecode(instr);
}
//return res;
}
}
/// <summary>
/// 取时间戳生成随即数,替换交易单号中的后10位流水号
/// </summary>
/// <returns></returns>
public static UInt32 UnixStamp()
{
TimeSpan ts = SystemTime.Now - new DateTimeOffset(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
return Convert.ToUInt32(ts.TotalSeconds);
}
/// <summary>
/// 取随机数
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string BuildRandomStr(int length)
{
int num;
lock (random)
{
num = random.Next();
}
string str = num.ToString();
if (str.Length > length)
{
str = str.Substring(0, length);
}
else if (str.Length < length)
{
int n = length - str.Length;
while (n > 0)
{
str = str.Insert(0, "0");
n--;
}
}
return str;
}
/// <summary>
/// 创建当天内不会重复的数字
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string BuildDailyRandomStr(int length)
{
var stringFormat = SystemTime.Now.ToString("HHmmss0000");//共10位
return stringFormat;
}
/// <summary>
/// 对退款通知消息进行解密
/// </summary>
/// <param name="reqInfo"></param>
/// <param name="mchKey"></param>
/// <returns></returns>
public static string DecodeRefundReqInfo(string reqInfo, string mchKey)
{
//参考文档https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_16&index=11
/*
1Abase64B
2keymd532key* ( key(pay.weixin.qq.com)-->-->API--> )
3key*BAES-256-ECBPKCS7Padding
*/
//var base64Encode = Encoding.UTF8.GetString(Convert.FromBase64String(reqInfo));//(1)
var base64Encode = reqInfo;//(1) EncryptHelper.AESDecrypt 方法内部会进行一次base64解码因此这里不再需要解码
var md5Str = EncryptHelper.GetLowerMD5(mchKey, Encoding.UTF8);//(2)
var result = EncryptHelper.AESDecrypt(base64Encode, md5Str);//(3)
return result;
}
}
}