示例#1
0
        /// <summary>
        /// 微信通知
        /// </summary>
        /// <example>页面只能显示success/failed 不能有任何html标签</example>
        /// <returns>成功返回success 失败返回其它任意对象</returns>
        public ActionResult PayNotifyUrlTest()
        {
            var orderNotify = new OrderNotifyImpl()
            {
                apiKey = ConfigurationManager.AppSettings["apiKey"]
            };

            var param = new UpdatePayStatusRequest()
            {
                Amount     = new decimal(200),
                VoucherNo  = "4200000083201803148591866779",
                OrderSysNo = "3535d53a9cd34816a532b80a1bfd7f3d"
            };

            WriteWeixinLog("更新参数:\r\n" + param.ToJson2(), null);

            var result = RechargeApp.Instance.UpdatePayStatus(param);

            return(Content("", "text/xml"));
        }
示例#2
0
        /// <summary>
        /// 微信通知
        /// </summary>
        /// <example>页面只能显示success/failed 不能有任何html标签</example>
        /// <returns>成功返回success 失败返回其它任意对象</returns>
        public ActionResult PayNotifyUrl()
        {
            //返回状态码SUCCESS/FAIL,SUCCESS 表示商户接收通知成功并校验成功
            var returnCode = "FAIL";
            //返回信息 非空为错误原因:1、签名失败 2、参数格式校验错误
            var returnMsg = string.Empty;


            try
            {
                WriteWeixinLog("测试开始==========================\r\n", null);

                OrderNotifyImpl orderNotifyImpl = new OrderNotifyImpl()
                {
                    apiKey = ConfigurationManager.AppSettings["apiKey"]
                };

                WriteWeixinLog("PayNotifyUrl", null);

                var requestStream = System.Web.HttpContext.Current.Request.InputStream;

                byte[] requestByte = new byte[requestStream.Length];
                requestStream.Read(requestByte, 0, (int)requestStream.Length);
                string xmlStr = Encoding.UTF8.GetString(requestByte);

                if (!string.IsNullOrWhiteSpace(xmlStr))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.XmlResolver = null;
                    doc.LoadXml(xmlStr);

                    XmlNode     xmlNode     = doc.SelectSingleNode("xml");
                    XmlNodeList xmlNodeList = xmlNode.ChildNodes;
                    foreach (XmlNode xnf in xmlNodeList)
                    {
                        orderNotifyImpl.Add(xnf.Name, xnf.InnerText);
                    }

                    #region 参数
                    var model = new OrderNotifyResponse()
                    {
                        //返回状态码 SUCCESS/FAIL 此字段是通信标识,非交易标识,交易是否成功需要查看 result_code来判断
                        return_code = orderNotifyImpl.Get("return_code"),
                        //返回信息,如非空为错误原因 1:签名失败 2:参数格式校验错误
                        return_msg = orderNotifyImpl.Get("return_msg"),

                        #region 以下字段在return_code为SUCCESS的时候有返回
                        //微信公众账号id
                        appid = orderNotifyImpl.Get("appid"),
                        //商户号
                        mch_id = orderNotifyImpl.Get("mch_id"),
                        //终端设备号
                        device_info = orderNotifyImpl.Get("device_info"),
                        //随机字符串
                        nonce_str = orderNotifyImpl.Get("nonce_str"),
                        //签名
                        sign = orderNotifyImpl.Get("sign"),
                        //业务结果
                        result_code = orderNotifyImpl.Get("result_code"),
                        //错误代码
                        err_code = orderNotifyImpl.Get("err_code"),
                        //错误代码描述
                        err_code_des = orderNotifyImpl.Get("err_code_des"),
                        #endregion

                        #region 以下字段在return_code和result_code都为SUCCESS的时候有返回
                        //用户标识
                        openid = orderNotifyImpl.Get("openid"),
                        //是否关注公众账号
                        is_subscribe = orderNotifyImpl.Get("is_subscribe"),
                        //交易类型
                        trade_type = orderNotifyImpl.Get("trade_type"),
                        //付款银行
                        bank_type = orderNotifyImpl.Get("bank_type"),
                        //总金额
                        total_fee = orderNotifyImpl.Get("total_fee"),
                        //现金券金额
                        coupon_fee = orderNotifyImpl.Get("coupon_fee"),
                        //货币种类
                        fee_type = orderNotifyImpl.Get("fee_type"),
                        //微信支付订单号
                        transaction_id = orderNotifyImpl.Get("transaction_id"),
                        //商户订单号
                        out_trade_no = orderNotifyImpl.Get("out_trade_no"),
                        //商家数据包
                        attach = orderNotifyImpl.Get("attach"),
                        //支付完成时间
                        time_end = orderNotifyImpl.Get("time_end")
                                   #endregion
                    };
                    #endregion

                    WriteWeixinLog("model:" + model.ToJson(), null);

                    if (model.return_code != "SUCCESS" || model.result_code != "SUCCESS")
                    {
                        WriteWeixinLog(string.Format("通信或业务结果失败,return_code:{0},result_code:{1},err_code:{2},err_code_des:{3}", model.return_code, model.result_code, model.err_code, model.err_code_des), null);
                    }

                    if (orderNotifyImpl.IsTenpaySign())
                    {
                        if (model.return_code == "SUCCESS" && model.result_code == "SUCCESS")
                        {
                            WriteWeixinLog("签名成功:\r\n", null);

                            var param = new UpdatePayStatusRequest()
                            {
                                Amount     = decimal.Parse(model.total_fee) / 100,
                                VoucherNo  = model.transaction_id,
                                OrderSysNo = model.out_trade_no
                            };

                            WriteWeixinLog("更新参数:\r\n" + param.ToJson2(), null);

                            var result = RechargeApp.Instance.UpdatePayStatus(param);

                            WriteWeixinLog(string.Format("更新订单:{0},支付状态{1}:\r\n", model.out_trade_no, result.ToJson()), null);

                            if (!result.Status)
                            {
                                returnCode = "FAIL";
                                returnMsg  = result.Message;
                            }
                            returnCode = "SUCCESS";
                        }
                    }
                    else
                    {
                        returnMsg = "签名失败";
                        WriteWeixinLog("签名失败:" + model.ToJson(), null);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteWeixinLog("exception:" + ex.Message, null);
            }
            string xml = string.Format(@"<xml>
                                            <return_code><![CDATA[{0}]]></return_code>
                                            <return_msg><![CDATA[{1}]]></return_msg>
                                         </xml>", returnCode, returnMsg);

            return(Content(xml, "text/xml"));
        }