微信支付JSAPI,V3版本,签名方法1

最近走了很多坑,记录下:

#region 微信支付基本信息
        public const string MchId = "1699999999";
        public const string ApiV3Key = "asdfghjab639cb399cb371cbd893e15e";
        //APIV2秘钥:ilrve9cab639cb39tcb371cbd893e18a   调用APIv2时,需用APIv2密钥签名,从而界定你的身份,需妥善保管防止泄露 (APIv2密钥与APIv3密钥互不影响)
        //APIV3秘钥:asdfghjab639cb399cb371cbd893e15e   调用APIv3的下载平台证书接口、处理回调通知中报文时,要通过该密钥来解密信息
        public const string AppId = "wx2fba12345806c75d";//开发者ID(AppID)
        public const string P12Password = "1699999999";
        public const string CertSerialNumber = "1810297430A25BC5254854585F2B748FE33789EB";//证书序列号
        #endregion

        #region 签名方法1:使用商户私钥签名:apiclient_cert.p12(调用下单接口  +   JSAPI调起支付)
        /// <summary>
        /// 签名方法1:使用商户私钥签名:apiclient_cert.p12(调用下单接口  +   JSAPI调起支付)
        /// </summary>
        /// <param name="message">需要签名的数据</param>
        public static string GenerateSignatureA(string message)
        {
            string P12FilePath = HttpContext.Current.Server.MapPath("~/Content/apiclient_cert.p12");
            X509Certificate2 cert = null;//new X509Certificate2(P12FilePath, P12Password, X509KeyStorageFlags.Exportable);
            try
            {
                RSA rsa = null;
                try// 加载p12证书
                {
                    cert = new X509Certificate2(P12FilePath, P12Password, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
                    rsa = cert.GetRSAPrivateKey();
                }
                catch (Exception)
                {
                    cert = new X509Certificate2(P12FilePath, P12Password, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.DefaultKeySet);
                    rsa = cert.GetRSAPrivateKey();
                }
                if (rsa != null)
                {
                    var signature = Convert.ToBase64String(rsa.SignData(Encoding.UTF8.GetBytes(message), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
                    return signature;
                }
                else
                {
                    //throw new InvalidOperationException("无法从p12证书中获取RSA私钥");
                    SLogText.WriteLog("签名A:RSA空null:", "RSA rsa = null");
                    return "";
                }
            }
            catch (Exception ex)
            {
                // 这里可以记录日志或抛出更具体的异常
                SLogText.WriteLog("签名A:报错:", ex.ToString() + ex.Message);
                //throw new Exception("签名微信支付请求时出错", ex);
                return "";
            }
        }
        #endregion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值