Esempio n. 1
0
        //public ActionResult Order()
        //{
        //    return View();
        //}

        public ActionResult Index(int id)
        {
            try
            {
                T_Order order = null;;
                using (IT_Order repository = new T_OrderRepository())
                {
                    order = repository.Find(id);
                }
                if (order != null)
                {
                    var out_trade_no       = order.OrderNo;
                    var domain             = System.Configuration.ConfigurationManager.AppSettings["Domain"];
                    var body               = "茶室预定";
                    var detail             = "倒茶茶室预定";
                    var attach             = "";
                    var product_id         = "1";
                    var openid             = User.Identity.Name;
                    var goods_tag          = "";
                    var fee_type           = "CNY";
                    var total_fee          = (int)(order.FeeCode.Value * 100);
                    var trade_type         = "JSAPI";
                    var appId              = System.Configuration.ConfigurationManager.AppSettings["AppId"];
                    var nonceStr           = Util.CreateNonce_str();
                    var timestamp          = Util.CreateTimestamp();
                    var success_redict_url = string.Format("{0}/WxPay/Success?orderNo={1}", domain, order.OrderNo);
                    var url              = domain + Request.Url.PathAndQuery;
                    var userAgent        = Request.UserAgent;
                    var userVersion      = userAgent.Substring(userAgent.LastIndexOf("MicroMessenger/") + 15, 3);//微信版本号高于或者等于5.0才支持微信支付
                    var spbill_create_ip = (trade_type == "APP" || trade_type == "NATIVE") ? Request.UserHostName : WeixinConfig.spbill_create_ip;
                    var time_start       = DateTime.Now.ToString("yyyyMMddHHmmss");
                    var time_expire      = DateTime.Now.AddMinutes(15).ToString("yyyyMMddHHmmss"); //默认1个小时订单过期,开发者可自定义其他超时机制,原则上微信订单超时时间不超过2小时

                    var notify_url   = string.Format("{0}/WxPay/Notify", domain);                  //与下面的Notify对应,开发者可自定义其他url地址
                    var partnerKey   = WeixinConfig.PartnerKey;
                    var mch_id       = WeixinConfig.mch_id;
                    var device_info  = WeixinConfig.device_info;
                    var returnXML    = "";
                    var paramaterXml = "";
                    var content      = WxPayAPI.UnifiedOrder(
                        appId, mch_id, device_info, nonceStr,
                        body, detail, attach, out_trade_no, fee_type, total_fee, spbill_create_ip, time_start, time_expire,
                        goods_tag, notify_url, trade_type, product_id, openid, partnerKey, out returnXML, out paramaterXml);
                    LogWriter.Default.WriteError(paramaterXml);
                    LogWriter.Default.WriteError(returnXML);
                    var prepay_id             = "";
                    var sign                  = "";
                    var return_code           = "";
                    var return_msg            = "";
                    var err_code              = "";
                    var err_code_des          = "";
                    var isUnifiedOrderSuccess = false;
                    if (content.return_code.Value == "SUCCESS" && content.result_code.Value == "SUCCESS")
                    {
                        prepay_id             = content.prepay_id.Value;
                        sign                  = WxPayAPI.SignPay(appId, timestamp.ToString(), nonceStr, prepay_id, partnerKey);
                        trade_type            = content.trade_type.Value;
                        isUnifiedOrderSuccess = true;
                    }
                    else
                    {
                        return_code           = content.return_code.Value;
                        return_msg            = content.return_msg.Value;
                        isUnifiedOrderSuccess = false;
                    }
                    if (!isUnifiedOrderSuccess)
                    {
                        return(RedirectToAction("Failed", new { msg = "(代码:101)服务器下单失败,请重试" }));
                    }
                    var model = new JSPayModel
                    {
                        appId              = appId,
                        nonceStr           = nonceStr,
                        timestamp          = timestamp,
                        userAgent          = userAgent,
                        userVersion        = userVersion,
                        attach             = "",
                        body               = body,
                        detail             = detail,
                        openid             = openid,
                        product_id         = out_trade_no,
                        goods_tag          = goods_tag,
                        price              = order.FeeCode.Value,
                        total_fee          = total_fee,
                        prepay_id          = prepay_id,
                        trade_type         = trade_type,
                        sign               = sign,
                        return_code        = return_code,
                        return_msg         = return_msg,
                        err_code           = err_code,
                        err_code_des       = err_code_des,
                        success_redict_url = success_redict_url
                    };

                    return(View(model));
                }
                else
                {
                    return(View());
                }
            }
            catch (Exception ex)
            {
                LogWriter.Default.WriteError(ex.Message);
                return(RedirectToAction("Failed", new { msg = "(代码:200)" + ex.Message }));
            }
        }