perbaikan decript

This commit is contained in:
2025-09-02 19:14:05 +07:00
parent bd231b5919
commit bb5f3876ec
5 changed files with 320 additions and 130 deletions

View File

@@ -10,6 +10,7 @@ import (
"time"
"api-service/internal/config"
"api-service/internal/models/vclaim/peserta"
"github.com/mashingan/smapping"
"github.com/rs/zerolog/log"
@@ -198,41 +199,53 @@ func (s *Service) processResponse(res *http.Response) (*ResponDTOVclaim, error)
return finalResp, nil
}
// Decrypt response
consID, secretKey, _, tstamp, _ := s.config.SetHeader()
respDecrypt, err := ResponseVclaim(respMentah.Response, consID+secretKey+tstamp)
if err != nil {
log.Error().
Err(err).
Str("meta_code", respMentah.MetaData.Code).
Msg("Failed to decrypt response")
return nil, fmt.Errorf("failed to decrypt response: %w", err)
}
log.Debug().
Str("encrypted_length", fmt.Sprintf("%d bytes", len(respMentah.Response))).
Str("decrypted_length", fmt.Sprintf("%d bytes", len(respDecrypt))).
Msg("Response decrypted successfully")
// Unmarshal decrypted response
if respDecrypt != "" {
if err := json.Unmarshal([]byte(respDecrypt), &finalResp.Response); err != nil {
// If JSON unmarshal fails, store as string
log.Warn().
// Try to unmarshal response as JSON first (in case it's not encrypted)
var tempResp interface{}
if json.Unmarshal([]byte(respMentah.Response), &tempResp) == nil {
log.Debug().Msg("Response is valid JSON, not encrypted")
finalResp.Response = tempResp
} else {
log.Debug().Msg("Response is not valid JSON, trying to decrypt")
// Decrypt response
consID, secretKey, _, tstamp, _ := s.config.SetHeader()
respDecrypt, err := ResponseVclaim(respMentah.Response, consID+secretKey+tstamp)
if err != nil {
log.Error().
Err(err).
Msg("Failed to unmarshal decrypted response, storing as string")
finalResp.Response = respDecrypt
} else {
log.Debug().Msg("Decrypted response unmarshaled successfully")
Str("meta_code", respMentah.MetaData.Code).
Msg("Failed to decrypt response")
return nil, fmt.Errorf("failed to decrypt response: %w", err)
}
// Use gjson to extract and log some metadata from the response if it's JSON
if jsonBytes, err := json.Marshal(finalResp.Response); err == nil {
jsonStr := string(jsonBytes)
// Extract some common fields using gjson
if metaCode := gjson.Get(jsonStr, "metaData.code"); metaCode.Exists() {
log.Info().
Str("response_meta_code", metaCode.String()).
Msg("Final response metadata")
log.Debug().
Str("encrypted_length", fmt.Sprintf("%d bytes", len(respMentah.Response))).
Str("decrypted_length", fmt.Sprintf("%d bytes", len(respDecrypt))).
Msg("Response decrypted successfully")
log.Debug().
Str("decrypted_data", respDecrypt).
Msg("Decrypted data")
// Unmarshal decrypted response
if respDecrypt != "" {
if err := json.Unmarshal([]byte(respDecrypt), &finalResp.Response); err != nil {
// If JSON unmarshal fails, store as string
log.Warn().
Err(err).
Msg("Failed to unmarshal decrypted response, storing as string")
finalResp.Response = respDecrypt
} else {
log.Debug().Msg("Decrypted response unmarshaled successfully")
// Use gjson to extract and log some metadata from the response if it's JSON
if jsonBytes, err := json.Marshal(finalResp.Response); err == nil {
jsonStr := string(jsonBytes)
// Extract some common fields using gjson
if metaCode := gjson.Get(jsonStr, "metaData.code"); metaCode.Exists() {
log.Info().
Str("response_meta_code", metaCode.String()).
Msg("Final response metadata")
}
}
}
}
@@ -410,6 +423,20 @@ func mapToResult(resp *ResponDTOVclaim, result interface{}) error {
return fmt.Errorf("failed to unmarshal to result: %w", err)
}
// Handle BPJS peserta response structure
if pesertaResp, ok := result.(*peserta.PesertaResponse); ok {
if resp.Response != nil {
if responseMap, ok := resp.Response.(map[string]interface{}); ok {
if pesertaMap, ok := responseMap["peserta"]; ok {
pesertaBytes, _ := json.Marshal(pesertaMap)
var pd peserta.PesertaData
json.Unmarshal(pesertaBytes, &pd)
pesertaResp.Data = &pd
}
}
}
}
return nil
}