Updated code.
This commit is contained in:
@@ -137,6 +137,7 @@ public class GepafinConstant {
|
||||
public static final String APPLICATION_IS_INCOMPLETE_MSG = "application.is.incomplete";
|
||||
public static final String AUTHORIZATION = "Authorization";
|
||||
public static final String CHECK_VATNUMBER_V2_NEW_URL = "https://imprese.openapi.it/advance";
|
||||
public static final String CHECK_VATNUMBER_V2_NEW_URL_IT_ADVANCE = "https://company.openapi.com/IT-advanced";
|
||||
public static final String VALIDATION_FIELD_CUSTOM = "validation.field.custom";
|
||||
public static final String VALIDATION_CODICE_FISCALE = "validation.codice.fiscale";
|
||||
public static final String VALIDATION_CAP = "validation.cap";
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@@ -45,7 +46,97 @@ public class VatCheckDao {
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
// public VatCheckResponseBean checkVatNumberApi(String vatNumber) {
|
||||
// VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
|
||||
// vatCheckResponseBean.setValid(false);
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
// if (Boolean.TRUE.equals(Boolean.parseBoolean(isVatCheckGloballyDisabled))) {
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
// return vatCheckResponseBean;
|
||||
// }
|
||||
// Map<String, Object> responseBody = new HashMap<>();
|
||||
// try {
|
||||
// HttpHeaders headers = new HttpHeaders();
|
||||
// headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
// headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
// headers.set(GepafinConstant.AUTHORIZATION, "Bearer " + vatCheckNewToken);
|
||||
// headers.add(org.apache.http.HttpHeaders.USER_AGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0");
|
||||
//
|
||||
// URI baseUrl = URI.create(GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL);
|
||||
// ResponseEntity<Map<String, Object>> response = vatCheckService.checkVatNumber(baseUrl, vatNumber, headers);
|
||||
//
|
||||
//
|
||||
// if (response.getStatusCode() == HttpStatus.OK && response.hasBody()) {
|
||||
// log.info("Successfully checked vat number");
|
||||
// Map<String, Object> responseMap = response.getBody();
|
||||
// processValidResponse(responseMap, vatCheckResponseBean);
|
||||
// }
|
||||
// } catch (FeignException ex) {
|
||||
// if (ex.status() == 406) {
|
||||
// try {
|
||||
// Map<String, Object> errorResponse = Utils.parseErrorResponse(ex.contentUTF8());
|
||||
// processValidResponse(errorResponse, vatCheckResponseBean);
|
||||
// } catch (Exception parseEx) {
|
||||
// log.error("Failed to parse 406 error response: {0}", parseEx);
|
||||
// }
|
||||
// } else {
|
||||
// log.error("Exception occurred while checking vat number: {0}", ex);
|
||||
// Utils.callException(ex.status(), ex);
|
||||
// }
|
||||
// }
|
||||
// return vatCheckResponseBean;
|
||||
// }
|
||||
// public static void processValidResponse(Map<String, Object> responseMap, VatCheckResponseBean vatCheckResponseBean) {
|
||||
// if (responseMap != null && responseMap.containsKey("data")) {
|
||||
// Map<String, Object> responseBody = (Map<String, Object>) responseMap.get("data");
|
||||
//
|
||||
// if (responseBody != null) {
|
||||
// responseBody.remove("timestamp_creation");
|
||||
// responseBody.remove("timestamp_last_update");
|
||||
// responseBody.remove("data_iscrizione");
|
||||
// responseBody.remove("id");
|
||||
//
|
||||
// Map<String, Object> data = new LinkedHashMap<>();
|
||||
// data.put("data", responseBody);
|
||||
//
|
||||
// vatCheckResponseBean.setValid(true);
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.VALID_VATNUMBER_MSG));
|
||||
// vatCheckResponseBean.setVatCheckResponse(data);
|
||||
// } else {
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
// }
|
||||
// } else {
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// public VatCheckResponseBean checkVatNumber(String vatNumber) {
|
||||
// try {
|
||||
// return checkVatNumberApi(vatNumber);
|
||||
// } catch (Exception e) {
|
||||
// log.error("Error in checkVatNumber: {}", e.getMessage());
|
||||
// VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
|
||||
// vatCheckResponseBean.setValid(false);
|
||||
// vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
// return vatCheckResponseBean;
|
||||
// }
|
||||
// }
|
||||
|
||||
public VatCheckResponseBean checkVatNumber(String vatNumber) {
|
||||
|
||||
try {
|
||||
return checkVatNumberApi(vatNumber);
|
||||
} catch (Exception e) {
|
||||
log.error("Error in checkVatNumber: {}", e.getMessage());
|
||||
VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
|
||||
vatCheckResponseBean.setValid(false);
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
return vatCheckResponseBean;
|
||||
}
|
||||
}
|
||||
|
||||
public VatCheckResponseBean checkVatNumberApi(String vatNumber) {
|
||||
|
||||
VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
|
||||
vatCheckResponseBean.setValid(false);
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
@@ -53,46 +144,48 @@ public class VatCheckDao {
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
return vatCheckResponseBean;
|
||||
}
|
||||
Map<String, Object> responseBody = new HashMap<>();
|
||||
try {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set(GepafinConstant.AUTHORIZATION, "Bearer " + vatCheckNewToken);
|
||||
headers.add(org.apache.http.HttpHeaders.USER_AGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0");
|
||||
|
||||
URI baseUrl = URI.create(GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL);
|
||||
URI baseUrl = URI.create(GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL_IT_ADVANCE);
|
||||
ResponseEntity<Map<String, Object>> response = vatCheckService.checkVatNumber(baseUrl, vatNumber, headers);
|
||||
|
||||
|
||||
if (response.getStatusCode() == HttpStatus.OK && response.hasBody()) {
|
||||
log.info("Successfully checked vat number");
|
||||
Map<String, Object> responseMap = response.getBody();
|
||||
processValidResponse(responseMap, vatCheckResponseBean);
|
||||
}
|
||||
} catch (FeignException ex) {
|
||||
if (ex.status() == 406) {
|
||||
try {
|
||||
Map<String, Object> errorResponse = Utils.parseErrorResponse(ex.contentUTF8());
|
||||
processValidResponse(errorResponse, vatCheckResponseBean);
|
||||
} catch (Exception parseEx) {
|
||||
log.error("Failed to parse 406 error response: {0}", parseEx);
|
||||
}
|
||||
} else {
|
||||
log.error("Exception occurred while checking vat number: {0}", ex);
|
||||
Utils.callException(ex.status(), ex);
|
||||
if (ex.status() == 406) {
|
||||
try {
|
||||
Map<String, Object> errorResponse = Utils.parseErrorResponse(ex.contentUTF8());
|
||||
processValidResponse(errorResponse, vatCheckResponseBean);
|
||||
} catch (Exception parseEx) {
|
||||
log.error("Failed to parse 406 error response: {0}", parseEx);
|
||||
}
|
||||
} else {
|
||||
log.error("Exception occurred while checking vat number: {0}", ex);
|
||||
Utils.callException(ex.status(), ex);
|
||||
}
|
||||
}
|
||||
return vatCheckResponseBean;
|
||||
}
|
||||
public static void processValidResponse(Map<String, Object> responseMap, VatCheckResponseBean vatCheckResponseBean) {
|
||||
if (responseMap != null && responseMap.containsKey("data")) {
|
||||
Map<String, Object> responseBody = (Map<String, Object>) responseMap.get("data");
|
||||
|
||||
if (responseBody != null) {
|
||||
responseBody.remove("timestamp_creation");
|
||||
responseBody.remove("timestamp_last_update");
|
||||
responseBody.remove("data_iscrizione");
|
||||
if (responseMap != null && responseMap.containsKey("data")) {
|
||||
Object dataObject = responseMap.get("data");
|
||||
|
||||
if (dataObject instanceof List && !((List<?>) dataObject).isEmpty()) {
|
||||
dataObject = ((List<?>) dataObject).get(0);
|
||||
}
|
||||
|
||||
if (dataObject instanceof Map) {
|
||||
Map<String, Object> responseBody = (Map<String, Object>) dataObject;
|
||||
|
||||
responseBody.remove("creationTimestamp");
|
||||
responseBody.remove("lastUpdateTimestamp");
|
||||
responseBody.remove("id");
|
||||
|
||||
Map<String, Object> data = new LinkedHashMap<>();
|
||||
@@ -108,17 +201,4 @@ public class VatCheckDao {
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
}
|
||||
}
|
||||
|
||||
public VatCheckResponseBean checkVatNumber(String vatNumber) {
|
||||
try {
|
||||
return checkVatNumberApi(vatNumber);
|
||||
} catch (Exception e) {
|
||||
log.error("Error in checkVatNumber: {}", e.getMessage());
|
||||
VatCheckResponseBean vatCheckResponseBean = new VatCheckResponseBean();
|
||||
vatCheckResponseBean.setValid(false);
|
||||
vatCheckResponseBean.setMessage(Translator.toLocale(GepafinConstant.INVALID_VATNUMBER));
|
||||
return vatCheckResponseBean;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
@FeignClient(value = "vat-check-service", url = GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL)
|
||||
@FeignClient(value = "vat-check-service", url = GepafinConstant.CHECK_VATNUMBER_V2_NEW_URL_IT_ADVANCE)
|
||||
public interface VatCheckService {
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user