/// <summary> /// 立即购买 -> 确认收货地址 -> 确认支付 /// </summary> /// <returns></returns> public ActionResult PayApply() { //Init bool flag = true; string redirectUrl = "/WeShop/OrderList"; //Request string openid = Request.QueryString["openid"]; string total_fee = Request.QueryString["total_fee"]; string orderNum = Request.QueryString["ordernum"]; string commTitle = Request.QueryString["title"]; string strContent = Request.QueryString["content"]; ViewBag.Content = strContent; //检测是否给当前页面传递了相关参数 if (string.IsNullOrEmpty(openid) || string.IsNullOrEmpty(total_fee)) { //Response.Write("<span style='color:#FF0000;font-size:20px'>" + "页面传参出错,请返回重试" + "</span>"); Log.Error(this.GetType().ToString(), "This page have not get params, cannot be inited, exit..."); flag = false; ViewBag.CanSubmit = flag; ViewBag.RedirectUrl = redirectUrl; ViewBag.Msg = "页面传参出错,请返回重试!"; return View(); } //若传递了相关参数,则调统一下单接口,获得后续相关接口的入口参数 JsApiPayCNa jsApiPay = new JsApiPayCNa(this.HttpContext); jsApiPay.openid = openid; jsApiPay.total_fee = int.Parse(total_fee) * 100; //JSAPI支付预处理 try { //定制化具体参数 JsPayApiParam par = new JsPayApiParam(); par.out_trade_no = orderNum; par.body = commTitle; par.attach = strContent; par.goods_tag = string.Empty; WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(par); string wxJsApiParam = jsApiPay.GetJsApiParameters();//获取H5调起JS API参数 Session["wxJsApiParam"] = wxJsApiParam; Log.Debug(this.GetType().ToString(), "wxJsApiParam : " + wxJsApiParam); ////在页面上显示订单信息 //Response.Write("<span style='color:#00CD00;font-size:20px'>订单详情:</span><br/>"); //Response.Write("<span style='color:#00CD00;font-size:20px'>" + unifiedOrderResult.ToPrintStr() + "</span>"); } catch (Exception ex) { //LogFileHelper.WriteLogByTxt("/WeToken/PayApply页面下单异常:" + ex.Message); //Response.Write("<span style='color:#FF0000;font-size:20px'>" + "下单失败,请返回重试" + "</span>"); flag = false; ViewBag.RedirectUrl = redirectUrl; ViewBag.Msg = "下单失败,请返回重试!"; ViewBag.CanSubmit = flag; LogFileHelper.WriteLogByTxt("PayApply页面生成 wxJsApiParam 异常信息:" + ex.Message); return View(); } ViewBag.CanSubmit = flag; ViewBag.RedirectUrl = string.Empty; ViewBag.Msg = string.Empty; return View(); }
//重写 /** * 调用统一下单,获得下单结果 * @return 统一下单结果 * @失败时抛异常WxPayException */ public WxPayData GetUnifiedOrderResult(JsPayApiParam par) { //统一下单 WxPayData data = new WxPayData(); data.SetValue("body", par.body); data.SetValue("attach", par.attach); data.SetValue("out_trade_no", par.out_trade_no); data.SetValue("total_fee", total_fee); data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); data.SetValue("time_expire", DateTime.Now.AddMinutes(par.ValidMinute).ToString("yyyyMMddHHmmss")); data.SetValue("goods_tag", par.goods_tag); data.SetValue("trade_type", "JSAPI"); if (string.IsNullOrEmpty(openid)) { openid = page.Session["openid"].ToString(); } data.SetValue("openid", openid); WxPayData result = WxPayApi.UnifiedOrder(data); if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { Log.Error(this.GetType().ToString(), "UnifiedOrder response error!"); throw new WxPayException("UnifiedOrder response error!"); } unifiedOrderResult = result; return result; }