Update signature generation
This commit is contained in:
parent
41012f0503
commit
ecc0bf3297
@ -3,12 +3,14 @@ package com.banesco.common.application.helper;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@ -36,6 +38,14 @@ public class SerializationHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public String encodeStringToBase64(String text) {
|
||||
if (StringUtil.isNullOrEmpty(text)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public <T> T decodeBase64(String base64String, Class<T> clazz) {
|
||||
try {
|
||||
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
|
||||
@ -79,7 +89,7 @@ public class SerializationHelper {
|
||||
|
||||
try {
|
||||
Map<String, Object> map = objectMapper.convertValue(
|
||||
element, new TypeReference<>() {}
|
||||
element, new TypeReference<>() {}
|
||||
);
|
||||
|
||||
if (excludedFields != null && !excludedFields.isEmpty()) {
|
||||
@ -93,4 +103,55 @@ public class SerializationHelper {
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
public <T> Map<String, Object> toTreeMap(T element) {
|
||||
return toTreeMap(element, null);
|
||||
}
|
||||
|
||||
public <T> Map<String, Object> toTreeMap(
|
||||
T element,
|
||||
List<String> excludedFields
|
||||
) {
|
||||
if (element == null) {
|
||||
return new TreeMap<>();
|
||||
}
|
||||
|
||||
try {
|
||||
Map<String, Object> tempMap = objectMapper.convertValue(
|
||||
element, new TypeReference<>() {}
|
||||
);
|
||||
|
||||
Map<String, Object> treeMap = new TreeMap<>(tempMap);
|
||||
|
||||
if (excludedFields != null && !excludedFields.isEmpty()) {
|
||||
Set<String> excludedSet = new HashSet<>(excludedFields);
|
||||
excludedSet.forEach(treeMap::remove);
|
||||
}
|
||||
|
||||
return treeMap;
|
||||
} catch (Exception e) {
|
||||
log.error("Error al convertir objeto a TreeMap: {}", e.getMessage());
|
||||
return new TreeMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
public String generateSignature(
|
||||
Object operation,
|
||||
String channelOrigin
|
||||
) {
|
||||
Map<String, Object> operationSortedMap = toTreeMap(operation);
|
||||
StringBuilder concatenatedValues = new StringBuilder();
|
||||
String channelBase64 = encodeStringToBase64(channelOrigin);
|
||||
|
||||
for (Object value : operationSortedMap.values()) {
|
||||
concatenatedValues.append(value.toString());
|
||||
}
|
||||
|
||||
String finalString = concatenatedValues + channelBase64;
|
||||
|
||||
log.info("1. Operation concatenando valores: {}", concatenatedValues);
|
||||
log.info("2. Channel Origin codificado: {}", channelBase64);
|
||||
|
||||
return encodeSha256(finalString);
|
||||
}
|
||||
}
|
||||
|
||||
@ -114,9 +114,8 @@ public class PaymentFileClient implements PaymentFileUseCase {
|
||||
private Map<String, Object> getPayload(PaymentFileRequest params) {
|
||||
Map<String, Object> paymentStatusRequest = serializationHelper.toMap(params);
|
||||
Map<String, Object> operation = serializationHelper.toMap(params.getOperation());
|
||||
String signature = serializationHelper.encodeSha256(
|
||||
serializationHelper.toJsonString(operation) +
|
||||
serializationHelper.encodeBase64(params.getChannelOrigin())
|
||||
String signature = serializationHelper.generateSignature(
|
||||
params.getOperation(), params.getChannelOrigin()
|
||||
);
|
||||
|
||||
paymentStatusRequest.put("signature", signature);
|
||||
@ -124,9 +123,9 @@ public class PaymentFileClient implements PaymentFileUseCase {
|
||||
|
||||
paymentStatusRequest.remove("fintechId");
|
||||
|
||||
log.info("1. Firma generada: {}", signature);
|
||||
log.info("2. Parametros de la solicitud: {}", paymentStatusRequest);
|
||||
log.info("3. Solicitud final: {}", paymentStatusRequest);
|
||||
log.info("3. Firma generada: {}", signature);
|
||||
log.info("4. Parametros de la solicitud: {}", paymentStatusRequest);
|
||||
log.info("5. Solicitud final: {}", paymentStatusRequest);
|
||||
|
||||
return paymentStatusRequest;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user