- Yet another Golang daraja api client library (WIP).
- Lipa Na Mpesa Api / Express.
- C2B Regsiter URL & Simulate Payment Api
- B2B Api
- B2C Api
- Transaction Api
- Balance Query APi
- Reversal Api
- Parsers for the callback responses(stk callback parser done)
go get github.com/jakhax/go_daraja
- This section contain code examples for different apis in daraja
- Note this is not a daraja documentation, refer to references in every section below for detailed documentations.
package main;
import(
"fmt"
"log"
"github.com/jakhax/go_daraja/mpesa"
)
//MpesaConfig config
var MpesaConfig = &mpesa.Config{
//'sandbox' / 'production' , you can use built in consts like below
Environment:mpesa.SandBox,
ConsumerKey:"CONSUMER KEY",
ConsumerSecret:"CONSUMER SECRET",
}
mpesaService, err := mpesa.NewMpesa(config)
- To send an STK push to a customer phone
func sTKPushExample()(err error){
mpesaService, err := mpesa.NewMpesa(MpesaConfig)
if err != nil{
return
}
express := &mpesa.Express{
ShortCode:"174379",
Password:"LNM Password",
Amount:1,
PhoneNumber:"0712345678",
CallBackURL:"https://callback.com",
}
stkRes, err := mpesaService.STKPush(express)
if err != nil{
return
}
fmt.Println(stkRes.ResponseDescription)
return
}
func main(){
err := sTKPushExample()
if err != nil{
log.Fatal(err)
}
}
mpesaService.STKPush
returnsSTKPushRes
which is a pointer to struct containing the api response. For more information refer to the source.
- Get the transaction status of an lipa na mpesa stk push
func lnmTransactionStatusExample()(err error){
mpesaService, err := mpesa.NewMpesa(MpesaConfig)
if err != nil{
return
}
res, err := mpesaService.ExpressTransactionStatus("shortcode","LNM Password","checkout request id")
if err != nil{
return
}
fmt.Println(res.ResponseDescription)
return
}
- https://developer.safaricom.co.ke/lipa-na-m-pesa-online/apis/post/stkpush/v1/processrequest
- https://developer.safaricom.co.ke/docs#lipa-na-m-pesa-online-query-request
func c2BRegisterURLExample()(err error){
mpesaService, err := mpesa.NewMpesa(MpesaConfig)
if err != nil{
return
}
registerURL := &mpesa.RegisterURLs{
ValidationURL:"https://callback.com/validation",
ConfirmationURL:"https://callback.com/confirmation",
ShortCode:"123456",
// Cancelled/Completed , you can use built in const like below
ResponseType:mpesa.CompletedResponseType,
}
res,err := mpesaService.RegisterURLs(registerURL)
if err != nil{
return
}
fmt.Println(res.ResponseDescription)
return
}
- https://peternjeru.co.ke/safdaraja/ui/#register_url_tutorial
- https://developer.safaricom.co.ke/c2b/apis/post/registerurl
- https://developer.safaricom.co.ke/docs#c2b-api
func c2BSimulateExample()(err error){
mpesaService, err := mpesa.NewMpesa(MpesaConfig)
if err != nil{
return
}
c2bSimulate := &mpesa.C2BSimulate{
Amount:100,
ShortCode:"123456",
//phone number
Msisdn:"254712345678",
}
res,err := mpesaService.C2BSimulate(c2bSimulate)
if err != nil{
return
}
fmt.Println(res.ResponseDescription)
return
}
- https://developer.safaricom.co.ke/c2b/apis/post/simulate
- https://peternjeru.co.ke/safdaraja/ui/#c2b_tutorial
- https://developer.safaricom.co.ke/docs#c2b-api
func b2CExample()(err error){
mpesaService, err := mpesa.NewMpesa(MpesaConfig)
if err != nil{
return
}
b2c := &mpesa.B2C{
EncryptPassword: true,
ShortCode:"123456",
InitiatorUserName:"testapi115",
InitiatorPassword:"Safaricom007@",
PhoneNumber:"254712345678",
Amount:100,
ResultCallBackURL:"https://callback.com/results",
}
res,err := mpesaService.B2C(b2c)
if err != nil{
return
}
fmt.Println(res.ResponseDescription)
return
}
- https://developer.safaricom.co.ke/b2c/apis/post/paymentrequest
- https://peternjeru.co.ke/safdaraja/ui/#b2c_tutorial
- https://developer.safaricom.co.ke/docs#b2c-api
func balanceQueryExample()(err error){
mpesaService, err := mpesa.NewMpesa(MpesaConfig)
if err != nil{
return
}
balanceQuery := &mpesa.BalanceQuery{
ShortCode:"123456",
InitiatorUserName:"testapi115",
InitiatorPassword:"Safaricom007@",
ResultCallBackURL:"https://callback.com/results",
}
res,err := mpesaService.BalanceQuery(balanceQuery)
if err != nil{
return
}
fmt.Println(res.ResponseDescription)
return
}
- https://developer.safaricom.co.ke/account-balance/apis/post/query
- https://developer.safaricom.co.ke/docs#account-balance-api
func transactionStatusExample()(err error){
mpesaService, err := mpesa.NewMpesa(MpesaConfig)
if err != nil{
return
}
transactionStatus := &mpesa.TransactionStatus{
EncryptPassword: true,
PhoneNumber:"254712345678",
InitiatorUserName:"testapi115",
InitiatorPassword:"Safaricom007@",
ResultCallBackURL:"https://callback.com/",
TransactionID:"LKXXXX1234",
}
res,err := mpesaService.TransactionStatus(transactionStatus)
if err != nil{
return
}
fmt.Println(res.ResponseDescription)
return
}
- https://developer.safaricom.co.ke/transaction-status/apis/post/query
- https://developer.safaricom.co.ke/docs#transaction-status
func reversalExample()(err error){
mpesaService, err := mpesa.NewMpesa(MpesaConfig)
if err != nil{
return
}
reversal := &mpesa.Reversal{
PhoneNumber:"254712345678",
InitiatorUserName:"testapi115",
InitiatorPassword:"Safaricom007@",
ResultCallBackURL:"https://callback.com/",
TransactionID:"LKXXXX1234",
Amount:1.00,
}
res,err := mpesaService.Reverse(reversal)
if err != nil{
return
}
fmt.Println(res.ResponseDescription)
return
}
- https://developer.safaricom.co.ke/reversal/apis/post/request
- https://peternjeru.co.ke/safdaraja/ui/#reversal_tutorial
- https://developer.safaricom.co.ke/docs#reversal
- Highly welcomed, documenting, report bugs, fix bugs and new features, write the b2b api client.