public override bool CheckPayState(PayParameter parameter) { var config = PayFactory.GetConfig <Config>(this.GetType(), parameter.TradeID); var head = new Dictionary <string, object>(); head["service"] = "front.query"; var body = new Dictionary <string, object>(); body["out_trade_no"] = parameter.TradeID; var strResult = LianTuo_Helper.PostJsonReturnString(config, URL, head, body, parameter.RequestTimeout); var responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject <ResponseObject>(strResult); string serverSign = responseObj.head["sign"].ToString(); if (LianTuo_Helper.Sign(config.key, responseObj.head, responseObj.body) != serverSign) { throw new Exception("服务器返回信息签名检验失败"); } if ((string)responseObj.body["is_success"] == "S") { if ((string)responseObj.body["trade_status"] == "success") { double?receipt_amount = null; try { if (responseObj.body["receipt_amount"] != null) { receipt_amount = Convert.ToDouble(responseObj.body["receipt_amount"]); } } catch { } PayFactory.OnPaySuccessed(parameter.TradeID, receipt_amount, null, strResult); return(true); } else if ((string)responseObj.body["trade_status"] == "fail") { PayFactory.OnPayFailed(parameter.TradeID, (string)responseObj.body["trade_error_msg"], strResult); return(true); } else if ((string)responseObj.body["trade_status"] == "closed") { throw new Exception("订单已关闭"); } else if ((string)responseObj.body["trade_status"] == "cancel") { throw new Exception("订单已取消"); } } return(false); }
public override bool CheckPayState(PayParameter parameter) { var attrs = this.GetType().GetCustomAttributes(typeof(PayInterfaceAttribute), false); //获取当前接口类型 var myInterfaceType = ((PayInterfaceAttribute)attrs[0]).InterfaceType; var config = new Config(PayFactory.GetInterfaceXmlConfig(myInterfaceType, parameter.TradeID)); Dictionary <string, object> postDict = new Dictionary <string, object>(); postDict["merchantCode"] = config.merchantCode; postDict["operatorCode"] = config.operatorCode; postDict["businessCode"] = this.BusinessCode; var detail = new Dictionary <string, string> { { "merchantGenCode", parameter.TradeID }, { "operatorCode", config.operatorCode }, }; postDict["detail"] = new object[] { detail }; string json = Newtonsoft.Json.JsonConvert.SerializeObject(postDict); string queryStr = $"request={WebUtility.UrlEncode(json)}"; var result = Helper.PostQueryString(QueryUrl, queryStr, parameter.RequestTimeout); var resultDict = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(result); if (resultDict["resultCode"].ToString() == "0000") { var resultDetail = (Newtonsoft.Json.Linq.JArray)resultDict["detail"]; if (resultDetail[0]["statusId"].ToString() == "14") { var charge = Convert.ToDouble(resultDetail[0]["charge"].ToString()) / 100.0; var amount = Convert.ToDouble(resultDetail[0]["charge"].ToString()) / 100.0; PayFactory.OnPaySuccessed(parameter.TradeID, amount - charge, null, result); return(true); } else if (resultDetail[0]["statusId"].ToString() == "3") { PayFactory.OnPayFailed(parameter.TradeID, "订单已打回", result); return(true); } else if (resultDetail[0]["statusId"].ToString() == "12") { PayFactory.OnPayFailed(parameter.TradeID, "通道提交失败", result); return(true); } else if (resultDetail[0]["statusId"].ToString() == "15") { PayFactory.OnPayFailed(parameter.TradeID, "处理失败", result); return(true); } } return(false); }
public TaskStatus Handle(IHttpProxy httpProxy) { try { var requestJson = httpProxy.Form["requestJson"]; if (!string.IsNullOrEmpty(requestJson)) { var responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject <ResponseObject>(requestJson); var tradeId = responseObj.body["out_trade_no"].ToString(); var config = PayFactory.GetConfig <Config>(typeof(LianTuo_WeixinJsApi), tradeId); string serverSign = responseObj.head["sign"].ToString(); if (LianTuo_Helper.Sign(config.key, responseObj.head, responseObj.body) != serverSign) { throw new Exception("服务器返回信息签名检验失败"); } if ((string)responseObj.body["is_success"] == "S") { double?receipt_amount = null; try { if (responseObj.body["receipt_amount"] != null) { receipt_amount = Convert.ToDouble(responseObj.body["receipt_amount"]); } } catch { } PayFactory.OnPaySuccessed(tradeId, receipt_amount, null, requestJson); } else if ((string)responseObj.body["is_success"] == "F") { PayFactory.OnPayFailed(tradeId, (string)responseObj.body["message"], requestJson); } } httpProxy.ResponseWrite("success"); } catch (Exception ex) { using (Log log = new Log("Jack.Pay.LianTuo.WXJSApi.Result Error", false)) { log.Log(ex.ToString()); log.LogJson(httpProxy.Form); } } return(TaskStatus.Completed); }
public override bool CheckPayState(PayParameter parameter) { var config = new Config(PayFactory.GetInterfaceXmlConfig(PayInterfaceType.WeiXinScanQRCode, parameter.TradeID)); SortedDictionary <string, string> postDict = new SortedDictionary <string, string>(); postDict["appid"] = config.AppID; postDict["mch_id"] = config.MchID; postDict["out_trade_no"] = parameter.TradeID; postDict["nonce_str"] = Guid.NewGuid().ToString().Replace("-", "");//随机字符串 postDict["sign_type"] = "MD5"; postDict["sign"] = Helper.GetMd5Hash(postDict, config.Key); var xml = ToXml(postDict); var result = Helper.PostXml(QueryUrl, xml, parameter.RequestTimeout); XDocument xmldoc = XDocument.Parse(result); CheckSign(xmldoc, config); var return_code = xmldoc.Root.XPathSelectElement("return_code").Value; var return_msg = xmldoc.Root.XPathSelectElement("return_msg").Value; if (return_code == "SUCCESS" && return_msg == "OK") { if (xmldoc.Root.XPathSelectElement("err_code_des") != null) { throw new PayServerReportException(xmldoc.Root.XPathSelectElement("err_code_des").Value); } var trade_state = xmldoc.Root.XPathSelectElement("trade_state").Value; if (trade_state == "SUCCESS") { PayFactory.OnPaySuccessed(parameter.TradeID, null, null, result); return(true); } else { if (trade_state == "PAYERROR" || trade_state == "REVOKED") { var trade_state_desc = xmldoc.Root.XPathSelectElement("trade_state_desc").Value; PayFactory.OnPayFailed(parameter.TradeID, trade_state_desc, result); return(true); } } } return(false); }
public override bool CheckPayState(PayParameter parameter) { var attrs = this.GetType().GetCustomAttributes(typeof(PayInterfaceAttribute), false); //获取当前接口类型 var myInterfaceType = ((PayInterfaceAttribute)attrs[0]).InterfaceType; var config = new Config(PayFactory.GetInterfaceXmlConfig(myInterfaceType, parameter.TradeID)); SortedDictionary <string, string> dict = new SortedDictionary <string, string>(); dict["out_tradeno"] = parameter.TradeID; var result = Bboqi_Helper.PostJson(config, QueryUrl, dict, parameter.RequestTimeout); if (result["pay_status"] == "2") { PayFactory.OnPayFailed(parameter.TradeID, "退款中", Newtonsoft.Json.JsonConvert.SerializeObject(result)); return(true); } else if (result["pay_status"] == "3") { PayFactory.OnPayFailed(parameter.TradeID, "已退款", Newtonsoft.Json.JsonConvert.SerializeObject(result)); return(true); } else if (result["pay_status"] == "4") { PayFactory.OnPayFailed(parameter.TradeID, "退款失败", Newtonsoft.Json.JsonConvert.SerializeObject(result)); return(true); } else if (result["pay_status"] == "5") { PayFactory.OnPayFailed(parameter.TradeID, "已撤销", Newtonsoft.Json.JsonConvert.SerializeObject(result)); return(true); } else if (result["pay_status"] == "1") { PayFactory.OnPaySuccessed(parameter.TradeID, Convert.ToDouble(result["total_fee"]), null, Newtonsoft.Json.JsonConvert.SerializeObject(result)); return(true); } return(false); }
/// <summary> /// 检查订单状态 /// </summary> /// <param name="parameter"></param> /// <param name="config"></param> /// <returns>只要有结果,无论成功或者失败,返回true,不确定支付结果返回false</returns> bool checkPayStateByConfig(PayParameter parameter, WxPayConfig config) { try { WxPayData queryOrderInput = new WxPayData(); queryOrderInput.SetValue("out_trade_no", parameter.TradeID); WxPayData result = WxPayApi.OrderQuery(queryOrderInput, config); string xml = result.ToXml(); PayFactory.OnLog(parameter.TradeID, xml); if (result.GetValue("return_code").ToString() == "SUCCESS" && result.GetValue("result_code").ToString() == "SUCCESS") { //支付成功 if (result.GetValue("trade_state").ToString() == "SUCCESS") { //触发回调函数 PayFactory.OnPaySuccessed(parameter.TradeID, result.ToXml()); return(true); } //用户支付中,需要继续查询 else if (result.GetValue("trade_state").ToString() == "USERPAYING") { return(false); } else if (result.GetValue("trade_state").ToString() == "NOTPAY") { //触发回调函数 PayFactory.OnPayFailed(parameter.TradeID, "放弃支付", xml); return(true); } } string returnMsg = result.GetValue("err_code_des").ToSafeString(); if (string.IsNullOrEmpty(returnMsg)) { returnMsg = result.GetValue("return_msg").ToSafeString(); } //如果返回错误码为“此交易订单号不存在”,直接失败 if (result.GetValue("err_code").ToString() == "ORDERNOTEXIST") { //触发回调函数 PayFactory.OnPayFailed(parameter.TradeID, returnMsg, xml); return(true); } else if (result.GetValue("err_code").ToString() == "SYSTEMERROR") { //如果是系统错误,则后续继续 return(false); } else if (result.GetValue("return_code").ToString() == "FAIL" || result.GetValue("result_code").ToString() == "FAIL") { //FAIL //触发回调函数 PayFactory.OnPayFailed(parameter.TradeID, returnMsg, xml); return(true); } } catch { } return(false); }
public virtual string StartPay(PayParameter parameter) { try { WxPayConfig config = new WxPayAPI.WxPayConfig(PayFactory.GetInterfaceXmlConfig(PayInterfaceType.WeiXinBarcode, parameter.TradeID)); WxPayData data = new WxPayData(); data.SetValue("auth_code", parameter.AuthCode); //授权码 data.SetValue("body", parameter.TradeName == null ? parameter.Description : parameter.TradeName); //商品描述 data.SetValue("total_fee", Convert.ToInt32(parameter.Amount * 100)); //总金额,以分为单位 data.SetValue("out_trade_no", parameter.TradeID); //产生随机的商户订单号 WxPayData result = WxPayApi.Micropay(data, config, 20); //提交被扫支付,接收返回结果 string xml = result.ToXml(); PayFactory.OnLog(parameter.TradeID, xml); string returnMsg = result.IsSet("return_msg") ? result.GetValue("return_msg").ToSafeString() : result.GetValue("err_code_des").ToSafeString(); //如果提交被扫支付接口调用失败,则抛异常 if (!result.IsSet("return_code") || result.GetValue("return_code").ToString() == "FAIL") { //触发回调函数 PayFactory.OnPayFailed(parameter.TradeID, returnMsg, xml); return(null); } //签名验证 result.CheckSign(config); //刷卡支付直接成功 if (result.GetValue("return_code").ToString() == "SUCCESS" && result.GetValue("result_code").ToString() == "SUCCESS") { //触发回调函数 PayFactory.OnPaySuccessed(parameter.TradeID, result.ToXml()); return(null); } //1)业务结果明确失败 if (result.GetValue("err_code").ToString() != "USERPAYING" && result.GetValue("err_code").ToString() != "SYSTEMERROR") { //触发回调函数 PayFactory.OnPayFailed(parameter.TradeID, result.GetValue("err_code_des").ToSafeString(), xml); return(null); } //到这里,不能确定支付结果,循环30秒确定 int checkTimes = parameter.Timeout / 2; Thread.Sleep(1000); for (int i = 0; i < checkTimes; i++) { if (checkPayStateByConfig(parameter, config)) { break; } if (i + 1 == checkTimes) { break; } Thread.Sleep(2000); } } catch (Exception ex) { throw ex; } return(null); }
public override string BeginPay(PayParameter parameter) { var attrs = this.GetType().GetCustomAttributes(typeof(PayInterfaceAttribute), false); //获取当前接口类型 var myInterfaceType = ((PayInterfaceAttribute)attrs[0]).InterfaceType; var config = new Config(PayFactory.GetInterfaceXmlConfig(myInterfaceType, parameter.TradeID)); SortedDictionary <string, string> dict = new SortedDictionary <string, string>(); dict["cashdesk_id"] = config.CashId ?? ""; dict["cashdesk_name"] = config.CashDesc ?? ""; dict["orig_fee"] = parameter.Amount.ToString(); dict["favo_fee"] = "0"; dict["total_fee"] = parameter.Amount.ToString(); dict["out_trade_no"] = parameter.TradeID; var resultJson = Bboqi_Helper.PostJson(config, OrderUrl, dict, parameter.RequestTimeout); string trade_no = resultJson["trade_no"]; dict = new SortedDictionary <string, string>(); dict["trade_no"] = trade_no; dict["body"] = parameter.Description; dict["auth_code"] = parameter.AuthCode; dict["channel"] = "wx"; var strResult = Bboqi_Helper.PostJsonReturnString(config, PayUrl, dict, parameter.RequestTimeout); resultJson = (SortedDictionary <string, string>)Newtonsoft.Json.JsonConvert.DeserializeObject(strResult, typeof(SortedDictionary <string, string>)); if (resultJson["result_code"] == "FAIL" && (resultJson.ContainsKey("trade_status") == false || resultJson["trade_status"] != "2")) { throw new Exception(resultJson["return_msgs"]); } string serverSign = resultJson["sign"]; if (Bboqi_Helper.Sign(config, resultJson) != serverSign) { throw new Exception("服务器返回信息签名检验失败"); } if (resultJson["result_code"] == "SUCCESS") { if (resultJson["trade_status"] == "1") { PayFactory.OnPaySuccessed(parameter.TradeID, parameter.Amount, null, strResult); return(null); } else if (resultJson["trade_status"] == "0") { PayFactory.OnPayFailed(parameter.TradeID, resultJson["return_msgs"], strResult); return(null); } } if (resultJson.ContainsKey("trade_status") && resultJson["trade_status"] == "2") { new Thread(() => { CheckPayStateInLoop(parameter); }).Start(); } return(null); }
/// <summary> /// 直接付款,适合条码支付 /// </summary> /// <param name="parameter"></param> public virtual string StartPay(PayParameter parameter) { if (parameter.AuthCode.IsNullOrEmpty()) { throw new Exception("条码为空"); } if (parameter.TradeID.IsNullOrEmpty()) { throw new Exception("交易编号为空"); } try { Config config = new Config(PayFactory.GetInterfaceXmlConfig(PayInterfaceType.AlipayBarcode, parameter.TradeID)); //创建提交的内容 AlipayTradePayContentBuilder builder = BuildPayContent(config, parameter); var client = config.AppConfig.CreateAopClient(); AlipayTradePayRequest payRequest = new AlipayTradePayRequest(); payRequest.BizContent = builder.BuildJson(); AlipayTradePayResponse payResponse = client.Execute(payRequest); PayFactory.OnLog(parameter.TradeID, payResponse.Body); string[] errorCodes = new string[] { "20000", "20001", "40001", "40002", "40003", "40004", "40006" };//明确一定是错误的代码 if (errorCodes.Contains(payResponse.Code)) { PayFactory.OnPayFailed(parameter.TradeID, payResponse.SubMsg, payResponse.Body); } else if (payResponse.Code == ResultCode.SUCCESS) { PayFactory.OnPaySuccessed(parameter.TradeID, payResponse.Body); } else { //到这里,不能确定支付结果,循环30秒确定 int checkTimes = parameter.Timeout / 2; Thread.Sleep(1000); for (int i = 0; i < checkTimes; i++) { if (checkPayStateByConfig(parameter, config)) { break; } if (i + 1 == checkTimes) { break; } Thread.Sleep(2000); } } } catch (Exception ex) { } return(null); }