f
This commit is contained in:
@@ -171,6 +171,16 @@ func (s *ApiApplicationServiceImpl) CallApi(ctx context.Context, cmd *commands.A
|
||||
go s.asyncProcessDeduction(context.Background(), apiCall, validationResult)
|
||||
return transactionId, "", ErrQueryEmpty
|
||||
}
|
||||
if errors.Is(err, ErrSuccessNoBill) {
|
||||
encryptedResponse, encErr := crypto.AesEncrypt([]byte(response), validationResult.GetSecretKey())
|
||||
if encErr != nil {
|
||||
s.logger.Error("加密响应失败", zap.Error(encErr))
|
||||
go s.asyncRecordFailure(context.Background(), apiCall, encErr)
|
||||
return "", "", ErrSystem
|
||||
}
|
||||
go s.asyncSaveApiCall(context.Background(), apiCall, validationResult, response, decimal.Zero)
|
||||
return transactionId, string(encryptedResponse), nil
|
||||
}
|
||||
if err != nil {
|
||||
// 异步记录失败状态
|
||||
go s.asyncRecordFailure(context.Background(), apiCall, err)
|
||||
@@ -405,6 +415,11 @@ func (s *ApiApplicationServiceImpl) callExternalApi(ctx context.Context, cmd *co
|
||||
callContext)
|
||||
|
||||
if err != nil {
|
||||
var successNoBill *processors.SuccessNoBillError
|
||||
if errors.As(err, &successNoBill) {
|
||||
return string(successNoBill.Response), ErrSuccessNoBill
|
||||
}
|
||||
|
||||
// 数据宝 10001/10006:查空但计费,由 CallApi 统一处理
|
||||
if errors.Is(err, shujubao.ErrQueryEmpty) {
|
||||
return "", ErrQueryEmptyBillable
|
||||
@@ -440,10 +455,13 @@ func (s *ApiApplicationServiceImpl) callExternalApi(ctx context.Context, cmd *co
|
||||
return string(response), nil
|
||||
}
|
||||
|
||||
// asyncSaveApiCall 异步保存API调用记录
|
||||
func (s *ApiApplicationServiceImpl) asyncSaveApiCall(ctx context.Context, apiCall *entities.ApiCall, validation *dto.ApiCallValidationResult, response string) {
|
||||
// 标记为成功
|
||||
apiCall.MarkSuccess(validation.GetAmount())
|
||||
// asyncSaveApiCall 异步保存API调用记录;可选 cost 覆盖扣费金额(如成功但不扣费场景传 decimal.Zero)
|
||||
func (s *ApiApplicationServiceImpl) asyncSaveApiCall(ctx context.Context, apiCall *entities.ApiCall, validation *dto.ApiCallValidationResult, response string, cost ...decimal.Decimal) {
|
||||
chargeAmount := validation.GetAmount()
|
||||
if len(cost) > 0 {
|
||||
chargeAmount = cost[0]
|
||||
}
|
||||
apiCall.MarkSuccess(chargeAmount)
|
||||
|
||||
// 检查TransactionID是否已存在,避免重复创建
|
||||
existingCall, err := s.apiCallRepository.FindByTransactionId(ctx, apiCall.TransactionId)
|
||||
|
||||
@@ -7,6 +7,8 @@ var (
|
||||
ErrQueryEmpty = errors.New("查询为空")
|
||||
// ErrQueryEmptyBillable 数据宝查空(10001/10006):对外返回查询为空,但仍正常扣费
|
||||
ErrQueryEmptyBillable = errors.New("查询为空")
|
||||
// ErrSuccessNoBill 对外返回正常业务响应,但不扣费
|
||||
ErrSuccessNoBill = errors.New("成功不扣费")
|
||||
ErrQueryFailed = errors.New("查询失败")
|
||||
ErrSystem = errors.New("接口异常")
|
||||
ErrDecryptFail = errors.New("解密失败")
|
||||
|
||||
Reference in New Issue
Block a user