Skip to content

Reconciling Payment

Mauko Maunde edited this page Jan 8, 2021 · 1 revision

The M-PESA transaction requests are asynchronous, and as such the payment details are not instantaneous. To get the transaction data and update the payment, use the reconcile() method. A callback function may be supplied to process the data. The callback function can either be a defined funtion or a closure(anonymous). If ommited, the method will return a successful response by default.

C2B::reconcile();
C2B::reconcile(function ($response){
    $response                   = $response["Body"];
    $resultCode 			    = $response["stkCallback"]["ResultCode"];
    $resultDesc 			    = $response["stkCallback"]["ResultDesc"];
    $merchantRequestID 			= $response["stkCallback"]["MerchantRequestID"];

    if(isset($response["stkCallback"]["CallbackMetadata"])){
        $CallbackMetadata       = $response["stkCallback"]["CallbackMetadata"]["Item"];

        $amount                 = $CallbackMetadata[0]["Value"];
        $mpesaReceiptNumber     = $CallbackMetadata[1]["Value"];
        $balance                = $CallbackMetadata[2]["Value"];
        $transactionDate        = $CallbackMetadata[3]["Value"];
        $phone                  = $CallbackMetadata[4]["Value"];

        $payment->status        = "Paid";
        $payment->amount        = $amount;
        $payment->receipt       = $mpesaReceiptNumber;
    }

    return true;
});
Clone this wiki locally