Example #1
0
        /// <summary>
        /// 查询订单
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="errMsg"></param>
        public SortedDictionary <string, string> OrderQueryOrderId(string orderId, out string errMsg)
        {
            string postXml = generateOrderQueryByOrderId(orderId);

            //下单
            string result;

            if (!WxpayCore.Post(WxpayConfig.queryPay_Url, postXml, out result, out errMsg))
            {
                errMsg = "查询订单失败";
                return(null);
            }
            if (string.IsNullOrEmpty(result) || result.IndexOf("return_code") == -1)
            {
                errMsg = "查询订单失败";
                return(null);
            }

            var order = WxpayCore.FromXml(result);

            if (order["return_code"] != "SUCCESS")
            {
                errMsg = "查询订单失败(return_code=" + order["return_code"] + ";return_msg=" + order["return_msg"] + ")";
                return(null);
            }
            errMsg = "";
            return(order);
        }
Example #2
0
        /// <summary>
        /// 生成统一下单交易数据包(为pc端网页扫码模式)
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="money"></param>
        /// <param name="body"></param>
        /// <param name="notifyUrl"></param>
        /// <param name="expire"></param>
        /// <param name="userOpenId">用户openid</param>
        /// <param name="ip"></param>
        private string genPrePay4Qrcode(string orderId, decimal money, string body, string notifyUrl, DateTime expire, string ip)
        {
            string noncestr = Guid.NewGuid().ToString().Replace("-", ""); //32 位内的随机串,防重发
            var    param    = new SortedDictionary <string, object>();

            param.Add("appid", config.AppId);
            param.Add("mch_id", config.MerchantId);
            param.Add("nonce_str", noncestr);
            param.Add("device_info", "WEB");
            param.Add("body", body);
            param.Add("out_trade_no", orderId);
            param.Add("total_fee", Convert.ToInt32(Math.Round(money * 100, 0))); //订单总金额,单位为分
            param.Add("spbill_create_ip", ip);
            param.Add("notify_url", notifyUrl);
            param.Add("product_id", orderId);
            param.Add("trade_type", "NATIVE");
            if (expire != DateTime.MinValue)
            {
                param.Add("time_expire", expire.ToString("yyyyMMddHHmmss"));
            }

            //生成签名
            string signString = WxpayCore.CreateLinkString(param) + "&key=" + config.MerchantKey;
            string packSign   = WxpayCore.MD5(signString, Encoding.UTF8);

            param.Add("sign", packSign);

            return(WxpayCore.ToXml(param));
        }
Example #3
0
        /// <summary>
        /// 为公众号网页提供支付请求,并以键值对形式返回(方便用于转为json)
        /// </summary>
        /// <param name="notifyUrl">到账通知Url</param>
        /// <param name="orderId"></param>
        /// <param name="money"></param>
        /// <param name="desc">商品描述</param>
        /// <param name="userOpenId">用户openid</param>
        /// <param name="ip"></param>
        /// <param name="errMsg"></param>
        public Dictionary <string, string> Pay4JSApi(string notifyUrl, string orderId, decimal money, string desc, string ip, string userOpenId, out string errMsg)
        {
            if (notifyUrl.IndexOf("://127.0.0.1") != -1)
            {
                errMsg = "尚未配置微信支付结果通知域名";
                return(null);
            }

            string postXml = genPrePay4Jsapi(orderId, money, desc, notifyUrl, DateTime.MinValue, userOpenId, ip);

            //下单
            var    url = string.Format(WxpayConfig.genPrePay_Url, config.GetAccessToken());
            string result;

            if (!WxpayCore.Post(url, postXml, out result, out errMsg))
            {
                errMsg = "预支付请求失败";
                return(null);
            }
            if (string.IsNullOrEmpty(result) || result.IndexOf("return_code") == -1)
            {
                errMsg = "预支付返回值无效";
                return(null);
            }

            var rlsObj = WxpayCore.FromXml(result);

            if (rlsObj["return_code"] != "SUCCESS" || rlsObj["result_code"] != "SUCCESS")
            {
                if (rlsObj["return_code"] != "SUCCESS")
                {
                    errMsg = "预支付失败(return_code=" + rlsObj["return_code"] + ")";
                }
                else
                {
                    errMsg = "预支付失败(result_code=" + rlsObj["result_code"] + ")";
                }
                return(null);
            }
            if (!WxpayCore.CheckSign(rlsObj, config.MerchantKey))
            {
                errMsg = "预支付返回值中签名无效";
                return(null);
            }
            var preId = (string)rlsObj["prepay_id"];

            if (string.IsNullOrEmpty(preId))
            {
                errMsg = "预支付返回值中prepay_id无效";
                return(null);
            }

            errMsg = "";
            string noncestr  = Guid.NewGuid().ToString().Replace("-", ""); //32 位内的随机串,防重发
            string timeStamp = WxpayCore.GetUnixDatetime();

            return(ToJsapi(orderId, noncestr, timeStamp, preId));
        }
