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"); }
/// <summary> /// 微信回调 /// </summary> /// <returns></returns> public ContentResult WexinPayNotify() { ResponseHandler resHandler = new ResponseHandler(null); string return_code = resHandler.GetParameter("return_code"); string return_msg = resHandler.GetParameter("return_msg"); string res = null; //TODO:这里需要验证签名 ////验证请求是否从微信发过来(安全) logger.Info("微信回调"+resHandler.ParseXML()); //if (resHandler.IsTenpaySign()) //{ // res = "success"; // logger.Info(resHandler.ParseXML()); // //正确的订单处理 //} //else //{ // res = "wrong"; // //错误的订单处理 //} if (return_code.ToLower() == "SUCCESS".ToLower()) { OrderService.UpdateOrderPay(new OrderPayModel() { PayId = resHandler.GetParameter("prepay_id"), Status = 1 }); } res = "success"; //订单处理 string xml = string.Format(@"<xml><return_code><![CDATA[{0}]]></return_code><return_msg><![CDATA[{1}]]></return_msg></xml>", return_code, return_msg); logger.Info("微信返回值"+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 ActionResult NativeNotifyUrl() { ResponseHandler resHandler = new ResponseHandler(null); //返回给微信的请求 RequestHandler res = new RequestHandler(null); string openId = resHandler.GetParameter("openid"); string productId = resHandler.GetParameter("product_id"); if (openId == null || productId == null) { res.SetParameter("return_code", "FAIL"); res.SetParameter("return_msg", "回调数据异常"); } //创建支付应答对象 //RequestHandler packageReqHandler = new RequestHandler(null); var sp_billno = DateTime.Now.ToString("HHmmss") + TenPayV3Util.BuildRandomStr(28); var nonceStr = TenPayV3Util.GetNoncestr(); //创建请求统一订单接口参数 //packageReqHandler.SetParameter("appid", TenPayV3Info.AppId); //packageReqHandler.SetParameter("mch_id", TenPayV3Info.MchId); //packageReqHandler.SetParameter("nonce_str", nonceStr); //packageReqHandler.SetParameter("body", "test"); //packageReqHandler.SetParameter("out_trade_no", sp_billno); //packageReqHandler.SetParameter("total_fee", "1"); //packageReqHandler.SetParameter("spbill_create_ip", Request.UserHostAddress); //packageReqHandler.SetParameter("notify_url", TenPayV3Info.TenPayV3Notify); //packageReqHandler.SetParameter("trade_type", TenPayV3Type.NATIVE.ToString()); //packageReqHandler.SetParameter("openid", openId); //packageReqHandler.SetParameter("product_id", productId); //string sign = packageReqHandler.CreateMd5Sign("key", TenPayV3Info.Key); //packageReqHandler.SetParameter("sign", sign); //string data = packageReqHandler.ParseXML(); var xmlDataInfo = new TenPayV3RequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, "test", sp_billno, 1, Request.UserHostAddress, TenPayV3Info.TenPayV3Notify, TenPayV3Type.JSAPI, productId, TenPayV3Info.Key, nonceStr); try { //调用统一订单接口 var result = TenPayV3.Unifiedorder(xmlDataInfo); //var unifiedorderRes = XDocument.Parse(result); //string prepayId = unifiedorderRes.Element("xml").Element("prepay_id").Value; //创建应答信息返回给微信 res.SetParameter("return_code", result.return_code); res.SetParameter("return_msg", result.return_msg ?? "OK"); res.SetParameter("appid", result.appid); res.SetParameter("mch_id", result.mch_id); res.SetParameter("nonce_str", result.nonce_str); res.SetParameter("prepay_id", result.prepay_id); res.SetParameter("result_code", result.result_code); res.SetParameter("err_code_des", "OK"); string nativeReqSign = res.CreateMd5Sign("key", TenPayV3Info.Key); res.SetParameter("sign", result.sign); } catch (Exception) { res.SetParameter("return_code", "FAIL"); res.SetParameter("return_msg", "统一下单失败"); } return Content(res.ParseXML()); }
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"; } }
public ActionResult PayNotifyUrl() { ResponseHandler resHandler = new ResponseHandler(null); string result_code = resHandler.GetParameter("result_code"); string appid = resHandler.GetParameter("appid"); string mch_id = resHandler.GetParameter("mch_id"); string device_info = resHandler.GetParameter("device_info"); string nonce_str = resHandler.GetParameter("nonce_str"); string sign = resHandler.GetParameter("sign"); string err_code = resHandler.GetParameter("err_code"); string err_code_des = resHandler.GetParameter("err_code_des"); string openid = resHandler.GetParameter("openid"); string is_subscribe = resHandler.GetParameter("is_subscribe"); string trade_type = resHandler.GetParameter("trade_type"); string bank_type = resHandler.GetParameter("bank_type"); string total_fee = resHandler.GetParameter("total_fee"); string coupon_fee = resHandler.GetParameter("coupon_fee"); string fee_type = resHandler.GetParameter("fee_type"); string transaction_id = resHandler.GetParameter("transaction_id"); string out_trade_no = resHandler.GetParameter("out_trade_no"); string attach = resHandler.GetParameter("attach"); string time_end = resHandler.GetParameter("time_end"); var fileStream = System.IO.File.OpenWrite(Server.MapPath("~/1.txt")); fileStream.Write(Encoding.Default.GetBytes(result_code), 0, Encoding.Default.GetByteCount(result_code)); fileStream.Close(); return Content("success"); }