As far as I know, there is now way of getting this from Pesapal via thei APIs.
The closes you can get is thei "QueryPaymentDetails" method which only returns
- pesapal_transaction_tracking_id,
- payment_method,
- payment_status,
- pesapal_merchant_reference
How I got around this in the past:
Notice that Pesapal returns "pesapal_merchant_reference" which was generated by your system and fed into Pesapal initially.
What I always do is to store the transaction details and then use this variable to query my own details once I get the IPN from Pesapal.
Unless someone from Pesapal corrects me, I believe you will never (as of right now) get back the MPESA reference or the reason for failure, if any.
One time I was parsing the excel file that you can get from Pesapal's dashboard to get a bit more information (the confirmation code / MPESA reference) - but I no longer think this is a good idea!