Class MpesaCallbackParser

java.lang.Object
io.github.openpaydev.mpesa.utils.MpesaCallbackParser

public final class MpesaCallbackParser extends Object
A utility class for parsing the JSON callback sent by the M-Pesa API.

The M-Pesa API communicates the result of an STK Push transaction by sending an HTTP POST request to a callback URL that you provide. The body of this request contains a JSON object with the transaction details. This class provides a simple, static method to parse that JSON.

Example Usage in a Spring Boot Controller:


 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;

 {@literal @}RestController
 public class MpesaCallbackController {

     {@literal @}PostMapping("/mpesa-callback")
     public void handleMpesaCallback({@literal @}RequestBody String callbackJson) {
         try {
             StkCallback stkCallback = MpesaCallbackParser.parse(callbackJson);
             // Now you can inspect the callback object
             int resultCode = stkCallback.getBody().getStkCallback().getResultCode();
             if (resultCode == 0) {
                 System.out.println("Payment successful!");
                 // TODO: Update your database, notify the user, etc.
             } else {
                 System.out.println("Payment failed or was cancelled. Reason: " + stkCallback.getBody().getStkCallback().getResultDesc());
             }
         } catch (JsonProcessingException e) {
             System.err.println("Error parsing M-Pesa callback: " + e.getMessage());
             // Handle the error appropriately
         }
     }
 }
 
  • Method Details

    • parse

      public static StkCallback parse(String jsonCallbackData) throws com.fasterxml.jackson.core.JsonProcessingException
      Parses the JSON string from an M-Pesa callback into a structured StkCallback object.
      Parameters:
      jsonCallbackData - The raw JSON string received from the M-Pesa API in the callback request body.
      Returns:
      A deserialized StkCallback object containing the transaction results.
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - if the provided JSON string is malformed or cannot be parsed into the target object.