/// <summary> /// API提交 /// </summary> /// <typeparam name="T">结果类型</typeparam> /// <param name="app">App</param> /// <param name="request">请求</param> /// <param name="code">异常结果</param> /// <returns></returns> private async Task <T> SubmitAsync <T>(App app, WeChatPayRequest request, WeChatPayResultCode code) where T : WeChatPayResponse { T response = await WeChatPayClient.ExecuteAsync <T>(app, request); if (!response.Success) { throw new MaxException(code, response.Detail); } return(response); }
///// <summary> 行程定制 ///// 取消定制订单 ///// </summary> ///// <param name="request">The request.</param> ///// <returns>Task<Response>.</returns> ///// <exception cref="NotImplementedException"></exception> //public async Task<Response> CancelCustomize(CancelCustomizeRequest request) //{ // var userId = contextAccessor.UserId(); // var userInfo = await this.strokeContext.Users.FirstOrDefaultAsync(x => x.Id == userId); // if (userInfo == null) // { // throw new Exception("获取用户信息失败"); // } // var customInfo = await this.strokeContext.Customizes.FirstOrDefaultAsync(x => x.Id == request.CustomizeId); // if (customInfo==null) // { // throw new Exception("获取定制信息"); // } // if ((int)customInfo.Status==-1) // { // throw new Exception("此定制订单已经取消"); // } // if ((int)customInfo.Status>=(int)CustomizeStatus.Agree) // { // throw new Exception("已经给您定制,不能取消"); // } // customInfo.Status = CustomizeStatus.Cancel; // customInfo.UpdateTime = DateTime.Now; // this.strokeContext.Customizes.Update(customInfo); // this.strokeContext.SaveChanges(); // return Response.Success(); //} ///// <summary> ///// 确认定制行程状态完成 ///// </summary> ///// <param name="request">The request.</param> ///// <returns>Task<Response>.</returns> ///// <exception cref="NotImplementedException"></exception> //public async Task<Response> CompleteCustomize(CompleteCustomizeRequest request) //{ // var userId = contextAccessor.UserId(); // var userInfo = await this.strokeContext.Users.FirstOrDefaultAsync(x => x.Id == userId); // if (userInfo == null) // { // throw new Exception("获取用户信息失败"); // } // var customInfo = await this.strokeContext.Customizes.FirstOrDefaultAsync(x => x.Id == request.CustomizeId); // if (customInfo == null) // { // throw new Exception("获取定制信息"); // } // if ((int)customInfo.Status==-1) // { // throw new Exception("此定制订单已经取消"); // } // if (customInfo.Status != CustomizeStatus.Paid) // { // throw new Exception("此定制还未付款,不能确认完成"); // } // customInfo.Status = CustomizeStatus.Complete; // customInfo.UpdateTime = DateTime.Now; // this.strokeContext.Customizes.Update(customInfo); // this.strokeContext.SaveChanges(); // return Response.Success(); //} ///// <summary> ///// 定制行程支付 ///// </summary> ///// <param name="request">The request.</param> ///// <returns>Task<Response>.</returns> ///// <exception cref="NotImplementedException"></exception> //public async Task<Response<PlaceAnOrderInfoResponse>> CustiomizePay(CustiomizePayRequest request) //{ // var userId = contextAccessor.UserId(); // var userInfo = await this.strokeContext.Users.FirstOrDefaultAsync(x => x.Id == userId); // if (userInfo == null) // { // throw new Exception("获取用户信息失败"); // } // var orderInfo = await this.strokeContext.Customizes.FirstOrDefaultAsync(x => x.Id == request.CustomizesId); // if (orderInfo == null) // { // throw new Exception("获取定制行程信息失败"); // } // //支付 // var weChatPay = new WeChatPayRequest // { // Body = HttpUtility.UrlEncode("订单支付"), // TotalFee = orderInfo.TotalAmount, // SuccessUrl = request.SuccessUrl, // FailedUrl = request.FaildUrl, // NotifyUrl = "https://bcl.baocailang.com:8995/api/Payment/WeChatpayNotify", // SignType = "MD5" // }; // weChatPay.OrderIds = orderInfo.Id.ToString(); // //获取微信公众号中,微信js 支付信息 // var result = GetJsPayRedirect(weChatPay, PaymentType.WeChat); // return new Response<PlaceAnOrderInfoResponse> // { // Data = new PlaceAnOrderInfoResponse // { // MwebUrl = result, // WeChatAmount = orderInfo.TotalAmount, // OutTradeNo = orderInfo.Id.ToString(), // PaymentType = PaymentType.WeChat // } // }; //} /// <summary> /// 创建微信JsPay支付请求信息 /// </summary> /// <param name="weChatPay">The we chat pay.</param> /// <param name="paymentType">Type of the payment.</param> /// <returns>System.String.</returns> public string GetJsPayRedirect(WeChatPayRequest weChatPay, PaymentType paymentType) { Logger.LogDebug("创建支付订单信息"); var paymentInfo = CreatePaymentInfo(weChatPay.OrderIds, paymentType); Logger.LogDebug($"订单支付PaymentId:{paymentInfo.Id}"); var returnUrl = string.Format("https://bcl.baocailang.com/WeChat/JsPay"); var state = string.Format("{0}|{1}|{2}|{3}|{4}", paymentInfo.Id, weChatPay.TotalFee, "123", "456", weChatPay.Body);//申请时携带的状态参数 //var state = Guid.NewGuid().ToString("N"); //weChatPay.PaymentId = paymentInfo.Id.ToString(); //this.memoryCache.Set(state, weChatPay, TimeSpan.FromMinutes(2)); Logger.LogDebug("创建state成功---{0}", state); return(OAuthApi.GetAuthorizeUrl(this.weChatOptions.AppId, returnUrl, state, Senparc.Weixin.MP.OAuthScope.snsapi_base)); }
/// <summary> /// 未支付订单的支付 /// </summary> /// <param name="request">The request.</param> /// <returns>Task<Response<OrderPayResponse>>.</returns> /// <exception cref="NotImplementedException"></exception> public async Task <Response <OrderPayResponse> > OrderPay(OrderPayRequest request) { var userId = contextAccessor.UserId(); var userInfo = await this.strokeContext.Users.FirstOrDefaultAsync(x => x.Id == userId); if (userInfo == null) { throw new Exception("获取用户信息失败"); } if (!userInfo.IsActive) { throw new Exception("此用户已经被禁用"); } var orderInfo = await this.strokeContext.Orders.FirstOrDefaultAsync(x => x.Id == request.OrderId); if (orderInfo == null) { throw new Exception("获取订单信息失败"); } //支付 var weChatPay = new WeChatPayRequest { Body = HttpUtility.UrlEncode(orderInfo.Title), TotalFee = orderInfo.TotalAmount, SuccessUrl = request.SuccessUrl, FailedUrl = request.FaildUrl, NotifyUrl = weChatOptions.HostUrl + "/api/Payment/WeChatpayNotify", SignType = "MD5", OrderIds = orderInfo.Id.ToString() }; //获取微信公众号中,微信js 支付信息 var result = GetJsPayRedirect(weChatPay, PaymentType.WeChat); Logger.LogDebug($"支付链接-----{result}"); return(new Response <OrderPayResponse> { Data = new OrderPayResponse { MwebUrl = result } }); }
/// <summary> /// ExecuteAsync /// </summary> /// <typeparam name="T"></typeparam> /// <param name="app"></param> /// <param name="request"></param> /// <returns></returns> public static async Task <T> ExecuteAsync <T>(App app, WeChatPayRequest request) { //生成Xml string xml = Utility.Build(app, request); //提交 string result = await HttpClient.PostXml(request.Action, xml); //解析对象 T data = Utility.Get <T>(result, app.Key, app.SignType); WeChatPayResponse response = data as WeChatPayResponse; response.Success = response.ReturnCode == TAG_SUCCESS && response.ResultCode == TAG_SUCCESS; response.Message = string.IsNullOrWhiteSpace(response.ErrCodeDes) ? response.ReturnMsg : response.ErrCodeDes; response.Detail = $"{response.ReturnCode}:{response.ReturnMsg}/{response.ResultCode}:{response.ErrCode}:{response.ErrCodeDes}"; return(data); }
/// <summary> /// 生成Xml请求 /// </summary> /// <param name="app"></param> /// <param name="request"></param> /// <returns></returns> public static string Build(App app, WeChatPayRequest request) { //构建请求参数 request.Build(); //构建公共参数 request.Params.Add("appid", app.AppId); request.Params.Add("mch_id", app.MchId); request.Params.Add("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); request.Params.Add("sign_type", app.SignType); request.Params.Add("sign", Signature.Sign(request.Params, app.Key, app.SignType)); //ToXml StringBuilder xml = new StringBuilder(); xml.Append("<xml>"); xml.AppendJoin("", request.Params.Select(x => $"<{x.Key}>{x.Value}</{x.Key}>")); xml.Append("</xml>"); return(xml.ToString()); }
/// <summary> /// 行程支付 /// </summary> /// <param name="request">The request.</param> /// <returns>Task<Response<PlaceAnOrderInfoResponse>>.</returns> /// <exception cref="NotImplementedException"></exception> public async Task <Response <PlaceAnOrderInfoResponse> > StrokePay(StrokePayRequest request) { var userId = contextAccessor.UserId(); var userInfo = await this.strokeContext.Users.FirstOrDefaultAsync(x => x.Id == userId); if (userInfo == null) { throw new Exception("获取用户信息失败"); } if (string.IsNullOrEmpty(userInfo.Cphone)) { return(new Response <PlaceAnOrderInfoResponse> { Code = 200, Message = "手机号未绑定" }); } var strokeInfo = await this.strokeContext.Strokes.FirstOrDefaultAsync(x => x.Id == request.StrokeId); if (strokeInfo == null) { throw new Exception("获取行程信息失败"); } #region 添加订单 long shareUser = 0; long.TryParse(request.ShareCode, out shareUser); long fromSource = 0; long.TryParse(request.From, out fromSource); Logger.LogDebug($"用户来源-from:{fromSource},分享用户:{shareUser}"); var orderInfo = this.strokeContext.Orders.Add(new Order { OrderNo = DateTime.Now.ToString("yyyyMMddHHmmssffff"), UserId = userId, StrokeId = strokeInfo.Id, Title = strokeInfo.Title, City = strokeInfo.City, Destination = strokeInfo.Destination, ImageUrl = strokeInfo.ImageUrl, NumberDay = strokeInfo.NumberDay, From = (UserFrom)fromSource, ShareUser = shareUser, OrderStatus = OrderStatus.NoPaid, Price = strokeInfo.Price, TotalAmount = strokeInfo.SpecialPrice, Payment = PaymentType.WeChat, Remark = "", TotalCount = 1 }); #endregion this.strokeContext.SaveChanges(); //支付 var weChatPay = new WeChatPayRequest { Body = HttpUtility.UrlEncode(orderInfo.Entity.Title), TotalFee = orderInfo.Entity.TotalAmount, SuccessUrl = request.SuccessUrl, FailedUrl = request.FaildUrl, NotifyUrl = this.weChatOptions.HostUrl + "/api/Payment/WeChatpayNotify", SignType = "MD5", OrderIds = orderInfo.Entity.Id.ToString() }; //获取微信公众号中,微信js 支付信息 var result = GetJsPayRedirect(weChatPay, PaymentType.WeChat); Logger.LogDebug($"支付链接-----{result}"); return(new Response <PlaceAnOrderInfoResponse> { Data = new PlaceAnOrderInfoResponse { OutTradeNo = orderInfo.Entity.Id.ToString(), PaymentType = PaymentType.WeChat, WeChatAmount = orderInfo.Entity.TotalAmount, MwebUrl = result } }); }