Skip to content

jakhax/go_daraja

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Daraja Api Client

Description

  • Yet another Golang daraja api client library (WIP).

Work Done

  • 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)

Installation

go get github.com/jakhax/go_daraja

APIs

  • 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.

Creating the Mpesa Service

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)

Express / LNM API

LNM STK Push

  • 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 returns STKPushRes which is a pointer to struct containing the api response. For more information refer to the source.
Resources

LNM Transaction Status

  • 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
}
Resources

C2B API

C2B Register URL

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
}
Resources

C2B Simulate Transaction

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
}
Resources

B2C API

B2C Transaction

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
}
References

Balance Query API

balance query

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
}
References

Transaction Status API

transaction status

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
}
References

Reversal API

Reversal

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
}
References

Contributions

  • Highly welcomed, documenting, report bugs, fix bugs and new features, write the b2b api client.

References

About

safaricom mpesa api(daraja) client for go

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages