Пример #1
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));
        }
Пример #2
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);
        }