public JsonResult wxCreatePay(int oid, string remark = null) { OrderDTO model = _iOrderService.Get(oid); model.Shop_id = Session["fxid"].ToInt32(); if (remark != null) { model.Remark = remark;//输入验证? _iOrderService.SaveOrUpdate(model); } //先用商店id当分销id; _iOrderService.ChangeStatus(model, "付款成功"); //string teststr="{\"success\": null }"; //return Json(Message.Success(teststr));//测试用 WechatHelper wechatHelper = new WechatHelper(this.wid); string notifyUrl = "http://ms.571400yb.com/wfxShoping/api/wxNotify"; if (this.OpenId == "oEeLDwVCN9aA66-ghojAh8ptupNQ") { model.Price = 0.01M; } //第一个参数需要修改。 UnifiedOrderResponse uoRes = wechatHelper.CreateUnifiedOrder(title + " 购买商品", notifyUrl, this.OpenId, model.Order_sn, Convert.ToInt32((model.Price) * 100)); if (uoRes.return_code.ToLower() == "SUCCESS".ToLower()) { LogHelper.Info <UserController>("创建订单成功:" + wid); string timeSpan = DateTime.Now.ToFileTime().ToString().Substring(0, 9); string nonceStr = Guid.NewGuid().ToString().Replace("-", ""); model.Pay_sn = uoRes.prepay_id; string package = "prepay_id=" + uoRes.prepay_id; Dictionary <String, String> dict = new Dictionary <string, string>(); dict.Add("appId", wechatHelper.AppId); dict.Add("nonceStr", nonceStr); dict.Add("package", package); dict.Add("signType", "MD5"); dict.Add("timeStamp", timeSpan); string sign = wechatHelper.WechatPaySign(dict); String cfg = "{\"timestamp\": " + timeSpan + ", \"nonceStr\": \"" + nonceStr + "\", \"package\": \"" + package + "\", \"signType\":\"MD5\", \"paySign\": \"" + sign + "\", \"success\": null }"; return(Json(Message.Success(cfg))); } else { return(Json(Message.Error(uoRes.return_msg))); } }
public CreateOrderResult CreateOrderAsync(CreateOrderParameter createOrderParameter) { string str; if (!this.ValidateParameter <CreateOrderParameter>(createOrderParameter, out str)) { CreateOrderResult result1 = new CreateOrderResult(); result1.ErrorMessage = str; return(result1); } UnifiedOrderRequest request = new UnifiedOrderRequest { AppId = this.m_AppId, MerchantId = createOrderParameter.MerchantId, DeviceInfo = _DeviceInfo, NonceString = WeChatSignHelper.CreateRandomString(), Body = createOrderParameter.Body, Detail = createOrderParameter.Detail, Attach = createOrderParameter.Attach, OutTradeNo = createOrderParameter.OutTradeNo, FeeType = _OrderFeeType, TotalFee = (int)(createOrderParameter.TotalFee * 100.0), SpbllCreateIP = createOrderParameter.IP, TimeStart = DateTime.Now.ToString("yyyyMMddHHmmss"), TimeExpire = DateTime.Now.AddHours((double)_OrderExpireHour).ToString("yyyyMMddHHmmss"), NotifyUrl = createOrderParameter.NotifyUrl, TradeType = createOrderParameter.TradeType, ProductId = createOrderParameter.ProductId, OpenId = createOrderParameter.OpenId }; UnifiedOrderResponse response = this.InvokeApiAsync <UnifiedOrderRequest, UnifiedOrderResponse>(_UnifiedorderUrl, request); if (!response.IsSuccess) { CreateOrderResult result3 = new CreateOrderResult(); result3.ErrorCode = response.ErrCode; result3.ErrorMessage = response.ReturnMsg; return(result3); } CreateOrderResult result2 = new CreateOrderResult(); result2.IsSuccess = true; result2.CodeUrl = response.CodeUrl; result2.PrepayId = response.PrepayId; return(result2); }
public void Deserialize_输入UnifiedOrderResponse对象的xml字符串_解析正确() { //arrange string xmlStr = @" <xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <appid><![CDATA[wx2421b1c4370ec43b]]></appid> <mch_id><![CDATA[10000100]]></mch_id> <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str> <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign> <result_code><![CDATA[SUCCESS]]></result_code> <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id> <trade_type><![CDATA[JSAPI]]></trade_type> </xml>"; //act UnifiedOrderResponse target = XmlSerializerWrapper.Deserialize <UnifiedOrderResponse>(xmlStr); //assert Assert.IsTrue(target.Code == "SUCCESS"); Assert.IsTrue(target.Message == "OK"); Assert.IsTrue(target.IsSuccess); }
//支付接口 async Task <UnifiedOrderResponse> IWechatPay.Pay(UnifiedOrderRequest unifiedOrder) { //生成请求字段集合原料 var properties = this.GenerateCommonRequestEntity(); properties.Add("notify_url", this._settings.PaymentNotifyURI); //填充请求字段集合 this.FillRequestEntity(ref properties, unifiedOrder, this._payRequireProperties); //获取响应 UnifiedOrderResponse response = await this.CallAsync <UnifiedOrderResponse>( Constant.UnifiedOrder, properties); //检验响应体合法性 if (this.CheckResponseLegal(response)) { return(response); } else { return(null); } }
/// <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)); } }