Exemplo n.º 1
0
        /// <summary>
        /// 统一下单接口返回正常的prepay_id,再按签名规范重新生成签名后,将数据传输给APP
        /// </summary>
        /// <returns></returns>
        // ReSharper disable once InconsistentNaming
        public string GetPrepayIDSign()
        {
            const string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            SortedDictionary <string, object> mValues = new SortedDictionary <string, object>
            {
                ["body"]             = _wxInfo.Body,
                ["attach"]           = _wxInfo.Body,
                ["out_trade_no"]     = _wxInfo.OutTradeNo,
                ["total_fee"]        = _wxInfo.TotalFee,
                ["time_start"]       = DateTime.Now.ToString("yyyyMMddHHmmss"),
                ["time_expire"]      = DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"),
                ["trade_type"]       = _wxInfo.TradeType,
                ["appid"]            = _wxInfo.AppID,
                ["mch_id"]           = _wxInfo.Mchid,
                ["notify_url"]       = _wxInfo.NotifyUrl,
                ["spbill_create_ip"] = "",
                ["nonce_str"]        = GenerateNonceStr()
            };

            if (!string.IsNullOrEmpty(_wxInfo.OpenId))
            {
                mValues["openid"] = _wxInfo.OpenId;
            }
            mValues["sign"] = MakeSign(mValues, _wxInfo.Key);

            //签名
            string xml      = ToXml(mValues);
            string response = WxHttpService.Post(xml, url, 6);
            SortedDictionary <string, object> sDic = FromXml(response);
            object obj;

            sDic.TryGetValue("prepay_id", out obj);
            string prepayId = obj?.ToString() ?? "";

            if (prepayId != "")
            {
                SortedDictionary <string, object> cDic =
                    new SortedDictionary <string, object>
                {
                    ["appid"]     = _wxInfo.AppID,
                    ["noncestr"]  = Guid.NewGuid().ToString().Replace("-", ""),
                    ["package"]   = "Sign=WXPay",
                    ["partnerid"] = _wxInfo.Mchid,
                    ["prepayid"]  = prepayId,
                    ["timestamp"] = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000)
                                    .ToString()
                };
                cDic["sign"] = MakeSign(cDic, _wxInfo.Key);

                return(ToJson(cDic));
            }
            return("");
        }
Exemplo n.º 2
0
        /// <summary>
        /// 统一下单接口返回正常的prepay_id,再按签名规范重新生成签名后,将数据传输给APP
        /// </summary>
        /// <returns></returns>
        // ReSharper disable once InconsistentNaming
        public string GetWXPayLink()
        {
            string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

            Log4Net.WriteInfoLog("微信IP" + ip);
            Log4Net.WriteInfoLog("_wxInfo.TotalFee:" + _wxInfo.TotalFee);

            const string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            SortedDictionary <string, object> mValues = new SortedDictionary <string, object>
            {
                ["body"]             = _wxInfo.Body,
                ["attach"]           = _wxInfo.Body,
                ["out_trade_no"]     = _wxInfo.OutTradeNo,
                ["total_fee"]        = _wxInfo.TotalFee,
                ["time_start"]       = DateTime.Now.ToString("yyyyMMddHHmmss"),
                ["time_expire"]      = DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"),
                ["trade_type"]       = _wxInfo.TradeType,
                ["appid"]            = _wxInfo.AppID,
                ["mch_id"]           = _wxInfo.Mchid,
                ["notify_url"]       = _wxInfo.NotifyUrl,
                ["spbill_create_ip"] = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"],
                ["nonce_str"]        = GenerateNonceStr()
            };

            if (!string.IsNullOrEmpty(_wxInfo.OpenId))
            {
                mValues["openid"] = _wxInfo.OpenId;
            }
            mValues["sign"] = MakeSign(mValues, _wxInfo.Key);

            //签名
            string xml      = ToXml(mValues);
            string response = WxHttpService.Post(xml, url, 6);

            Log4Net.WriteInfoLog("微信获取连接response:" + response);
            SortedDictionary <string, object> sDic = FromXml(response);
            object obj;

            sDic.TryGetValue("mweb_url", out obj);
            string payLink = obj?.ToString() ?? "";

            if (payLink != "")
            {
                return(payLink);
            }
            return("");
        }