Пример #1
0
        /// <summary>
        /// 订单查询
        /// </summary>
        /// <param name="localTradeNo">系统交易单号(预付单返回的OutTradeNo)</param>
        /// <returns></returns>
        public bool TryOrderQuery(string appId, string mchId, string mchKey, string localTradeNo,
                                  out OrderQueryResult result)
        {
            string nonceStr = TenPayV3Util.GetNoncestr();

            result = TenPayV3.OrderQuery(new TenPayV3OrderQueryRequestData(
                                             appId,
                                             mchId,
                                             null,
                                             nonceStr,
                                             localTradeNo,
                                             mchKey));

            Logger.LogInformation($"微信支付订单查询,out_trade_no:{localTradeNo},result:{result.ResultXml}");
            return(result.IsReturnCodeSuccess() && result.IsResultCodeSuccess());
        }
Пример #2
0
        public override async Task <PayResult> PayResultNotify(
            IEnumerable <KeyValuePair <string, string> > query,
            IDictionary <string, string> header,
            IDictionary <string, string> form,
            Stream body)
        {
            var notifyResult = new PayResult()
            {
                Success = false
            };

            string xml = await body.ReadToStringAsync();

            if (xml.IsNullOrWhiteSpace())
            {
                notifyResult.Message = "微信支付回调,xml请求数据为空!";
                Logger.LogError(notifyResult.Message);
                return(notifyResult);
            }

            ResponseHandler  resHandler;
            OrderQueryResult result;

            try
            {
                // 获取微信回调数据
                // ResponseHandler读取body时没有seek,先seek下
                HttpContextAccessor.HttpContext.Request.Body.Seek(0, SeekOrigin.Begin);
                resHandler = new ResponseHandler(HttpContextAccessor.HttpContext);

                Logger.LogInformation($"微信支付回调:" + xml);
                // 转换为对象
                result = new OrderQueryResult(xml);
            }
            catch (XmlException)
            {
                // xml格式异常直接不管
                notifyResult.Message = "微信支付回调,xml数据异常!";
                //logger.LogError(notifyResult.Message);
                return(notifyResult);
            }
            catch (Exception ex)
            {
                notifyResult.Message = "微信支付回调,数据解析错误!";
                Logger.LogError(ex, notifyResult.Message);
                return(notifyResult);
            }

            var secret = MchOptions.Value.Get(result.mch_id)?.MchKey;

            if (secret.IsNullOrWhiteSpace())
            {
                notifyResult.Message = $"微信支付回调,未找到商户密钥,mchId:{result.mch_id},result:{xml}";
                return(notifyResult);
            }

            // 设置appkey,用于验证参数签名
            resHandler.SetKey(secret);
            // 参数签名验证
            if (!resHandler.IsTenpaySign())
            {
                notifyResult.Message = $"微信支付回调:签名错误,result:{xml}";
                Logger.LogError(notifyResult.Message);
                return(notifyResult);
            }

            // 支付回调结果是否正确
            if (!result.IsReturnCodeSuccess() || !result.IsResultCodeSuccess())
            {
                notifyResult.Message = $"微信支付回调,支付失败:return error,result:{xml}";
                Logger.LogError(notifyResult.Message);
                return(notifyResult);
            }

            notifyResult.Success       = true;
            notifyResult.RealPayAmount = notifyResult.Amount = result.total_fee.ConvertTo <int>();
            notifyResult.LocalTradeNo  = result.out_trade_no;
            notifyResult.TransactionId = result.transaction_id;
            notifyResult.Attach        = result.attach;
            notifyResult.TraderId      = result.openid;
            notifyResult.PayData       = xml;
            notifyResult.MchId         = result.mch_id;
            notifyResult.AppId         = result.appid;

            return(await Task.FromResult(notifyResult));
        }