This is an unofficial Mpesa Daraja API wrapper. You can easily install and reuse this package in you Go projects. This allows you to focus on the business logic by leaving the Mpesa APIs heavylifting.
This documentation assumes that you already have Go installed on your device. If you have not installed Go, check out the documentation on how to install it on your computer.
go get github.com/vgichira/mpesa-wrapper
To start using the SDK, we first need to create a new instance of the service with the daraja app credentials and the environment. Environment is either SANDBOX or PRODUCTION. Default environment is SANDBOX.
mpesaService, err := mpesa.Init("CONSUMER_KEY_HERE", "CONSUMER_SECRET_HERE", "ENVIROMENT (SANDBOX / LIVE)")
When sending an STK Push request to a client phone number, one of the parameters required if the password. The password is generated by Base64 encoding the shortcode, LNM passkey and the timestamp (YYYYMMDDHHMMSS).
{
"BusinessShortCode": "",
"Password": "", // This password
"Timestamp": "",
"TransactionType": "",
"Amount": "",
"PartyA": "",
"PartyB": "",
"PhoneNumber": "",
"CallBackURL": ""
"AccountReference": "",
"TransactionDesc": "" ,
}
To generate the password, you need to call the GenerateLipaNaMpesaPassword method from the SDK and pass shortcode, passkey and the timestamp as parameters.
password := mpesaService.GenerateLipaNaMpesaPassword("SHORTCODE", "PASSKEY", "TIMESTAMP")
To send STK Push to client's phone number, you need to call the LipaNaMpesaOnline method and pass the payload as a parameter.
requestBody := &mpesa.LipaNaMpesaRequest {
BusinessShortCode: "",
Password: "",
Timestamp: "",
TransactionType: "",
Amount: "",
PartyA: "",
PartyB: "",
PhoneNumber: "",
CallBackURL: "",
AccountReference: "",
TransactionDesc: "" ,
}
response, err := mpesaService.LipaNaMpesaOnline(requestBody)
requestBody:= &mpesa.RegisterURL {
ValidationURL: "",
ConfirmationURL: "",
ResponseType: "", // Cancelled / Completed
ShortCode: "",
}
response, err := mpesaService.RegisterURL(requestBody)
requestBody := &mpesa.C2BTransaction {
CommandID: "", // CustomerPayBillOnline / CustomerBuyGoodsOnline
Amount: "",
MSISDN: "",
BillRefNumber: "",
ShortCode: "",
}
response, err:= mpesaService.SimulateC2B(requestBody)
securityCredential, err := mpesaService.GenerateSecurityCredential("PUBLIC_CERT_LOCATION", "INITIATOR_PASSWORD")
requestBody := &mpesa.B2C {
InitiatorName: "",
SecurityCredential: "", // Security credential generated above
CommandID: "", // Salary Payments / Business Payments / Promotion Payments
Amount: "",
PartyA: "",
PartyB: "",
Remarks: "",
QueueTimeoutURL: "",
ResultURL: "",
Occassion: "",
}
response, err := mpesaService.InitiateB2C(requestBody)
requestBody := &mpesa.Reversal {
Initiator: "",
SecurityCredential: "",
CommandID: "",
TransactionID: "",
Amount: "",
ReceiverParty: "",
ReceiverIdentifierType: "",
ResultURL: "",
QueueTimeoutURL: "",
Remarks: "",
Occasion: "",
}
response, err := mpesaService.ReverseTransaction(requestBody)
requestBody := &mpesa.TransactionStatus {
Initiator: "",
SecurityCredential: "",
CommandID: "",
TransactionID: "",
PartyA: "",
IdentifierType: "",
ResultURL: "",
QueueTimeoutURL: "",
Remarks: "",
Occasion: "",
}
response, err := mpesaService.CheckTransactionStatus(requestBody)