public void UpdatePayTest() { Libraries.Core.ServiceLocator.Initialization(); var request = new UpdatePayStatusRequest() { Amount = new decimal(200), VoucherNo = "4200000045201711277352608869", OrderSysNo = "9aa6f97bf7404fac92ddd5e7a07889ff" }; var result = RechargeApp.Instance.UpdatePayStatus(request); Assert.IsTrue(result.Status); }
/// <summary> /// 微信通知 /// </summary> /// <example>页面只能显示success/failed 不能有任何html标签</example> /// <returns>成功返回success 失败返回其它任意对象</returns> public ActionResult PayNotifyUrlTest() { var orderNotify = new OrderNotifyImpl() { apiKey = ConfigurationManager.AppSettings["apiKey"] }; var param = new UpdatePayStatusRequest() { Amount = new decimal(200), VoucherNo = "4200000083201803148591866779", OrderSysNo = "3535d53a9cd34816a532b80a1bfd7f3d" }; WriteWeixinLog("更新参数:\r\n" + param.ToJson2(), null); var result = RechargeApp.Instance.UpdatePayStatus(param); return(Content("", "text/xml")); }
/// <summary> /// 更新订单状态 /// </summary> /// <returns></returns> public JResult UpdatePayStatus(UpdatePayStatusRequest request) { //{"OrderSysNo":"de08b16c016d47f2b109eae46b11b545","Amount":0.1,"VoucherNo":"4200000083201803148591866779"} var response = new JResult() { Status = false }; try { var model = Using <IFnRecharge>().GetbyOrderNo(request.OrderSysNo); //如果订单已经支付,则直接返回true if (model != null && model.Status == PayStatus.已支付.GetHashCode()) { response.Status = false; response.Message = "订单已支付!"; return(response); } if (Using <IFnRecharge>().UpdatePayStatus(request) <= 0) { throw new Exception("更新支付状态失败!"); } if (model.Type.Equals(FnEnum.RechargeType.充值.GetHashCode())) { #region 充值 if (Using <ICrCustomer>().UpdatePayWalletAmount(new UpdatePayWalletAmountRequest() { CustomerSysNo = model.CustomerSysNo, UpgradeFundAmount = model.Amount, RechargeTotalAmount = model.Amount }) <= 0) { throw new Exception("更新支付状态失败!"); } #endregion } else if (model.Type.Equals(FnEnum.RechargeType.续费.GetHashCode())) { #region 续费 var customer = Using <ICrCustomer>().Get(model.CustomerSysNo); if (customer != null) { //如果过期时间为空则添加一年有效期 if (string.IsNullOrWhiteSpace(customer.ExpiresTime)) { customer.ExpiresTime = DateTime.Now.AddYears(1).ToString("yyyy-MM-dd"); if (CustomerApp.Instance.UpdateExpiresTime(customer) <= 0) { throw new Exception("更新过期时间失败!"); } } else { customer.ExpiresTime = Convert.ToDateTime(customer.ExpiresTime).AddYears(1).ToString("yyyy-MM-dd"); if (CustomerApp.Instance.UpdateExpiresTime(customer) <= 0) { throw new Exception("更新过期时间失败!"); } } } #endregion } else if (model.Type.Equals(FnEnum.RechargeType.升级.GetHashCode())) { #region 升级 if (Using <ICrCustomer>().UpdatePayWalletAmount(new UpdatePayWalletAmountRequest() { CustomerSysNo = model.CustomerSysNo, UpgradeFundAmount = model.Amount, RechargeTotalAmount = model.Amount }) <= 0) { throw new Exception("更新支付状态失败!"); } var customer = Using <ICrCustomer>().Get(model.CustomerSysNo); if (customer != null) { //获取等级字符串 var code = CodeApp.Instance.GetByType(CodeEnum.CodeTypeEnum.会员等级信息.GetHashCode()); if (code == null) { throw new Exception("获取会员等级信息失败!"); } var upgradeGrade = customer.Grade + Step.步长.GetHashCode(); var grades = JsonUtil.ToObject <List <Grade> >(code.Value); if (grades == null && !grades.Any()) { throw new Exception("请设置会员等级信息!"); } var grade = grades.FirstOrDefault(p => p.Type == upgradeGrade); if (grade == null) { throw new Exception("请选择会员等级!"); } var UpgradeResult = NewCustomerApp.Instance.NewUpgrade(new NewUpgradeRequest() { CustomerSysNo = customer.SysNo, //Amount = grade.Amount, //SelectGrade = customer.Grade + 10 }); if (!UpgradeResult.Status) { throw new Exception(UpgradeResult.Message); } } #endregion } response.Status = true; } catch (Exception ex) { response.Message = ex.Message; } return(response); }
/// <summary> /// 微信通知 /// </summary> /// <example>页面只能显示success/failed 不能有任何html标签</example> /// <returns>成功返回success 失败返回其它任意对象</returns> public ActionResult PayNotifyUrl() { //返回状态码SUCCESS/FAIL,SUCCESS 表示商户接收通知成功并校验成功 var returnCode = "FAIL"; //返回信息 非空为错误原因:1、签名失败 2、参数格式校验错误 var returnMsg = string.Empty; try { WriteWeixinLog("测试开始==========================\r\n", null); OrderNotifyImpl orderNotifyImpl = new OrderNotifyImpl() { apiKey = ConfigurationManager.AppSettings["apiKey"] }; WriteWeixinLog("PayNotifyUrl", null); var requestStream = System.Web.HttpContext.Current.Request.InputStream; byte[] requestByte = new byte[requestStream.Length]; requestStream.Read(requestByte, 0, (int)requestStream.Length); string xmlStr = Encoding.UTF8.GetString(requestByte); if (!string.IsNullOrWhiteSpace(xmlStr)) { XmlDocument doc = new XmlDocument(); doc.XmlResolver = null; doc.LoadXml(xmlStr); XmlNode xmlNode = doc.SelectSingleNode("xml"); XmlNodeList xmlNodeList = xmlNode.ChildNodes; foreach (XmlNode xnf in xmlNodeList) { orderNotifyImpl.Add(xnf.Name, xnf.InnerText); } #region 参数 var model = new OrderNotifyResponse() { //返回状态码 SUCCESS/FAIL 此字段是通信标识,非交易标识,交易是否成功需要查看 result_code来判断 return_code = orderNotifyImpl.Get("return_code"), //返回信息,如非空为错误原因 1:签名失败 2:参数格式校验错误 return_msg = orderNotifyImpl.Get("return_msg"), #region 以下字段在return_code为SUCCESS的时候有返回 //微信公众账号id appid = orderNotifyImpl.Get("appid"), //商户号 mch_id = orderNotifyImpl.Get("mch_id"), //终端设备号 device_info = orderNotifyImpl.Get("device_info"), //随机字符串 nonce_str = orderNotifyImpl.Get("nonce_str"), //签名 sign = orderNotifyImpl.Get("sign"), //业务结果 result_code = orderNotifyImpl.Get("result_code"), //错误代码 err_code = orderNotifyImpl.Get("err_code"), //错误代码描述 err_code_des = orderNotifyImpl.Get("err_code_des"), #endregion #region 以下字段在return_code和result_code都为SUCCESS的时候有返回 //用户标识 openid = orderNotifyImpl.Get("openid"), //是否关注公众账号 is_subscribe = orderNotifyImpl.Get("is_subscribe"), //交易类型 trade_type = orderNotifyImpl.Get("trade_type"), //付款银行 bank_type = orderNotifyImpl.Get("bank_type"), //总金额 total_fee = orderNotifyImpl.Get("total_fee"), //现金券金额 coupon_fee = orderNotifyImpl.Get("coupon_fee"), //货币种类 fee_type = orderNotifyImpl.Get("fee_type"), //微信支付订单号 transaction_id = orderNotifyImpl.Get("transaction_id"), //商户订单号 out_trade_no = orderNotifyImpl.Get("out_trade_no"), //商家数据包 attach = orderNotifyImpl.Get("attach"), //支付完成时间 time_end = orderNotifyImpl.Get("time_end") #endregion }; #endregion WriteWeixinLog("model:" + model.ToJson(), null); if (model.return_code != "SUCCESS" || model.result_code != "SUCCESS") { WriteWeixinLog(string.Format("通信或业务结果失败,return_code:{0},result_code:{1},err_code:{2},err_code_des:{3}", model.return_code, model.result_code, model.err_code, model.err_code_des), null); } if (orderNotifyImpl.IsTenpaySign()) { if (model.return_code == "SUCCESS" && model.result_code == "SUCCESS") { WriteWeixinLog("签名成功:\r\n", null); var param = new UpdatePayStatusRequest() { Amount = decimal.Parse(model.total_fee) / 100, VoucherNo = model.transaction_id, OrderSysNo = model.out_trade_no }; WriteWeixinLog("更新参数:\r\n" + param.ToJson2(), null); var result = RechargeApp.Instance.UpdatePayStatus(param); WriteWeixinLog(string.Format("更新订单:{0},支付状态{1}:\r\n", model.out_trade_no, result.ToJson()), null); if (!result.Status) { returnCode = "FAIL"; returnMsg = result.Message; } returnCode = "SUCCESS"; } } else { returnMsg = "签名失败"; WriteWeixinLog("签名失败:" + model.ToJson(), null); } } } catch (Exception ex) { WriteWeixinLog("exception:" + ex.Message, null); } string xml = string.Format(@"<xml> <return_code><![CDATA[{0}]]></return_code> <return_msg><![CDATA[{1}]]></return_msg> </xml>", returnCode, returnMsg); return(Content(xml, "text/xml")); }
/// <summary> /// 更新订单状态 /// </summary> /// <returns></returns> public int UpdatePayStatus(UpdatePayStatusRequest request) { return(DBContext.Sql(string.Format("update agent_fnrecharge set Status={0} where OrderNo='{1}'", PayStatus.已支付.GetHashCode(), request.OrderSysNo)).Execute()); }