Ejemplo n.º 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));
            }
        }
Ejemplo n.º 2
0
        public WxPayOrder Pay(WxPayInfo wxPayInfo)
        {
            //  var us = GetUserSession(false);


            WxPayOrder wxOrder  = null;
            JsApiPay   jsApiPay = new JsApiPay();

            try
            {
                var us = GetUserSession(false);
                if (us != null)
                {
                    string notifyUrl = "http://edu.iqiban.cn/api/wxPay/Notify";

                    var eCoursePrice = _CourseSrv.GetCoursePrice(wxPayInfo.PriceCode);
                    if (eCoursePrice.CourseScheduleType == EduCenterModel.BaseEnum.CourseScheduleType.VIP)
                    {
                        eCoursePrice.Qty   = wxPayInfo.VIPQty;
                        eCoursePrice.Price = us.UserAccount.VIPPrice1 * eCoursePrice.Qty;
                    }


                    jsApiPay.openid    = us.OpenId;
                    jsApiPay.total_fee = (int)eCoursePrice.Price * 100;



                    string OrderNo = WxPayApi.GenerateOutTradeNo();
                    var    order   = _BusinessSrv.PayCourseOrder(us.OpenId, eCoursePrice);

                    WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(wxPayInfo.ItemDes, notifyUrl, OrderNo);
                    WxPayData wxJsApiParam       = jsApiPay.GetJsApiParameters();

                    wxOrder = new WxPayOrder()
                    {
                        appId      = wxJsApiParam.GetValue("appId").ToString(),
                        nonceStr   = wxJsApiParam.GetValue("nonceStr").ToString(),
                        package    = wxJsApiParam.GetValue("package").ToString(),
                        paySign    = wxJsApiParam.GetValue("paySign").ToString(),
                        signType   = "MD5",
                        timeStamp  = wxJsApiParam.GetValue("timeStamp").ToString(),
                        OrderNo    = OrderNo,
                        EduOrderNo = order.OrderId
                    };
                }
                else
                {
                    wxOrder = new WxPayOrder()
                    {
                        IntMsg    = -1,
                        IsSuccess = false,
                        ErrorMsg  = "请重新登陆",
                    };
                }
            }
            catch (Exception ex)
            {
                NLogHelper.ErrorTxt($"WXPayController Pay:{ex.Message}");
                wxOrder = new WxPayOrder()
                {
                    IsSuccess = false,
                    ErrorMsg  = "支付失败,请联系客服",
                };
            }

            return(wxOrder);
        }
Ejemplo n.º 3
0
        public WxPayOrder JSPay(int feeAmt, int courseTime)
        {
            WxPayOrder wxOrder = null;
            var        us      = GetUserSession(false);

            try
            {
                if (us != null)
                {
                    string   notifyUrl = "http://edu.iqiban.cn/api/wxPay/Notify";
                    JsApiPay jsApiPay  = new JsApiPay();
                    jsApiPay.openid    = us.OpenId;
                    jsApiPay.total_fee = feeAmt * 100;


                    string OrderNo = WxPayApi.GenerateOutTradeNo();
                    var    desc    = "扫码支付学费";
                    if (courseTime > 0)
                    {
                        desc += $"[{courseTime}]课时";
                    }
                    /* 生成 系统订单 */
                    ECoursePrice eCoursePrice = _CourseSrv.GetCoursePrice("VIP");
                    eCoursePrice.Qty   = courseTime;
                    eCoursePrice.Price = feeAmt;
                    var order = _BusinessSrv.PayCourseOrder(us.OpenId, eCoursePrice);

                    WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(desc, notifyUrl, OrderNo);
                    WxPayData wxJsApiParam       = jsApiPay.GetJsApiParameters();

                    wxOrder = new WxPayOrder()
                    {
                        appId      = wxJsApiParam.GetValue("appId").ToString(),
                        nonceStr   = wxJsApiParam.GetValue("nonceStr").ToString(),
                        package    = wxJsApiParam.GetValue("package").ToString(),
                        paySign    = wxJsApiParam.GetValue("paySign").ToString(),
                        signType   = "MD5",
                        timeStamp  = wxJsApiParam.GetValue("timeStamp").ToString(),
                        OrderNo    = OrderNo,
                        EduOrderNo = order.OrderId
                    };
                }
                else
                {
                    wxOrder = new WxPayOrder()
                    {
                        IntMsg    = -1,
                        IsSuccess = false,
                        ErrorMsg  = "请重新登陆",
                    };
                }
            }
            catch (Exception ex)
            {
                NLogHelper.ErrorTxt($"JSPay:{ex.Message}");
                wxOrder = new WxPayOrder()
                {
                    IsSuccess = false,
                    ErrorMsg  = ex.Message,
                };
            }

            return(wxOrder);
        }
Ejemplo n.º 4
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"));
            }
        }