Webhook Example using Java

#1

Hello. Today is my 3rd day while trying to make a webhook using java. My webhook is getting called however the data am getting is scrambled. According to the documentation here https://flutterwavedevelopers.readme.io/docs/events-webhooks, am expecting a json message but instead, am getting this

id=64883&txRef=HJDVXUSEMO&flwRef=N%2FA&orderRef=URF_1516263337305_8473935&paymentPlan=&createdAt=2018-01-18T08%3A15%3A37.000Z&amount=12.819223403930664&charged_amount=13.08&status=pending&IP=%3A%3Affff%3A127.0.0.1&currency=USD&customer%5Bid%5D=12569&customer%5Bphone%5D=&customer%5BfullName%5D=solodriver%20KESO4&customer%5Bcustomertoken%5D=&customer%5Bemail%5D=email%40solo.com&customer%5BcreatedAt%5D=2018-01-18T08%3A15%3A37.000Z&customer%5BupdatedAt%5D=2018-01-18T08%3A15%3A37.000Z&customer%5BdeletedAt%5D=&customer%5BAccountId%5D=957&entity%5Bcard6%5D=424242&entity%5Bcard_last4%5D=4242.

Am using this code inside a HttpServlet

private static JSONObject getBody(HttpServletRequest request) throws IOException {

String jb = IOUtils.toString(request.getReader());
AppConstants.logger("JB:" + jb);
try {
    return HTTP.toJSONObject(jb);
} catch (JSONException e) {
    // crash and burn
    throw new IOException("Error parsing JSON request string");
}

}
.
Please help me get this format

{
“id”: “8368”,
“txRef”: “rave-checkout-1499791631”,
“flwRef”: “N/A”,
“createdAt”: “2017-07-11T16:47:39.000Z”,
“amount”: “9000”,
“charged_amount”: “9152.5”,
“status”: “pending”,
“IP”: “154.120.106.151”,
“currency”: “NGN”,
“customer[id]”: “380”,
“customer[phone]”: “”,
“customer[fullName]”: “Anonymous customer”,
“customer[customertoken]”: “”,
“customer[email]”: "user@example.com",
“customer[createdAt]”: “2017-05-09T18:32:52.000Z”,
“customer[updatedAt]”: “2017-05-09T18:32:52.000Z”,
“customer[deletedAt]”: “customer[AccountId]”,
“entity[card6]”: “543889”,
“entity[card_last4]”: “0229”
}

0 Likes

#2

So sorry for your block, please confirm what is printed in your log on this line AppConstants.logger(“JB:” + jb); I expect that you receive a JSONString.

If yes, try converting the string to JSON by creating a new JSONObject(StringObject), you can then print the returned JSON out by calling its .toString() method and you should have your expected response.

See below:

private static JSONObject getBody(HttpServletRequest request) throws IOException {

String jb = IOUtils.toString(request.getReader());
AppConstants.logger("JB:" + jb);
try {
    return HTTP.toJSONObject(jb); ==>  return new JSONObject(jb);

} catch (JSONException e) {
    // crash and burn
    throw new IOException("Error parsing JSON request string");
}

}
Please let me me know if this resolves your issue.

0 Likes