public XResult <YeePayAgreePayBindCardResponse> BindCard(YeePayAgreePayBindCardRequest request) { if (request == null) { return(new XResult <YeePayAgreePayBindCardResponse>(null, ErrorCode.INVALID_ARGUMENT, new ArgumentNullException(nameof(request)))); } if (!request.IsValid) { return(new XResult <YeePayAgreePayBindCardResponse>(null, ErrorCode.INVALID_ARGUMENT, new ArgumentException(request.ErrorMessage))); } String service = $"{this.GetType().FullName}.{nameof(BindCard)}(...)"; var requestHash = $"{nameof(BindCard)}:{request.PayerId}.{request.BankCardNo}".GetHashCode(); if (_lockProvider.Exists(requestHash)) { return(new XResult <YeePayAgreePayBindCardResponse>(null, ErrorCode.SUBMIT_REPEAT)); } try { if (!_lockProvider.Lock(requestHash)) { return(new XResult <YeePayAgreePayBindCardResponse>(null, ErrorCode.SUBMIT_REPEAT)); } // 根据付款人Id、银行卡号和通道判断绑卡数据是否已存在 var existsBindInfo = _bankCardBindInfoRepository.Exists(x => x.PayerId == request.PayerId && x.BankCardNo == request.BankCardNo && x.PayChannelCode == GlobalConfig.X99BILL_PAYCHANNEL_CODE); if (existsBindInfo) { return(new XResult <YeePayAgreePayBindCardResponse>(null, ErrorCode.INFO_EXISTED, new ArgumentException("绑卡信息已存在"))); } // 从已保存的绑卡数据中查出缺失的字段数据 var existedBankCardInfo = _bankCardInfoRepository.QueryProvider.FirstOrDefault(x => x.BankCardNo == request.BankCardNo); if (existedBankCardInfo == null) { return(new XResult <YeePayAgreePayBindCardResponse>(null, ErrorCode.INFO_NOT_EXIST, new ArgumentException("该付款人的申请绑卡记录不存在"))); } else { request.Mobile = existedBankCardInfo.Mobile; } var tradeTime = DateTime.Now; var result = YeePayAgreePayUtil.Execute <RawYeePayBindCardRequest, RawYeePayBindCardResponse>("/rest/v1.0/paperorder/auth/confirm", new RawYeePayBindCardRequest() { merchantno = GlobalConfig.YeePay_AgreePay_MerchantNo, requestno = request.OutTradeNo, validatecode = request.SmsValidCode }); String callMethod = $"{nameof(YeePayAgreePayUtil)}.{nameof(YeePayAgreePayUtil.Execute)}(...)"; if (!result.Success) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, callMethod, "绑卡失败", result.FirstException, new Object[] { request, result.Value }); return(new XResult <YeePayAgreePayBindCardResponse>(null, ErrorCode.DEPENDENT_API_CALL_FAILED, result.FirstException)); } if (result.Value == null) { return(new XResult <YeePayAgreePayBindCardResponse>(null, ErrorCode.REMOTE_RETURN_NOTHING)); } var respResult = result.Value; if (respResult.status != "BIND_SUCCESS") { //如果绑卡失败要删除之前的申请绑卡时保存的银行卡信息 _bankCardInfoRepository.Remove(existedBankCardInfo); var removeExistedBankCardInfoResult = _bankCardInfoRepository.SaveChanges(); if (!removeExistedBankCardInfoResult.Success) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, $"{nameof(_bankCardInfoRepository)}.SaveChanges()", "删除绑卡失败的银行卡信息失败", removeExistedBankCardInfoResult.FirstException, existedBankCardInfo); } return(new XResult <YeePayAgreePayBindCardResponse>(null, ErrorCode.DEPENDENT_API_CALL_FAILED, new RemoteException(respResult.errormsg))); } // 绑卡成功后数据入库 var bindInfo = new AgreePayBankCardBindInfo() { Id = IDGenerator.GenerateID(), AppId = request.AppId, PayerId = request.PayerId, BankCardId = existedBankCardInfo.Id, OutTradeNo = request.OutTradeNo, BankCardNo = request.BankCardNo, PayChannelCode = GlobalConfig.YEEPAY_PAYCHANNEL_CODE, BindStatus = nameof(BankCardBindStatus.BOUND), ApplyTime = tradeTime }; _bankCardBindInfoRepository.Add(bindInfo); var saveResult = _bankCardBindInfoRepository.SaveChanges(); if (!saveResult.Success) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, $"{nameof(_bankCardBindInfoRepository)}.SaveChanges()", "保存绑卡信息失败", saveResult.FirstException, bindInfo); return(new XResult <YeePayAgreePayBindCardResponse>(null, ErrorCode.DB_UPDATE_FAILED, saveResult.FirstException)); } var resp = new YeePayAgreePayBindCardResponse() { PayerId = request.PayerId, OutTradeNo = respResult.requestno, BindTime = tradeTime.ToString("yyyy-MM-dd HH:mm:ss"), Status = CommonStatus.SUCCESS.ToString(), Msg = $"绑卡{CommonStatus.SUCCESS.GetDescription()}" }; return(new XResult <YeePayAgreePayBindCardResponse>(resp)); } finally { _lockProvider.UnLock(requestHash); } }
public XResult <CPIAgreePayBindCardResponse> BindCard(CPIAgreePayBindCardRequest request) { if (request == null) { return(new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.INVALID_ARGUMENT, new ArgumentNullException(nameof(request)))); } if (!request.IsValid) { return(new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.INVALID_ARGUMENT, new ArgumentException(request.ErrorMessage))); } String service = $"{this.GetType().FullName}.BindCard(...)"; var requestHash = $"bindcard:{request.PayerId}.{request.BankCardNo}".GetHashCode(); if (_lockProvider.Exists(requestHash)) { return(new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.SUBMIT_REPEAT)); } try { if (!_lockProvider.Lock(requestHash)) { return(new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.SUBMIT_REPEAT)); } // 根据付款人Id、银行卡号和通道判断绑卡数据是否已存在 var existsBindInfo = _bankCardBindInfoRepository.Exists(x => x.PayerId == request.PayerId && x.BankCardNo == request.BankCardNo && x.PayChannelCode == GlobalConfig.X99BILL_PAYCHANNEL_CODE); if (existsBindInfo) { return(new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.INFO_EXISTED, new ArgumentException("绑卡信息已存在"))); } // 从已保存的绑卡数据中查出缺失的字段数据 var existedBankCardInfo = _bankCardInfoRepository.QueryProvider.FirstOrDefault(x => x.BankCardNo == request.BankCardNo); if (existedBankCardInfo == null) { return(new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.INFO_NOT_EXIST, new ArgumentException("该付款人的申请绑卡记录不存在"))); } else { request.Mobile = existedBankCardInfo.Mobile; } var tradeTime = DateTime.Now; // 调用第三方绑卡接口进行绑卡 var bindRequest = new AgreementBindRequest() { Version = "1.0", IndAuthDynVerifyContent = new IndAuthDynVerifyRequestContent() { BindType = "0", CustomerId = request.PayerId, ExternalRefNumber = request.OutTradeNo, Pan = request.BankCardNo, PhoneNO = request.Mobile, Token = request.ApplyToken, ValidCode = request.SmsValidCode } }; String callMethod = $"{_api.GetType().FullName}.AgreementVerify(...)"; _logger.Trace(TraceType.BLL.ToString(), CallResultStatus.OK.ToString(), service, callMethod, LogPhase.BEGIN, $"开始调用{callMethod}", new Object[] { ApiConfig.Bill99_AgreePay_BindCard_RequestUrl, bindRequest }); var result = _api.AgreementVerify(ApiConfig.Bill99_AgreePay_BindCard_RequestUrl, bindRequest); _logger.Trace(TraceType.BLL.ToString(), (result.Success ? CallResultStatus.OK : CallResultStatus.ERROR).ToString(), service, callMethod, LogPhase.END, $"完成调用{callMethod}", result.Value); if (!result.Success) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, callMethod, "绑卡失败", result.FirstException, bindRequest); return(new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.DEPENDENT_API_CALL_FAILED, result.FirstException)); } if (result.Value == null) { return(new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.REMOTE_RETURN_NOTHING)); } if (result.Value.IndAuthDynVerifyContent == null) { return(result.Value.ErrorMsgContent != null ? new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.DEPENDENT_API_CALL_FAILED, new RemoteException(result.Value.ErrorMsgContent.ErrorMessage)) : new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.REMOTE_RETURN_NOTHING)); } var respContent = result.Value.IndAuthDynVerifyContent; if (respContent.ResponseCode != "00") { //如果绑卡失败要删除之前的申请绑卡时保存的银行卡信息 _bankCardInfoRepository.Remove(existedBankCardInfo); var removeExistedBankCardInfoResult = _bankCardInfoRepository.SaveChanges(); if (!removeExistedBankCardInfoResult.Success) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, $"{nameof(_bankCardInfoRepository)}.SaveChanges()", "删除绑卡失败的银行卡信息失败", removeExistedBankCardInfoResult.FirstException, existedBankCardInfo); } return(new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.DEPENDENT_API_CALL_FAILED, new RemoteException(respContent.ResponseTextMessage))); } // 绑卡成功后数据入库 var bindInfo = new AgreePayBankCardBindInfo() { Id = IDGenerator.GenerateID(), AppId = request.AppId, PayerId = request.PayerId, BankCardId = existedBankCardInfo.Id, OutTradeNo = request.OutTradeNo, BankCardNo = request.BankCardNo, PayToken = respContent.PayToken, PayChannelCode = GlobalConfig.X99BILL_PAYCHANNEL_CODE, BindStatus = nameof(BankCardBindStatus.BOUND), ApplyTime = DateTime.Now }; _bankCardBindInfoRepository.Add(bindInfo); var saveResult = _bankCardBindInfoRepository.SaveChanges(); if (!saveResult.Success) { _logger.Error(TraceType.BLL.ToString(), CallResultStatus.ERROR.ToString(), service, $"{nameof(_bankCardBindInfoRepository)}.SaveChanges()", "保存绑卡信息失败", saveResult.FirstException, bindInfo); return(new XResult <CPIAgreePayBindCardResponse>(null, ErrorCode.DB_UPDATE_FAILED, saveResult.FirstException)); } var resp = new CPIAgreePayBindCardResponse() { PayerId = respContent.CustomerId, PayToken = respContent.PayToken, BindTime = tradeTime.ToString("yyyy-MM-dd HH:mm:ss"), Status = CommonStatus.SUCCESS.ToString(), Msg = CommonStatus.SUCCESS.GetDescription() }; return(new XResult <CPIAgreePayBindCardResponse>(resp)); } finally { _lockProvider.UnLock(requestHash); } }