Exemplo n.º 1
0
        /// <summary>
        /// 创建微信统一下单
        /// </summary>
        /// <param name="openid"></param>
        /// <param name="body"></param>
        /// <param name="out_trade_no"></param>
        /// <param name="total_fee"></param>
        /// <param name="trade_type"></param>
        public Wx_OrderResult createUnifiedOrder(string openid, string body, string out_trade_no, int total_fee, string trade_type, string device_no)
        {
            string appid  = WeChatUtilities.getConfigValue(WeChatUtilities.APP_ID);
            string mch_id = WeChatUtilities.getConfigValue(WeChatUtilities.MCH_ID);
            //先确认,之后做随机数
            string nonce_str = CommonUtilities.generateNonce();
            //string out_trade_no = "WX" + CommonUtilities.generateTimeStamp();
            string spbill_create_ip          = WeChatUtilities.getConfigValue(WeChatUtilities.IP);
            string notify_url                = "http://webapp.shouquanzhai.cn/Pay/wx_pay_nofity";
            List <QueryParameter> parameters = new List <QueryParameter>();

            parameters.Add(new QueryParameter("appid", appid));
            parameters.Add(new QueryParameter("mch_id", mch_id));
            parameters.Add(new QueryParameter("nonce_str", nonce_str));
            parameters.Add(new QueryParameter("body", body));
            parameters.Add(new QueryParameter("out_trade_no", out_trade_no));
            parameters.Add(new QueryParameter("total_fee", total_fee.ToString()));
            parameters.Add(new QueryParameter("spbill_create_ip", spbill_create_ip));
            parameters.Add(new QueryParameter("notify_url", notify_url));
            parameters.Add(new QueryParameter("trade_type", trade_type));
            parameters.Add(new QueryParameter("openid", openid));
            if (device_no != "")
            {
                parameters.Add(new QueryParameter("device_info", device_no));
            }
            string sign        = WeChatUtilities.createPaySign(parameters);
            string xml_content = parseXml(parameters, sign);
            string post_url    = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            var    request     = WebRequest.Create(post_url) as HttpWebRequest;

            try
            {
                request.Method = "post";
                StreamWriter streamWriter = new StreamWriter(request.GetRequestStream());
                streamWriter.Write(xml_content);
                streamWriter.Flush();
                streamWriter.Close();
                var          response = (HttpWebResponse)request.GetResponse();
                StreamReader reader   = new StreamReader(response.GetResponseStream());
                string       result   = reader.ReadToEnd();
                XmlDocument  doc      = new XmlDocument();
                doc.LoadXml(result);
                string fromUser   = doc.GetElementsByTagName("return_code")[0].InnerText;
                string returnText = doc.GetElementsByTagName("return_msg")[0].InnerText;
                string prepay_id  = doc.GetElementsByTagName("prepay_id")[0].InnerText;
                return(new Wx_OrderResult("SUCCESS", prepay_id, "OK"));
            }
            catch (Exception e)
            {
                return(new Wx_OrderResult("FAIL", "", e.ToString()));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 生成扫码链接
        /// </summary>
        /// <param name="product_id"></param>
        /// <returns></returns>
        public ContentResult create_native_pay_url(string product_id)
        {
            string appid      = WeChatUtilities.getConfigValue(WeChatUtilities.APP_ID);
            string mch_id     = WeChatUtilities.getConfigValue(WeChatUtilities.MCH_ID);
            string nonce      = CommonUtilities.generateNonce();
            string time_stamp = Convert.ToString(CommonUtilities.generateTimeStamp());
            List <QueryParameter> parameters = new List <QueryParameter>();

            parameters.Add(new QueryParameter("appid", appid));
            parameters.Add(new QueryParameter("mch_id", mch_id));
            parameters.Add(new QueryParameter("time_stamp", time_stamp));
            parameters.Add(new QueryParameter("nonce_str", nonce));
            parameters.Add(new QueryParameter("product_id", product_id));
            string sign = WeChatUtilities.createPaySign(parameters);
            string url  = "weixin://wxpay/bizpayurl?" + QueryParameter.NormalizeRequestParameters(parameters) + "&sign=" + sign;

            return(Content(url));
        }
Exemplo n.º 3
0
        public ContentResult wx_native_notify()
        {
            StreamReader reader = new StreamReader(Request.InputStream, Encoding.UTF8);
            XmlDocument  doc    = new XmlDocument();

            doc.Load(reader);
            string appid        = doc.GetElementsByTagName("appid")[0].InnerText;
            string openid       = doc.GetElementsByTagName("openid")[0].InnerText;
            string mch_id       = doc.GetElementsByTagName("mch_id")[0].InnerText;
            string is_subscribe = doc.GetElementsByTagName("is_subscribe")[0].InnerText;
            string nonce_str    = doc.GetElementsByTagName("nonce_str")[0].InnerText;
            string product_id   = doc.GetElementsByTagName("product_id")[0].InnerText;
            string request_sign = doc.GetElementsByTagName("sign")[0].InnerText;
            string trade_no     = "WXNATIVE_" + DateTime.Now.Ticks;
            int    total_fee    = 10;
            string _body        = "未知商品";
            string _detail      = "未知商品";

            try
            {
                int          _product_id = Convert.ToInt32(product_id);
                WxPayProduct product     = offlineDB.WxPayProduct.SingleOrDefault(m => m.Id == _product_id);
                if (product != null)
                {
                    _body     = product.ProductName;
                    _detail   = product.ProductDetails;
                    total_fee = Convert.ToInt32(product.Total_Fee * 100);
                }
            }
            catch (Exception e)
            {
                CommonUtilities.writeLog(e.ToString());
                total_fee = 10;
                _body     = "未知商品";
                _detail   = "未知商品";
            }

            Wx_OrderResult result = createUnifiedOrder(openid, _body, trade_no, total_fee, WeChatUtilities.TRADE_TYPE_NATIVE, "");

            if (result.Result == "SUCCESS")
            {
                WxPayOrder order = new WxPayOrder()
                {
                    Body         = _body,
                    Time_Start   = DateTime.Now,
                    Mch_Id       = mch_id,
                    Open_Id      = openid,
                    Detail       = _detail,
                    Trade_No     = trade_no,
                    Product_Id   = product_id,
                    Prepay_Id    = result.PrepayId,
                    Total_Fee    = total_fee,
                    Trade_Status = WeChatUtilities.TRADE_STATUS_CREATE,
                    Trade_Type   = WeChatUtilities.TRADE_TYPE_NATIVE
                };
                offlineDB.WxPayOrder.Add(order);
                offlineDB.SaveChanges();
                List <QueryParameter> parameters = new List <QueryParameter>();
                parameters.Add(new QueryParameter("return_code", result.Result));
                parameters.Add(new QueryParameter("return_msg", result.Message));
                parameters.Add(new QueryParameter("appid", appid));
                parameters.Add(new QueryParameter("mch_id", mch_id));
                parameters.Add(new QueryParameter("nonce_str", CommonUtilities.generateNonce()));
                parameters.Add(new QueryParameter("prepay_id", result.PrepayId));
                parameters.Add(new QueryParameter("result_code", result.Result));
                parameters.Add(new QueryParameter("err_code_des", result.Message));
                string response_sign = WeChatUtilities.createPaySign(parameters);
                string xmlcontent    = parseXml(parameters, response_sign);
                return(Content(xmlcontent, "text/plain"));
            }
            else
            {
                List <QueryParameter> parameters = new List <QueryParameter>();
                parameters.Add(new QueryParameter("return_code", result.Result));
                parameters.Add(new QueryParameter("return_msg", result.Message));
                string response_sign = WeChatUtilities.createPaySign(parameters);
                string xmlcontent    = parseXml(parameters, response_sign);
                return(Content(xmlcontent, "text/plain"));
            }
        }