Esempio n. 1
0
        public override Task <string> GetPayResultResponse(PayResult notify)
        {
            // 订单正确和错误的返回给微信的结果数据
            string errStr = @"
<xml>
    <return_code><![CDATA[FAIL]]></return_code>
    <return_msg><![CDATA[支付失败]]></return_msg>
</xml>";

            string rightStr = @"
<xml>
    <return_code><![CDATA[SUCCESS]]></return_code>
    <return_msg><![CDATA[支付成功]]></return_msg>
</xml>";

            return(notify.Success ? Task.FromResult(rightStr) : Task.FromResult(errStr));
        }
Esempio n. 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));
        }
Esempio n. 3
0
 public abstract Task <string> GetPayResultResponse(PayResult notify);
Esempio n. 4
0
 public PaySuccessInnerNotifyEvent(PayResult notify)
 {
     Notify = notify;
 }