/// <summary> /// 小程序下单 /// </summary> /// <param name="oIds">订单Id</param> /// <param name="code">临时登录凭证</param> /// <returns></returns> public ActionResult WeXcxPay(int[] oIds, string code) { #region 验证订单是否有效,并合计价格 //订单价格 decimal payPrice = 0; //订单描述 string detail = ""; //验证订单..... #endregion #region 统一下单 try { //支付回调通知地址 var address = WebConfigurationManager.AppSettings["WxXcxNotifyUrl"].ToString(); XcxPayData data = new XcxPayData(); data.SetValue("body", "商品购买"); //可以将用户Id和订单Id同时封装在attach中 data.SetValue("attach", String.Join(",", oIds).ToString()); Random rd = new Random(); //外部商户订单号 var payNum = DateTime.Now.ToString("yyyyMMddHHmmss") + rd.Next(0, 1000).ToString().PadLeft(3, '0'); data.SetValue("out_trade_no", payNum); data.SetValue("detail", detail.Substring(0, detail.Length - 1)); data.SetValue("total_fee", Convert.ToInt32(payPrice * 100)); data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss")); data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss")); data.SetValue("notify_url", address); //data.SetValue("goods_tag", "test"); data.SetValue("trade_type", "JSAPI"); data.SetValue("openid", WeHelper.Code2Session(code).openid); XcxPayData result = XcxPayApi.UnifiedOrder(data); var flag = true; var msg = ""; var nonceStr = ""; var appId = ""; var package = ""; var mch_id = ""; if (!result.IsSet("appid") || !result.IsSet("prepay_id") || result.GetValue("prepay_id").ToString() == "") { flag = false; msg = "下单失败"; return(Json(new { Result = false, Msg = "下单失败!" })); } else { //统一下单 ///TO Do...... /// 修改订单状态 nonceStr = result.GetValue("nonce_str").ToString(); appId = result.GetValue("appid").ToString(); mch_id = result.GetValue("mch_id").ToString(); package = "prepay_id=" + result.GetValue("prepay_id").ToString(); } var signType = "MD5"; var timeStamp = ((DateTime.Now.Ticks - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).Ticks) / 10000).ToString(); XcxPayData applet = new XcxPayData(); applet.SetValue("appId", appId); applet.SetValue("nonceStr", nonceStr); applet.SetValue("package", package); applet.SetValue("signType", signType); applet.SetValue("timeStamp", timeStamp); var appletSign = applet.MakeSign(); return(Json(new { timeStamp, nonceStr, package, signType, paySign = appletSign, Result = flag, msg })); } catch (Exception ex) { return(Json(new { Result = false, msg = "缺少参数" })); } #endregion }