Exemple #1
0
        public JsonResult setRandomMoney(string _openId, string body)
        {
            //随机数字,并且生成Prepay
            string appid  = WeChatUtilities.getConfigValue(WeChatUtilities.APP_ID);
            string mch_id = WeChatUtilities.getConfigValue(WeChatUtilities.MCH_ID);
            //先确认,之后做随机数
            string nonce_str    = CommonUtilities.generateNonce();
            string out_trade_no = "WXJSAPI_" + DateTime.Now.Ticks;
            Random r            = new Random();
            int    total_fee    = 1580;
            int    baseRandom   = r.Next(0, 99);

            if (baseRandom < 3)
            {
                total_fee = r.Next(40, 158) * 10;
            }
            else
            {
                total_fee = r.Next(108, 158) * 10;
            }
            try
            {
                Wx_OrderResult result = createUnifiedOrder(_openId, body, out_trade_no, total_fee, WeChatUtilities.TRADE_TYPE_JSAPI, "");
                if (result.Result == "SUCCESS")
                {
                    WxPayOrder order = new WxPayOrder()
                    {
                        Body         = body,
                        Time_Start   = DateTime.Now,
                        Mch_Id       = mch_id,
                        Open_Id      = _openId,
                        Trade_No     = out_trade_no,
                        Total_Fee    = total_fee,
                        Prepay_Id    = result.PrepayId,
                        Trade_Status = WeChatUtilities.TRADE_STATUS_CREATE,
                        Trade_Type   = WeChatUtilities.TRADE_TYPE_JSAPI
                    };
                    offlineDB.WxPayOrder.Add(order);
                    offlineDB.SaveChanges();
                    return(Json(new { result = "SUCCESS", prepay_id = result.PrepayId, total_fee }, JsonRequestBehavior.AllowGet));
                }
                return(Json(new { result = "FAIL", msg = result.Message }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new { result = "FAIL", msg = e.ToString() }, JsonRequestBehavior.AllowGet));
            }
        }
Exemple #2
0
        public JsonResult SetFixMoney(string _openId, string body, int amount)
        {
            //随机数字,并且生成Prepay
            string appid  = WeChatUtilities.getConfigValue(WeChatUtilities.APP_ID);
            string mch_id = WeChatUtilities.getConfigValue(WeChatUtilities.MCH_ID);
            //先确认,之后做随机数
            string nonce_str    = CommonUtilities.generateNonce();
            string out_trade_no = "WXJSAPI_" + DateTime.Now.Ticks;

            try
            {
                Wx_OrderResult result = createUnifiedOrder(_openId, body, out_trade_no, amount, WeChatUtilities.TRADE_TYPE_JSAPI, "");
                if (result.Result == "SUCCESS")
                {
                    WxPaymentOrder order = new WxPaymentOrder()
                    {
                        Body         = body,
                        Time_Start   = DateTime.Now,
                        Mch_Id       = mch_id,
                        Open_Id      = _openId,
                        Trade_No     = out_trade_no,
                        Total_Fee    = amount,
                        Prepay_Id    = result.PrepayId,
                        Trade_Status = WeChatUtilities.TRADE_STATUS_CREATE,
                        Trade_Type   = WeChatUtilities.TRADE_TYPE_JSAPI
                    };
                    payDB.WxPaymentOrder.Add(order);
                    payDB.SaveChanges();
                    return(Json(new { result = "SUCCESS", prepay_id = result.PrepayId, amount }, JsonRequestBehavior.AllowGet));
                }
                return(Json(new { result = "FAIL", msg = result.Message }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new { result = "FAIL", msg = e.ToString() }, JsonRequestBehavior.AllowGet));
            }
        }
Exemple #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"));
            }
        }