/// <summary> /// 统一下单 /// </summary> /// <param name="unifiedOrderInfo"></param> /// <param name="errMessage"></param> /// <returns></returns> public JsAPIParameter UnifiedOrder(UnifiedOrderInfo unifiedOrderInfo, out string errMessage) { if (unifiedOrderInfo == null) { errMessage = "下单数据为空"; return null; } if (!unifiedOrderInfo.CheckRequired(out errMessage)) { return null; } var paymentInfoRepository = DependencyManager.Resolve<IPaymentInfoRepository>(); //判断当前订单号是否存在 var orderInfo = paymentInfoRepository.GetPaymentInfoBySysOrderNo(unifiedOrderInfo.PayModuleName, unifiedOrderInfo.OutTradeNo); if (orderInfo != null && orderInfo.Status != 0 && orderInfo.OpenId.Equals(unifiedOrderInfo.Openid)) //0表示尚未支付 { errMessage = "系统异常,请重新下单操作"; return null; } //调用微信统一下单接口 var appInfo = DependencyManager.Resolve<IAppInfoRepository>().GetAppInfo(unifiedOrderInfo.AppId); var payment = DependencyManager.Resolve<Weixin.Repository.IPaymentInfoRepository>() .GetModelByAppId(unifiedOrderInfo.AppId); if (string.IsNullOrEmpty(appInfo.AppId) || string.IsNullOrEmpty(payment.MchId) || string.IsNullOrEmpty(payment.Paykey)) { errMessage = "当前公众号支付配置不完整"; return null; } var packageReqHandler = new RequestHandler(default(HttpContext)); //初始化 packageReqHandler.Init(); var nonceStr = TenPayV3Util.GetNoncestr(); //设置package订单参数 packageReqHandler.SetParameter("appid", appInfo.AppId); //公众账号ID packageReqHandler.SetParameter("mch_id", payment.MchId); //商户号 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("body", unifiedOrderInfo.Body); //商品描述 packageReqHandler.SetParameter("attach", unifiedOrderInfo.PayModuleName); //向微信传递系统支付模块ID packageReqHandler.SetParameter("out_trade_no", unifiedOrderInfo.OutTradeNo); //商家订单号 //debug模式下,单位为 packageReqHandler.SetParameter("total_fee", PayHelper.IsDebug ? (unifiedOrderInfo.TotalFee / 100).ToString() : (unifiedOrderInfo.TotalFee * 100).ToString()); packageReqHandler.SetParameter("spbill_create_ip", "1.1.1.1"); //用户的公网ip,不是商户服务器IP packageReqHandler.SetParameter("notify_url", PayHelper.GetPayNotifyUrl()); //接收财付通通知的URL packageReqHandler.SetParameter("trade_type", TenPayV3Type.JSAPI.ToString()); //交易类型 packageReqHandler.SetParameter("openid", unifiedOrderInfo.Openid); //用户的openId var sign = packageReqHandler.CreateMd5Sign("key", payment.Paykey); packageReqHandler.SetParameter("sign", sign); //签名 var data = packageReqHandler.ParseXML(); //同意下单,获取到预付订单号 var unifiedOrderResult = TenPayV3Helper.Unifiedorder(data); var rtnUnifiedOrderResult = new UnifiedOrderResponse(unifiedOrderResult); //下单成功,保存下单对象 if (rtnUnifiedOrderResult.IsSuccess) { var paymentInfo = new PaymentInfo { PaymentId = Guid.NewGuid(), AppId = unifiedOrderInfo.AppId, CreateTime = DateTime.Now, Description = "无", Body = unifiedOrderInfo.Body, ModuleName = unifiedOrderInfo.PayModuleName, OrderCode = unifiedOrderInfo.OutTradeNo, OrderId = unifiedOrderInfo.OrderId, OpenId = unifiedOrderInfo.Openid, PayAmount = unifiedOrderInfo.TotalFee, WxOrderCode = rtnUnifiedOrderResult.prepay_id, ModifyTime = DateTime.Now, Status = 0 }; paymentInfoRepository.Add(paymentInfo); var jsApiParameters = rtnUnifiedOrderResult.GetJsApiParameters(payment.Paykey); return jsApiParameters; } errMessage = "下单失败"; return null; }
public void UnifiedOrder(string request) { try { var requestModel = JSONHelper.Deserialize <UnifiedOrderEntity>(request); //通过wid获取公众号的信息 var wxModel = new BLL.wx_userweixin().GetModel(requestModel.wid); var wxPayInfo = new BLL.wx_payment_wxpay().GetModelByWid(requestModel.wid); var packageReqHandler = new RequestHandler(null); //初始化 packageReqHandler.Init(); var timeStamp = TenPayV3Util.GetTimestamp(); var nonceStr = TenPayV3Util.GetNoncestr(); //设置package订单参数 packageReqHandler.SetParameter("appid", wxModel.AppId); //公众账号ID packageReqHandler.SetParameter("mch_id", wxPayInfo.mch_id); //商户号 packageReqHandler.SetParameter("nonce_str", nonceStr); //随机字符串 packageReqHandler.SetParameter("body", requestModel.body); //商品描述 packageReqHandler.SetParameter("attach", requestModel.PayModuleID.ToString()); //向微信传递系统支付模块ID packageReqHandler.SetParameter("out_trade_no", requestModel.out_trade_no); //商家订单号 //debug模式下,穿的单位为分 packageReqHandler.SetParameter("total_fee", PayHelper.IsDebug ? requestModel.total_fee.ToString() : (requestModel.total_fee * 100).ToString()); //packageReqHandler.SetParameter("spbill_create_ip", wxPayInfo); //用户的公网ip,不是商户服务器IP packageReqHandler.SetParameter("notify_url", HttpContext.Current.Request.Url.ToString().ToLower().Replace("wechatservice.asmx", "PayNotify.aspx")); //接收财付通通知的URL packageReqHandler.SetParameter("trade_type", TenPayV3Type.JSAPI.ToString()); //交易类型 packageReqHandler.SetParameter("openid", requestModel.openid); //用户的openId var sign = packageReqHandler.CreateMd5Sign("key", wxPayInfo.paykey); packageReqHandler.SetParameter("sign", sign); //签名 var data = packageReqHandler.ParseXML(); //同意下单,获取到预付订单号 var unifiedOrderResult = TenPayV3Helper.Unifiedorder(data); var rtnUnifiedOrderResult = new UnifiedOrderResponse(unifiedOrderResult); //下单成功,保存下单对象 if (rtnUnifiedOrderResult.IsSuccess) { var paymentInfo = new PaymentInfo { PaymentId = Guid.NewGuid(), Wid = requestModel.wid, CreateTime = DateTime.Now, Description = "无", ShopName = requestModel.body, ModuleName = ((PayModuleEnum)requestModel.PayModuleID).ToString(), OrderCode = requestModel.out_trade_no, OrderId = requestModel.OrderId, Pid = requestModel.openid, PayAmount = requestModel.total_fee, WXOrderCode = rtnUnifiedOrderResult.prepay_id, ModifyTime = DateTime.Now, Status = 0 }; paymentInfo.Add(); } //var jsApiParameters = rtnUnifiedOrderResult.GetJsApiParameters("4A5E7B87F3324A6DA22E55FDC12150B6"); var jsApiParameters = rtnUnifiedOrderResult.GetJsApiParameters(wxPayInfo.paykey); HttpContext.Current.Response.Write(AjaxResult.Success(jsApiParameters)); } catch (Exception exception) { HttpContext.Current.Response.Write(AjaxResult.Error(exception.Message)); } }