Example #4
0
        private Dictionary <string, string> ToJsapi(string orderId, string noncestr, string timeStamp, string prepayId)
        {
            var dic = new Dictionary <string, string>();

            dic.Add("appId", config.AppId);
            dic.Add("timeStamp", timeStamp);
            dic.Add("nonceStr", noncestr);
            dic.Add("package", "prepay_id=" + prepayId);
            dic.Add("signType", "MD5");

            //生成签名
            string signString = WxpayCore.CreateLinkString(dic) + "&key=" + config.MerchantKey;
            string sign       = WxpayCore.MD5(signString, Encoding.UTF8);

            dic.Add("paySign", sign);
            return(dic);
        }
Example #5
0
        /// <summary>
        /// 生成订单查询XML
        /// </summary>
        /// <param name="orderId"></param>
        private string generateOrderQueryByOrderId(string orderId)
        {
            string noncestr = Guid.NewGuid().ToString().Replace("-", ""); //32 位内的随机串,防重发
            var    param    = new SortedDictionary <string, object>();

            param.Add("appid", config.AppId);
            param.Add("mch_id", config.MerchantId);
            param.Add("nonce_str", noncestr);
            param.Add("out_trade_no", orderId);

            //生成签名
            string signString = WxpayCore.CreateLinkString(param) + "&key=" + config.MerchantKey;
            string packSign   = WxpayCore.MD5(signString, Encoding.UTF8);

            param.Add("sign", packSign);

            return(WxpayCore.ToXml(param));
        }
Example #6
0
        private void RefreshAccessToken()
        {
            var url = string.Format(getAccessToken_Url, appId, appSecret);

            string result, err;

            if (WxpayCore.Get(url, 5000, out result, out err) && !string.IsNullOrEmpty(result))
            {
                var root = JsonConvert.DeserializeObject <Dictionary <string, object> >(result);
                if (root.ContainsKey("access_token") && root.ContainsKey("expires_in"))
                {
                    var at = (string)root["access_token"];
                    var ex = root["expires_in"];
                    if (!string.IsNullOrEmpty(at))
                    {
                        accessToken       = at;
                        accessTokenExpire = DateTime.Now.AddSeconds(int.Parse(ex.ToString()) - 60 * 10);
                    }
                }
            }
        }
Example #7
0
        public static bool CheckSign(SortedDictionary <string, string> param, string merchantKey)
        {
            string sign   = null;
            var    param1 = new SortedDictionary <string, string>();

            foreach (KeyValuePair <string, string> p in param)
            {
                if (p.Key != "sign")
                {
                    param1.Add(p.Key, p.Value);
                }
                else
                {
                    sign = p.Value.ToString();
                }
            }
            //string signString = WxpayCore.CreateLinkString(param1) + "&key=" + WxpayConfig.MerchantKey;
            string signString = WxpayCore.CreateLinkString(param1) + "&key=" + merchantKey;
            string mySign     = WxpayCore.MD5(signString, Encoding.UTF8);

            return(sign == mySign);
        }
Example #8
0
        /// <summary>
        /// 为PC网页提供充值支付请求
        /// </summary>
        /// <param name="notifyUrl">到账通知Url</param>
        /// <param name="orderId"></param>
        /// <param name="money"></param>
        /// <param name="desc">商品描述</param>
        /// <param name="userOpenId">用户openid</param>
        /// <param name="ip"></param>
        /// <param name="errMsg"></param>
        /// <param name="prepay_id">预支付订单号</param>
        /// <param name="qrcode_url">二维码Url内容</param>
        public bool Pay4Qrcode(string notifyUrl, string orderId, decimal money, string desc, string ip, out string errMsg, out string prepay_id, out string qrcode_url)
        {
            prepay_id  = null;
            qrcode_url = null;
            if (notifyUrl.IndexOf("://127.0.0.1") != -1)
            {
                errMsg = "尚未配置微信支付结果通知域名";
                return(false);
            }

            string postXml = genPrePay4Qrcode(orderId, money, desc, notifyUrl, DateTime.MinValue, ip);

            //下单
            var    url = string.Format(WxpayConfig.genPrePay_Url, config.GetAccessToken());
            string result;

            if (!WxpayCore.Post(url, postXml, out result, out errMsg))
            {
                errMsg = "预支付请求失败";
                return(false);
            }
            if (string.IsNullOrEmpty(result) || result.IndexOf("return_code") == -1)
            {
                errMsg = "预支付返回值无效";
                return(false);
            }

            var rlsObj = WxpayCore.FromXml(result);

            LogHelper.writeLog("测试2", result, null);
            if (rlsObj["return_code"] != "SUCCESS" || rlsObj["result_code"] != "SUCCESS")
            {
                if (rlsObj["return_code"] != "SUCCESS")
                {
                    errMsg = "预支付失败(return_code=" + rlsObj["return_code"] + ")";
                }
                else
                {
                    errMsg = "预支付失败(result_code=" + rlsObj["result_code"] + ")";
                }
                return(false);
            }
            if (!WxpayCore.CheckSign(rlsObj, config.MerchantKey))
            {
                errMsg = "预支付返回值中签名无效";
                return(false);
            }
            prepay_id = (string)rlsObj["prepay_id"];
            if (string.IsNullOrEmpty(prepay_id))
            {
                errMsg = "预支付返回值中prepay_id无效";
                return(false);
            }
            qrcode_url = (string)rlsObj["code_url"];
            if (string.IsNullOrEmpty(qrcode_url))
            {
                errMsg = "预支付返回值中code_url无效";
                return(false);
            }

            return(true);
        }