@PostMapping(value = "/webhook")
public void postBody(
@RequestHeader("eformsign_signature") String signature,
@RequestBody String body) throws Exception {
System.out.println(" Webhook Header: " + signature);
System.out.println(" Body: " + body);
boolean valid = EformSignService.verifySignature(signature, body);
System.out.println(" verifySignature: " + valid);
if (valid) {
JSONParser jsonParse = new JSONParser();
JSONObject jsonObj = (JSONObject) jsonParse.parse(body);
String event_type = (String) jsonObj.get("event_type");
JSONObject doc = (JSONObject) jsonObj.get(event_type);
String document_status = (String) doc.get("document_status");
if(token == null) {
res = eformSignService.getAccessToken(member_id).block();
saveToken();
System.out.println(" getAccessToken: " + token);
} else if ( System.currentTimeMillis() > token.get("expiredAt").asLong()) {
System.out.println(" Token expired." );
res = eformSignService.getRefreshToken(token).block();
saveToken();
System.out.println(" getRefreshToken: " + res);
}
if(event_type.equals("ready_document_pdf") && document_status.equals("doc_complete")) {
String documentId = (String) doc.get("document_id");
byte[] bytes = eformSignService.downloadDocument(token, documentId, fileType).block();
Path path = Paths.get( downPath + documentId + ".zip");
Files.write(path, bytes);
System.out.println(" downloadDocument: " + path.getFileName() + " (" + Files.size(path) + " bytes)");
res = eformSignService.deleteDocument(token, documentId).block();
System.out.println(" deleteDocument: " + downPath+documentId+".zip" + res);
}
}
}