获得支付签名
2020-07-26 09:49 idea555 阅读(74) 评论(0) 收藏 举报/// <summary>
/// 统一下单参数类
/// </summary>
public class UnifiedOrder
{
/// <summary>
/// 公众账号ID
/// </summary>
public string appid { get; set;}
/// <summary>
/// 商户号
/// </summary>
public string mch_id { get; set; }
/// <summary>
/// 子商户公众账号ID
/// </summary>
public string sub_appid { get; set; }
/// <summary>
/// 微信支付分配的子商户号
/// </summary>
public string sub_mch_id { get; set; }
/// <summary>
/// 设备号
/// </summary>
public string device_info { get; set; }
/// <summary>
/// 随机字符串
/// </summary>
public string nonce_str{ get; set; }
/// <summary>
/// 签名,使用中文时,签名不正确
/// </summary>
public string sign { get; set; }
/// <summary>
/// 商品描述,使用中文时,签名不正确
/// </summary>
public string body{ get; set; }
/// <summary>
/// 商品详情
/// </summary>
public string detail{ get; set; }
/// <summary>
/// 附加数据
/// </summary>
public string attach{ get; set; }
/// <summary>
/// 商户订单号
/// </summary>
public string out_trade_no{ get; set; }
/// <summary>
/// 货币类型
/// </summary>
public string fee_type{ get; set; }
/// <summary>
/// 总金额,单位是分
/// </summary>
public string total_fee{ get; set; }
/// <summary>
/// 终端IP
/// </summary>
public string spbill_create_ip { get; set; }
/// <summary>
/// 交易起始时间
/// </summary>
public string time_start{ get; set; }
/// <summary>
/// 交易结束时间
/// </summary>
public string time_expire{ get; set; }
/// <summary>
/// 商品标记
/// </summary>
public string goods_tag{ get; set; }
/// <summary>
/// 通知地址
/// </summary>
public string notify_url{ get; set; }
/// <summary>
/// 交易类型,取值如下:JSAPI,NATIVE,APP,WAP
/// </summary>
public string trade_type{ get; set; }
/// <summary>
/// 商品ID
/// </summary>
public string product_id{ get; set; }
/// <summary>
/// 指定支付方式
/// </summary>
public string limit_pay{ get; set; }
/// <summary>
/// 用户标识
/// </summary>
public string openid{ get; set; }
#region 1.0 获得支付签名
/// <summary>
/// 获得支付签名(key代码微信支付秘钥,非公众平台秘钥)
/// </summary>
/// <returns></returns>
public string GetSign(string key)
{
//获得所有属性
Type unifiedOrderType = typeof(UnifiedOrder);
PropertyInfo[] propInfos = unifiedOrderType.GetProperties();
propInfos = propInfos.OrderBy(e => e.Name).ToArray();
string parasString = string.Empty;
//遍历属性,生成参数字符串
foreach (var propInfo in propInfos)
{
string value = propInfo.GetValue(this, null) as string;
if (!string.IsNullOrEmpty(value))
{
parasString += string.Format("{0}={1}&", propInfo.Name, value);
}
}
parasString += string.Format("key={0}", key);
//对参数字符串MD5加密
//byte[] bytes = Encoding.Default.GetBytes(parasString);
//byte[] hashedBytes = System.Security.Cryptography.MD5.Create().ComputeHash(bytes);
//string sign = BitConverter.ToString(hashedBytes).Replace("-", "").ToUpper();
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(parasString));
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
sign.Append(bytes[i].ToString("X2"));
}
return sign.ToString();
}
#endregion
浙公网安备 33010602011771号