Mpesa Golang SDK facilitates in integrating M-pesa APIS into your go project. The following APIs are currently supported:
API | Description |
---|---|
Authorization | Generates an access token for authenticating APIs |
Lipa Na M-Pesa Online API | Initiates online payment on behalf of a customer. |
Business To Customer (B2C) | Transact between an M-Pesa short code to a phone number registered on M-Pesa. |
M-Pesa Express Query | Check the status of a Lipa Na M-Pesa Online Payment. |
To use the APIs, follow these steps:
- Register or login to your account on Daraja
- Create a new or view existing apps here
- Copy the app credentials. To prevent exposing you API keys, you can store them on configuration file such as
.env
orconfig.yml
.
go get github.com/jwambugu/mpesa-golang-sdk
The SDK supports the following environments:
mpesa.Sandbox
for test environment.mpesa.Production
for production environment once you go live.
package main
import (
"context"
"github.com/jwambugu/mpesa-golang-sdk"
"log"
"net/http"
"time"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
mpesaApp := mpesa.NewApp(http.DefaultClient, "CONSUMER_KEY_GOES_HERE", "CONSUMER_SECRET_GOES_HERE", mpesa.Sandbox)
stkPushRes, err := mpesaApp.STKPush(ctx, "PASSKEY_GOES_HERE", mpesa.STKPushRequest{
BusinessShortCode: 174379,
TransactionType: "CustomerPayBillOnline",
Amount: 10,
PartyA: 254708374149,
PartyB: 174379,
PhoneNumber: 254708374149,
CallBackURL: "https://example.com",
AccountReference: "Test",
TransactionDesc: "Test Request",
})
if err != nil {
log.Fatalln(err)
}
log.Printf("%+v", stkPushRes)
b2cRes, err := mpesaApp.B2C(ctx, "INITIATOR_PASSWORD_GOES_HERE", mpesa.B2CRequest{
InitiatorName: "TestG2Init",
CommandID: "BusinessPayment",
Amount: 10,
PartyA: 600123,
PartyB: 254728762287,
Remarks: "This is a remark",
QueueTimeOutURL: "https://example.com",
ResultURL: "https://example.com",
Occasion: "Test Occasion",
})
if err != nil {
log.Fatalln(err)
}
log.Printf("%+v", b2cRes)
stkQueryRes, err := mpesaApp.STKQuery(ctx, "PASSKEY_GOES_HERE", mpesa.STKQueryRequest{
BusinessShortCode: 174379,
CheckoutRequestID: "ws_CO_03082022131319635708374149", // STK PUSH CheckoutRequestID,
})
if err != nil {
log.Fatalln(err)
}
log.Printf("%+v", stkQueryRes)
}
The SDK adds a helper functions to decode callbacks. These are:
mpesa.UnmarshalSTKPushCallback(v)
mpesa.UnmarshalB2CCallback(v)
The following types are supported, any other type will be decoded using json.Unmarshal
Supported Types |
---|
string |
*http.Request |
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
callback, err := mpesa.UnmarshalB2CCallback(r)
if err != nil {
log.Fatalln(err)
}
log.Printf("%+v", callback)
})
callback, err := mpesa.UnmarshalSTKPushCallback(`
{
"Body": {
"stkCallback": {
"MerchantRequestID": "29115-34620561-1",
"CheckoutRequestID": "ws_CO_191220191020363925",
"ResultCode": 1032,
"ResultDesc": "Request cancelled by user."
}
}
}`)
if err != nil {
log.Fatalln(err)
}
log.Printf("%+v", callback)