/// <summary> /// 根据商户信息获得门店支付权限,若只传商户ID,则返回全部门店的状态信息,若传门店ID则只返回指定门店的支付权限信息 /// </summary> /// <param name="cid">商户号</param> /// <param name="storeId">门店号(可选)</param> /// <returns></returns> public QctPayReturn GetMerchStorePayInfos(int cid, string storeId = "") { var merchObj = PaySvc.GetMerchByCID(cid); if (merchObj == null) { return(QctPayReturn.Fail("请先申请开通商户,若您已经申请,请等待审核")); } var query = TStoreRepost.GetQuery().Where(o => o.CID == cid); if (string.IsNullOrWhiteSpace(storeId)) { query = query.Where(o => o.State == (short)TraderStoreState.Enabled); } else { query = query.Where(o => o.StoreNum == storeId); } var queryStore = (from s in query select new MerchStorePayItem { StoreId = s.StoreNum, State = s.State == (short)TraderStoreState.Enabled ? DataItemState.Enabled : DataItemState.Disabled }).ToList(); var data = new MerchStorePayModel() { CID = cid, Items = queryStore }; return(QctPayReturn.Success(data: data)); }
/// <summary> /// 发送Post支付请求 /// </summary> /// <param name="url"></param> /// <param name="paramsStr"></param> /// <returns></returns> public static QctPayReturn SendPost(this string url, string paramsStr) { string reqUrl = url + "?" + paramsStr; PayLogServer.WriteInfo(string.Format("发送交易请求:{0}", reqUrl)); try { var httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.Method = "POST"; httpRequest.ContentType = "application/x-www-form-urlencoded"; httpRequest.Timeout = 45000; byte[] byteRequest = System.Text.Encoding.UTF8.GetBytes(paramsStr); httpRequest.ContentLength = byteRequest.Length; Stream requestStream = httpRequest.GetRequestStream(); requestStream.Write(byteRequest, 0, byteRequest.Length); requestStream.Close(); //获取服务端返回 var response = (HttpWebResponse)httpRequest.GetResponse(); //获取服务端返回数据 StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); var result = sr.ReadToEnd().Trim(); sr.Close(); result = HttpUtility.UrlDecode(result, Encoding.UTF8); return(QctPayReturn.Success(data: result)); } catch (Exception ex) { var logEng = new LogEngine(); logEng.WriteError(string.Format("发送交易请求异常:{0},请求地址:{1}", ex.Message, reqUrl), ex, LogModule.支付交易); return(QctPayReturn.Fail(code: PayConst.FAIL_CODE_20000, msg: "订单请求失败,服务器繁忙!")); } }
public override object Query(RefundQueryRequest reqModel) { try { var canObj = CanAccess(); if (!canObj.Successed) { return(canObj); } var sxfReq = new SxfRefundQueryRequest(reqModel, MerchStoreModel); //sxf签名并请求 var sxfResult = PayHelper.SendPost(MerchStoreModel.ApiUrl, PaySignHelper.ToDicAndSign(sxfReq, MerchModel.SecretKey3, "signature")); if (sxfResult.Successed) { //处理返回结果 var sxfResultObj = JsonConvert.DeserializeObject <SxfRefundQueryResponse>(sxfResult.Data.ToString()); var result = sxfResultObj.ToRefundQueryResponse(MerchStoreModel); //Qct签名 var rstRsp = PaySignHelper.ToDicAndSign(result, MerchModel.SecretKey, "sign"); return(rstRsp); } else { return(sxfResult); } } catch (Exception ex) { LogEngine.WriteError(string.Format("退款订单查询请求异常:{0},请求参数:{1}", ex.Message, reqModel.ToJson()), null, LogModule.支付交易); return(QctPayReturn.Fail()); } }
public object Refund(RefundApplyRequest reqModel) { var result = QctPayReturn.Fail(msg: "参数格式错误"); OrderBuilder <RefundApplyRequest, RefundApplyResponse> rfdOrder = new OrderBuilderForRefund(); result = rfdOrder.Build(reqModel); return(rfdOrder.Result(result)); }
public object ScanPay(PayMerchScanRequest reqModel) { //(对应融合支付:被扫支付) var result = QctPayReturn.Fail(msg: "参数格式错误"); OrderBuilder <PayMerchScanRequest, PayMerchScanResponse> merchOrder = new OrderBuilderForPayMerchScan(); result = merchOrder.Build(reqModel); return(merchOrder.Result(result)); }
public object Pay(PayBuyerScanDynaRequest reqModel) { //(对应融合支付:主扫支付动态) var result = QctPayReturn.Fail(msg: "参数格式错误"); OrderBuilder <PayBuyerScanDynaRequest, PayBuyerScanDynaResponse> buyerOrder = new OrderBuilderForBuyerScanDyna(); result = buyerOrder.Build(reqModel); return(buyerOrder.Result(result)); }
/// <summary> /// 验证参数格式 /// </summary> /// <returns></returns> public QctPayReturn VerifyParams() { var errorMsg = ReqModel.TryValidateObject(); if (errorMsg.IsNullOrEmpty()) { return(QctPayReturn.Success()); } else { return(QctPayReturn.Fail(code: PayConst.FAIL_CODE_40001, msg: errorMsg)); } }
/// <summary> /// 判断订单号是否重复 /// </summary> /// <returns></returns> public QctPayReturn CanCreateOrder() { var can = PaySvc.CanCreateOrder(ReqModel.Mch_Id, OutTradeNo); if (can) { return(QctPayReturn.Success()); } else { return(QctPayReturn.Fail(msg: "订单号已存在,不能重复提交")); } }
public object SubmitQROrder(PayBuyerScanStaticRequest reqModel) { var errMsg = PayTradeHelper.TryValidateObject(reqModel); if (string.IsNullOrWhiteSpace(errMsg)) { //取得商户当前系统时间 var orderDate = DateTime.Now; //商户订单号 String orderId = OrderHelper.GetMaxOutOrderNo(); //商户订单日期 var reqObj = new PayBuyerScanDynaRequest(); reqObj.Mch_Id = reqModel.Mch_Id; reqObj.Store_Id = reqModel.Store_Id; reqObj.Device_Id = ""; reqObj.Method = PayConst.QCTTRADE_PAY_QRORDER; reqObj.Charset = PayConst.DEF_CHARSET; reqObj.Sign_Type = PayConst.DEF_SIGNTYPE; reqObj.Version = PayConst.DEF_VERSION; reqObj.Out_Trade_No = orderId; reqObj.Create_Date = orderDate; reqObj.Total_Amount = reqModel.Total_Amount; reqObj.Pay_Notify_Url = ""; reqObj.Buyer_Mobile = ""; reqObj.Goods_Name = "购物消费"; reqObj.Goods_Desc = reqModel.Goods_Desc; reqObj.Sign = "nosignrequest"; OrderBuilder <PayBuyerScanDynaRequest, PayBuyerScanDynaResponse> buyerOrder = new OrderBuilderForBuyerScanDyna(); var result = buyerOrder.Build(reqObj); if (!result.Successed) { return(View("PayError")); } else { Response.Redirect(buyerOrder.RspModel.Pay_Token); return(View()); //return Json(buyerOrder.RspModel.Pay_Token); } } else { return(View("PayError", QctPayReturn.Fail(msg: errMsg))); } }
/// <summary> /// 支付订单分页查询 /// </summary> /// <param name="reqModel"></param> /// <returns></returns> public override object Query(PayBatchQueryRequest reqModel) { try { var canObj = CanAccess(); if (!canObj.Successed) { return(canObj); } var sxfReq = new SxfPayBatchQueryRequest(reqModel, MerchStoreModel); //sxf签名并请求 var sxfResult = PayHelper.SendPost(MerchStoreModel.ApiUrl, PaySignHelper.ToDicAndSign(sxfReq, MerchModel.SecretKey3, "signature")); if (sxfResult.Successed) { //处理返回结果 var sxfJObj = JObject.Parse(HttpUtility.UrlDecode(sxfResult.Data.ToString())); var sxfResultRsp = sxfJObj.ToObject <SxfPayBatchQueryResponse>(); if (sxfResultRsp.IsSuccess()) { var result = sxfResultRsp.ToPayBatchQueryRsp(MerchStoreModel); //Qct签名 var resultDic = PaySignHelper.ToDicAndSign(result, MerchModel.SecretKey, "sign"); return(resultDic); } else { LogEngine.WriteError(string.Format("支付订单分页查询请求错误:请求参数:{0},返回参数:{1}", sxfResultRsp.ToJson(), sxfResult.ToJson()), null, LogModule.支付交易); var rst = QctPayReturn.Fail(PayConst.FAIL_CODE_40004, sxfResultRsp.RspMsg); return(rst); } } else { return(sxfResult); } } catch (Exception ex) { LogEngine.WriteError(string.Format("支付订单分页查询请求异常:{0},请求参数:{1}", ex.Message, reqModel.ToJson()), null, LogModule.支付交易); var rst = QctPayReturn.Fail(); return(rst); } }
/// <summary> /// 检查商户通道是否可用 /// </summary> /// <param name="cid"></param> /// <param name="method"></param> /// <returns></returns> public QctPayReturn CheckMerchAccess(int cid, string method, decimal version) { var payLicense = GetPayLicense(cid, (short)TraderPayLicenseState.Audited); if (payLicense == null) { return(QctPayReturn.Fail(code: PayConst.FAIL_CODE_20001, msg: "请先申请支付许可")); } var merchBankAccount = GetBankAccount(cid, (short)TraderBalanceAccountState.Enabled); if (merchBankAccount == null) { return(QctPayReturn.Fail(code: PayConst.FAIL_CODE_20001, msg: "您的商户结算账户不可用,请先确保商户结算账户可用再提交支付订单")); } var merchChannel = GetTraderPaySecretKeyAndChannel(cid, (short)TraderPayCchannelState.Enabled); if (merchChannel == null) { return(QctPayReturn.Fail(code: PayConst.FAIL_CODE_20001, msg: "您的商户不可用,请先确认是否已经成功申请商户")); } var payApiObj = GetPayApi(merchChannel.ChannelNo, method, version); if (payApiObj == null) { return(QctPayReturn.Fail(code: PayConst.FAIL_CODE_40001, msg: "非法支付接口参数名")); } var merchObj = GetMerchByID(cid, payApiObj); if (merchObj == null) { return(QctPayReturn.Fail(code: PayConst.FAIL_CODE_20001, msg: "找不到对应商户密钥配置信息,请先申请开通商户")); } merchObj.SourceType = payLicense.SourceType; merchObj.ApiNo = payApiObj.ApiNo; merchObj.ApiUrl = payApiObj.ApiUrl; merchObj.Method = payApiObj.Method; merchObj.OptType = payApiObj.OptType; return(QctPayReturn.Success(data: merchObj)); }
/// <summary> /// 是否有访问接口权限 /// </summary> /// <returns></returns> public QctPayReturn CanAccess() { var result = VerifyParams(); if (!result.Successed) { return(result); } result = VerifyMerchAccess(); if (!result.Successed) { return(result); } var merchStoreModel = GetMerchStore(); if (merchStoreModel == null) { return(QctPayReturn.Fail("找不到商户门店信息")); } return(QctPayReturn.Success()); }
/// <summary> /// 发送请求 /// </summary> /// <returns></returns> public QctPayReturn SendPost <TSxfReqModel, TSxfRspModel>(string url, TSxfReqModel sxfReqModel) where TSxfReqModel : SxfBaseTradeRequest where TSxfRspModel : SxfBaseTradeResponse { //var resultStr = string.Empty; try { var postResult = PayHelper.SendPost(url, PaySignHelper.ToDicAndSign(sxfReqModel, MerchModel.SecretKey3, "signature")); if (postResult.Successed) { var resultObj = JsonConvert.DeserializeObject <TSxfRspModel>(postResult.Data.ToString()); if (resultObj.IsSuccess()) { return(QctPayReturn.Success(data: resultObj)); } else { //处理返回失败结果 LogEngine.WriteError(string.Format("发送交易请求成功但返回交易错误信息:请求参数:{0},返回参数:{1}]", sxfReqModel.ToJson(), postResult), null, LogModule.支付交易); if (string.IsNullOrWhiteSpace(resultObj.RspMsg)) { resultObj.RspMsg = "服务器请求失败"; } return(QctPayReturn.Fail(PayTradeHelper.TransCodeBySxf(resultObj.RspCod), resultObj.RspMsg)); } } else { return(postResult); } } catch (Exception ex) { return(ResultFail(msg: "订单请求失败", logMsg: string.Format("发送交易请求异常:服务器异常,请求参数:{0},异常信息:{1}]", sxfReqModel.ToJson(), ex.Message))); } }
/// <summary> /// 根据CID获取商户的支付类型列表 /// </summary> /// <param name="cid"></param> /// <returns></returns> public QctPayReturn GetMerchPayModes(int cid) { var payLicenseObj = PaySvc.GetPayLicense(cid, (short)TraderPayLicenseState.Audited); if (payLicenseObj == null) { return(QctPayReturn.Fail("请先申请支付许可")); } var merchObj = PaySvc.GetMerchByCID(cid); if (merchObj == null) { return(QctPayReturn.Fail("请先申请开通商户,若您已经申请,请等待审核")); } var query = (from p in PayApiRepost.GetQuery() join jtpsk in TPaySecretKeyRepost.GetQuery() on p.ChannelNo equals jtpsk.ChannelNo into itpsk from tpsk in itpsk.DefaultIfEmpty() where tpsk.CID == cid && p.State == (short)PayApiState.HasReleased && p.OptType == (short)PayOperateType.Receipt select new MerchPayModeItem() { Title = p.Title, ChannelNo = p.ChannelNo, PayMode = p.TradeMode, State = DataItemState.Enabled }); var objs = query.ToList(); var data = new MerchPayMode() { CID = cid, Items = objs }; return(QctPayReturn.Success(data: objs)); }
/// <summary> /// 验证签名 /// </summary> /// <param name="actionContext"></param> public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) { var dic = GetForm(System.Web.HttpContext.Current.Request.Form); try { //请求日志记录 PayLogServer.WriteInfo(string.Format("{0}:{1}", LogTitle, dic.ToJson())); var signSvc = new PaySignService(); if (!signSvc.VerifySign(dic, SignField)) { PayLogServer.WriteInfo(string.Format("{0}时签名失败:,{1}", LogTitle, dic.ToJson())); actionContext.Response = GetResponseMsg(QctPayReturn.Fail(msg: "签名失败").ToJson()); } } catch (Exception ex) { PayLogServer.WriteError(string.Format("{0}异常:{1}", LogTitle, dic.ToJson()), ex); actionContext.Response = GetResponseMsg(QctPayReturn.Fail(msg: "参数格式错误").ToJson()); } base.OnActionExecuting(actionContext); }
/// <summary> /// 返回错误信息,并记录日志 /// </summary> /// <param name="msg"></param> /// <param name="logMsg"></param> /// <returns></returns> public QctPayReturn ResultFail(string msg, string logMsg) { LogEngine.WriteError(logMsg, null, LogModule.支付交易); return(QctPayReturn.Fail(code: PayConst.FAIL_CODE_40004, msg: msg)); }
/// <summary> /// 创建生成订单 /// </summary> /// <param name="reqModel"></param> /// <returns></returns> public virtual QctPayReturn Build(TReqModel reqModel) { return(QctPayReturn.Fail()); }
/// <summary> /// 查询订单 /// </summary> /// <param name="reqModel"></param> /// <returns></returns> public virtual object Query(TReqModel reqModel) { return(QctPayReturn.Fail()); }