Ejemplo n.º 1
0
        /// <summary>
        /// 生成 Authorization 头
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        protected async Task <string> BuildAuthAsync(HttpRequestMessage request)
        {
            string method = request.Method.ToString();
            string body   = "";

            if (method == "POST" || method == "PUT" || method == "PATCH")
            {
                var content = request.Content;
                body = await content.ReadAsStringAsync();
            }

            string uri       = request.RequestUri.PathAndQuery;
            var    timestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
            string nonce     = Path.GetRandomFileName();

            string message = $"{method}\n{uri}\n{timestamp}\n{nonce}\n{body}\n";
            ////此处重构待测试
            //string signature = TenPaySignHelper.CreateSign(message, privateKey);

            //return $"mchid=\"{merchantId}\",nonce_str=\"{nonce}\",timestamp=\"{timestamp}\",serial_no=\"{serialNo}\",signature=\"{signature}\"";

            //TODO:此处重构使用ISenparcWeixinSettingForTenpayV3
            string signature = TenPaySignHelper.CreateSign(message, _tenpayV3Setting.TenPayV3_PrivateKey);

            return($"mchid=\"{_tenpayV3Setting.TenPayV3_MchId}\",nonce_str=\"{nonce}\",timestamp=\"{timestamp}\",serial_no=\"{_tenpayV3Setting.TenPayV3_SerialNumber}\",signature=\"{signature}\"");
        }
