public string WxNotify() { LogHelper.Info("微信回调成功"); Senparc.Weixin.MP.TenPayLibV3.ResponseHandler payNotifyRepHandler = new Senparc.Weixin.MP.TenPayLibV3.ResponseHandler(null); payNotifyRepHandler.SetKey(key); string return_code = payNotifyRepHandler.GetParameter("return_code"); string return_msg = payNotifyRepHandler.GetParameter("return_msg"); string xml = string.Format(@"<xml><return_code><![CDATA[{0}]]></return_code><return_msg><![CDATA[{1}]]></return_msg></xml>", return_code, return_msg); // log.Info(xml); if (return_code.ToUpper() != "SUCCESS") { return(xml); } string out_trade_no = payNotifyRepHandler.GetParameter("out_trade_no"); LogHelper.Info("订单号:" + out_trade_no); //log.Info("微信回调订单号:-" + out_trade_no); //微信服务器可能会多次推送到本接口,这里需要根据out_trade_no去查询订单是否处理,如果处理直接返回:return Content(xml, "text/xml"); 不跑下面代码 //if (false) //{ if (payNotifyRepHandler.IsTenpaySign()) { LogHelper.Info("微信验证成功;订单号:" + out_trade_no); if (_service.AlertComeOutRecord(out_trade_no, 1)) { return(xml); } else { //如果订单修改失败,需要微信再次发送请求 xml = string.Format(@"<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[]]></return_msg></xml>"); return(xml); } } else { xml = string.Format(@"<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[]]></return_msg></xml>"); return(xml); } }
public ActionResult BuyDemandPayNotifyUrl() { ResponseHandler resHandler = new ResponseHandler(null); string return_code = resHandler.GetParameter("return_code"); string return_msg = resHandler.GetParameter("return_msg"); try { resHandler.SetKey(TenPayV3Info.Key); //验证请求是否从微信发过来(安全) if (resHandler.IsTenpaySign()) { //正确的订单处理 string out_trade_no = resHandler.GetParameter("out_trade_no"); string total_fee = resHandler.GetParameter("total_fee"); //微信支付订单号 string transaction_id = resHandler.GetParameter("transaction_id"); //支付完成时间 string time_end = resHandler.GetParameter("time_end"); LogService.LogWexin("处理需求购买结果通知", "订单号:" + out_trade_no + " 交易流水号:" + transaction_id + " 支付完成时间:" + time_end); OrderService orderService = new OrderService(); TradeOrder order = orderService.GetOrderByOrderId(out_trade_no); if (order == null) { return_code = "FAIL"; return_msg = "根据返回的订单编号(" + out_trade_no + ")未查询到相应交易订单。"; } else if (order.State == (int)OrderState.Succeed) { return_code = "SUCCESS"; return_msg = "OK"; } else if ((order.Amount * 100) != Convert.ToDecimal(total_fee)) { //无效支付结果 orderService.UpdateOrderState(order.OrderId, (int)OrderState.Invalid); return_code = "FAIL"; return_msg = "交易金额与订单金额不一致"; } else { //交易成功 if (orderService.UpdateOrderState(order.OrderId, (int)OrderState.Succeed)) { return_code = "SUCCESS"; return_msg = "OK"; } else { return_code = "FAIL"; return_msg = "更新订单失败"; } } } else { return_code = "FAIL"; return_msg = "非法支付结果通知"; //错误的订单处理 LogService.LogWexin("接收到非法微信支付结果通知", return_msg); } } catch (Exception ex) { return_code = "FAIL"; return_msg = ex.ToString(); LogService.LogWexin("处理需求购买结果通知", ex.ToString()); } string xml = string.Format(@" <xml> <return_code><![CDATA[{0}]]></return_code> <return_msg><![CDATA[{1}]]></return_msg> </xml>", return_code, return_msg); LogService.LogWexin("处理需求购买结果通知", xml); return Content(xml, "text/xml"); }
public ActionResult PayNotifyUrl() { ResponseHandler resHandler = new ResponseHandler(null); string return_code = resHandler.GetParameter("return_code"); string return_msg = resHandler.GetParameter("return_msg"); string res = null; resHandler.SetKey(TenPayV3Info.Key); //验证请求是否从微信发过来(安全) if (resHandler.IsTenpaySign()) { res = "success"; //正确的订单处理 } else { res = "wrong"; //错误的订单处理 } var fileStream = System.IO.File.OpenWrite(Server.MapPath("~/1.txt")); fileStream.Write(Encoding.Default.GetBytes(res), 0, Encoding.Default.GetByteCount(res)); fileStream.Close(); string xml = string.Format(@"<xml> <return_code><![CDATA[{0}]]></return_code> <return_msg><![CDATA[{1}]]></return_msg> </xml>", return_code, return_msg); return Content(xml, "text/xml"); }
public ContentResult WexinPayNotify() { ResponseHandler resHandler = new ResponseHandler(null); string return_code = resHandler.GetParameter("return_code"); string return_msg = resHandler.GetParameter("return_msg"); resHandler.SetKey(PayKey); string res = null; //TODO:这里需要验证签名 ////验证请求是否从微信发过来(安全) //logger.Info("IsTenpaySign:" + resHandler.IsTenpaySign()); if (resHandler.IsTenpaySign()) { try { //订单处理 if (return_code.ToLower() == "SUCCESS".ToLower()) { string out_trade_no = resHandler.GetParameter("out_trade_no"); long orderId=0; if (!string.IsNullOrEmpty(out_trade_no)) { orderId = Convert.ToInt64(out_trade_no); } logger.Info("orderId:" + orderId+"|out_trade_no="+out_trade_no); //判断老订单和预售订单 var flag = OrderService.CheckOrderExist(orderId); if (flag) { OrderService.UpdateOrderPay(new OrderPayModel() { OrdeId = orderId, //long.Parse(resHandler.GetParameter("out_trade_no")), Status = OrderStatus.Paid.GetHashCode() }); } else { PreSaleOrderService.ModifyPreOrder(new PreSaleOrder() {OrderId = orderId,Status = 1}); var preOrder = PreSaleOrderService.GetPreSaleOrder(orderId); var mobile = preOrder.Phone; SMSService.SendSMSPreOrderCreated(mobile, "12月19日"); } } res = "SUCCESS"; } catch (Exception e) { logger.Error("微信支付回调错误:" + e); res = "FAIL"; } string xml = string.Format(@"<xml><return_code><![CDATA[{0}]]></return_code><return_msg><![CDATA[{1}]]></return_msg></xml>", res, return_msg); return Content(xml, "text/xml"); } else { return Content(""); } }
/// <summary> /// 回调处理 /// </summary> /// <param name="wxKey">微信支付授权KEY</param> /// <param name="success"></param> /// <param name="fail"></param> /// <returns></returns> public static string ProcessNotify(string wxKey, NotifySuccess success, NotifyFail fail) { NotyfyResult result = new NotyfyResult(); ResponseHandler resHandler = new ResponseHandler(null); try { result.Content = resHandler.ParseXML(); string openid = resHandler.GetParameter("openid"); string out_trade_no = resHandler.GetParameter("out_trade_no"); string transaction_id = resHandler.GetParameter("transaction_id"); string total_fee = resHandler.GetParameter("total_fee"); result = new NotyfyResult() { Content = resHandler.ParseXML(), out_trade_no = out_trade_no, openid = openid, transaction_id = transaction_id, total_fee = total_fee, appid = resHandler.GetParameter("appid"), fee_type = resHandler.GetParameter("fee_type"), is_subscribe = resHandler.GetParameter("is_subscribe"), mch_id = resHandler.GetParameter("mch_id"), result_code = resHandler.GetParameter("result_code"), time_end = resHandler.GetParameter("time_end"), }; resHandler.SetKey(wxKey); bool signResult = resHandler.IsTenpaySign(); if (signResult) { success(result); return "success"; } else { fail(result); return "error"; } } catch (Exception ex) { result.Content = ex.Message; fail(result); return "error"; } }