Ejemplo n.º 2
0
        public void CreatePaySign_ByPrivateKeyTest()
        {
            var appId     = "wx8888888888888888";
            var timestamp = "1414561699";
            var nonceStr  = "5K8264ILTKCH16CQ2502SI8ZNMTM67VS";

            var result = TenPaySignHelper.CreatePaySign(timestamp, nonceStr, package, appId, privateKey);

            Assert.IsNotNull(result);
            Console.WriteLine(result);

            var exceptedResult = "POmTZCzk7fj+FeSwbU4rNghygFOzwpoaQt9SBW8blDAPZCVJ7wVnDVisx6t1ryyBpB3NmOwiNaT+hHi7YthYZzr0kvL5kWKSnpssyWBofnjqbFWBSV8JaFx7Ia2qnsgdVYALisYjLBr+bj69YXuyWiBxYFx+JylH6wW4w55Rziatoa4rwrdlrpgE2yRTxDu9wSZ4VCdUYSMj2ctyAy2fOiCcP00VGjihJWGCXXjeVm2YQyFZXB7KqGPhncdHaFmJzIvL8SbWKSc36cUKSuHhZ5n+oZVU8Vf+lb/eJibzTWxBIAJbtQplKojG48ukd7QFtRUd3b2EkOjzmeJ26zMlfA==";

            Assert.AreEqual(exceptedResult, result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 将返回的结果中的ciphertext进行AEAD_AES_256_GCM解反序列化为实体
        /// 签名规则见微信官方文档 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml
        /// </summary>
        /// <param name="aes_key">这里需要传入apiv3秘钥进行AEAD_AES_256_GCM解密 可空</param>
        /// <param name="nonce">加密的随机串 可空</param>
        /// <param name="associated_data">附加数据包 可空</param>
        /// <returns></returns>
        // TODO: 本方法持续测试
        public async Task <T> AesGcmDecryptGetObjectAsync <T>(string aes_key = null, string nonce = null, string associated_data = null) where T : ReturnJsonBase, new()
        {
            aes_key ??= _tenpayV3Setting.TenPayV3_APIv3Key;
            nonce ??= NotifyRequest.resource.nonce;
            associated_data ??= NotifyRequest.resource.associated_data;

            var decrypted_string = SecurityHelper.AesGcmDecryptCiphertext(aes_key, nonce, associated_data, NotifyRequest.resource.ciphertext);

            T result = decrypted_string.GetObject <T>();

            //验证请求签名
            var wechatpayTimestamp = _httpContext.Request.Headers?["Wechatpay-Timestamp"];
            var wechatpayNonce     = _httpContext.Request.Headers?["Wechatpay-Nonce"];
            var wechatpaySignature = _httpContext.Request.Headers?["Wechatpay-Signature"];
            var wechatpaySerial    = _httpContext.Request.Headers?["Wechatpay-Serial"];

            result.VerifySignSuccess = await TenPaySignHelper.VerifyTenpaySign(wechatpayTimestamp, wechatpayNonce, wechatpaySignature, Body, wechatpaySerial, this._tenpayV3Setting);

            result.ResultCode = new TenPayApiResultCode($"{_httpContext.Response.StatusCode} / {_httpContext.Request.Method}", "", "", "", result.VerifySignSuccess == true);

            return(result);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> JsApi(int productId, int hc)
        {
            try
            {
                //获取产品信息
                var products = ProductModel.GetFakeProductList();
                var product  = products.FirstOrDefault(z => z.Id == productId);
                if (product == null || product.GetHashCode() != hc)
                {
                    return(Content("商品信息不存在,或非法进入!1002"));
                }
                ViewData["product"] = product;

                var openId = HttpContext.Session.GetString("OpenId");

                string sp_billno = Request.Query["order_no"];//out_trade_no
                if (string.IsNullOrEmpty(sp_billno))
                {
                    //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯一
                    sp_billno = string.Format("{0}{1}{2}", TenPayV3Info.MchId /*10位*/, SystemTime.Now.ToString("yyyyMMddHHmmss"),
                                              TenPayV3Util.BuildRandomStr(6));

                    //注意:以上订单号仅作为演示使用,如果访问量比较大,建议增加订单流水号的去重检查。
                }
                else
                {
                    sp_billno = Request.Query["order_no"];
                }

                //调用下单接口下单
                var name      = product == null ? "test" : product.Name;
                var price     = product == null ? 100 : (int)(product.Price * 100);//单位:分
                var notifyUrl = TenPayV3Info.TenPayV3Notify.Replace("/TenpayV3/", "/TenpayRealV3/").Replace("http://", "https://");

                //请求信息
                TransactionsRequestData jsApiRequestData = new(TenPayV3Info.AppId, TenPayV3Info.MchId, name + " - 微信支付 V3", sp_billno, new TenpayDateTime(DateTime.Now.AddHours(1), false), null, notifyUrl, null, new() { currency = "CNY", total = price }, new(openId), null, null, null);

                //请求接口
                var result = await _basePayApis.JsApiAsync(jsApiRequestData);

                if (result.VerifySignSuccess != true)
                {
                    throw new WeixinException("获取 prepay_id 结果校验出错!");
                }

                //获取 UI 信息包
                var jsApiUiPackage = TenPaySignHelper.GetJsApiUiPackage(TenPayV3Info.AppId, result.prepay_id);
                ViewData["jsApiUiPackage"] = jsApiUiPackage;

                //临时记录订单信息,留给退款申请接口测试使用(分布式情况下请注意数据同步)
                HttpContext.Session.SetString("BillNo", sp_billno);
                HttpContext.Session.SetString("BillFee", price.ToString());

                return(View());
            }
            catch (Exception ex)
            {
                Senparc.Weixin.WeixinTrace.BaseExceptionLog(ex);
                throw;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 请求参数,获取结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url"></param>
        /// <param name="data">如果为 GET 请求,此参数可为 null</param>
        /// <returns></returns>
        public async Task <T> RequestAsync <T>(string url, object data, int timeOut = Config.TIME_OUT, ApiRequestMethod requestMethod = ApiRequestMethod.POST, bool checkSign = true, Func <T> createDefaultInstance = null)
            where T : ReturnJsonBase/*, new()*/
        {
            T result = null;

            try
            {
                HttpResponseMessage responseMessage = await GetHttpResponseMessageAsync(url, data, timeOut, requestMethod);

                //获取响应结果
                string content = await responseMessage.Content.ReadAsStringAsync();//TODO:如果不正确也要返回详情

                //检查响应代码
                TenPayApiResultCode resutlCode = TenPayApiResultCode.TryGetCode(responseMessage.StatusCode, content);

                if (resutlCode.Success)
                {
                    //TODO:待测试
                    //验证微信签名
                    //result.Signed = VerifyTenpaySign(responseMessage.Headers, content);
                    var wechatpayTimestamp       = responseMessage.Headers.GetValues("Wechatpay-Timestamp").First();
                    var wechatpayNonce           = responseMessage.Headers.GetValues("Wechatpay-Nonce").First();
                    var wechatpaySignatureBase64 = responseMessage.Headers.GetValues("Wechatpay-Signature").First();//后续需要base64解码
                    var wechatpaySerial          = responseMessage.Headers.GetValues("Wechatpay-Serial").First();

                    result = content.GetObject <T>();

                    if (checkSign)
                    {
                        try
                        {
                            var pubKey = await TenPayV3InfoCollection.GetAPIv3PublicKeyAsync(this._tenpayV3Setting, wechatpaySerial);

                            result.VerifySignSuccess = TenPaySignHelper.VerifyTenpaySign(wechatpayTimestamp, wechatpayNonce, wechatpaySignatureBase64, content, pubKey);
                        }
                        catch (Exception ex)
                        {
                            throw new TenpayApiRequestException("RequestAsync 签名验证失败:" + ex.Message, ex);
                        }
                    }
                }
                else
                {
                    result = createDefaultInstance?.Invoke() ?? GetInstance <T>(true);
                    resutlCode.Additional = content;
                }
                //T result = resutlCode.Success ? (await responseMessage.Content.ReadAsStringAsync()).GetObject<T>() : new T();
                result.ResultCode = resutlCode;

                return(result);
            }
            catch (Exception ex)
            {
                SenparcTrace.BaseExceptionLog(ex);
                result = createDefaultInstance?.Invoke() ?? GetInstance <T>(false);
                if (result != null)
                {
                    result.ResultCode = new() { ErrorMessage = ex.Message };
                }

                return(result);
            }
        }