Skip to content

Golang-Tanzania/mpesa

Repository files navigation

Mpesa

Mpesa for Mpesa

made-with-Go GitHub go.mod Go version of a Go module GoDoc reference example

Golang client for the Mpesa Payment API. Make your MPESA payments Ready... To... Gooo! (pun intended). Made with love for gophers.

Features

  • Customer to Business (C2B) Single Payment
  • Business to Business (B2B)
  • Business to Customer (B2C)
  • Payment Reversal
  • Query Transaction status
  • Query Beneficiary Name
  • Query Direct Debit
  • Direct Debit Create
  • Direct Debit Payment

Pre-requisites

Installation

Simply install with the go get command:

go get github.com/Golang-Tanzania/mpesa

Then import it to your main package as:

package main

import (
	mpesa "github.com/Golang-Tanzania/mpesa
)

Usage

   // NewClient returns new Client struct
   client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
   // "your-api-key" obtained at https://openapiportal.m-pesa.com
   // sandbox is environment type can either be mpesa.Sandbox or mpesa.Production
   // 24 represent hours, its session lifetime before we request another session it can be found in the 
   // https://openapiportal.m-pesa.com/applications where you set for your application,



   // use custom htttp client

   c :=  http.Client{
	      Timeout: 50 * time.Second,
	},

   client.SetHttpClient(c)

Examples

Below are examples on how to make different transactions.

Customer To Business

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/Golang-Tanzania/mpesa"
)

func main() {

    client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24) 
	if err != nil {
		panic(err)
	}

	a := mpesa.C2BPaymentRequest{
		Amount:                   "100",
		CustomerMSISDN:           "000000000001",
		Country:                  "TZN",
		Currency:                 "TZS",
		ServiceProviderCode:      "000000",
		TransactionReference:     "T12344C",
		ThirdPartyConversationID: "asv02e5958774f7ba228d83d0d689761",
		PurchasedItemsDesc:       "Test",
	}
	res, err := client.C2BPayment(context.Background(), a)

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("res", res)
}

Business To Customer

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/Golang-Tanzania/mpesa"
)

func main() {

	client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24) 
	if err != nil {
		panic(err)
	}

	c := mpesa.B2CPaymentRequest{
		Amount:                   "100",
		CustomerMSISDN:           "000000000001",
		Country:                  "TZN",
		Currency:                 "TZS",
		ServiceProviderCode:      "000000",
		TransactionReference:     "T12344C",
		ThirdPartyConversationID: "asv02e5958774f7ba228d83d0d689761",
		PaymentItemsDesc:       "Test",
	}

	res, err := client.B2CPayment(context.Background(), c)

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("res", res)

}

Business To Business

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/Golang-Tanzania/mpesa"
)

func main() {

	client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24) 
	if err != nil {
		panic(err)
	}


	b := mpesa.B2BPaymentRequest{
		Amount:            "100",
		Country:           "TZN",
		Currency:          "TZS",
		PrimaryPartyCode: "000000",
		ReceiverPartyCode: "000001",
		ThirdPartyConversationID: "8a89835c71f15e99396",
		TransactionReference: "T12344C",
		PurchasedItemsDesc: "Test",
	}

	res, err := client.B2BPayment(context.Background(), b)

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("res", res)
}

Payment Reversal

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/Golang-Tanzania/mpesa"
)

func main() {

	client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24) 
	if err != nil {
		panic(err)
	}


	d :=  mpesa.ReversalRequest{
		TransactionID: "0000000000001",
		Country: "TZN",
		ServiceProviderCode: "000000",
		ReversalAmount: "100",
		ThirdPartyConversationID: "asv02e5958774f7ba228d83d0d689761",
	}

	res, err := client.Reversal(context.Background(), d)

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("res", res)
}

Query Transaction status

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/Golang-Tanzania/mpesa"
)

func main() {
	
	client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24) 
	if err != nil {
		panic(err)
	}


	e := mpesa.QueryTxStatusRequest{
		QueryReference:           "000000000000000000001",
		Country:                  "TZN",
		ServiceProviderCode:      "000000",
		ThirdPartyConversationID: "asv02e5958774f7ba228d83d0d689761",
	}

	res, err := client.QueryTxStatus(context.Background(), e)

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("res", res)
}

Query Beneficiary Name

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/Golang-Tanzania/mpesa"
)

func main() {
	
	client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24) 
	if err != nil {
		panic(err)
	}


	h := mpesa.QueryBenRequest{
		Country:                  "TZN",
		ServiceProviderCode:      "000000",
		ThirdPartyConversationID: "AAA6d1f939c1005v2de053v4912jbasdj1j2kk",
		CustomerMSISDN:           "000000000001",
		KycQueryType:             "Name",
	}

	res, err := client.QueryBeneficiaryName(context.Background(), h)

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("res", res)
}

Query Direct Debit

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/Golang-Tanzania/mpesa"
)

func main() {
	
	client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24) 
	if err != nil {
		panic(err)
	}


	i := mpesa.QueryDirectDBReq{
		QueryBalanceAmount:       true,
		BalanceAmount:            "100",
		Country:                  "TZN",
		CustomerMSISDN:           "255744553111",
		MsisdnToken:              "cvgwUBZ3lAO9ivwhWAFeng==",
		ServiceProviderCode:      "112244",
		ThirdPartyConversationID: "GPO3051656128",
		ThirdPartyReference:      "Test123",
		MandateID:                "15045",
		Currency:                 "TZS",
	}

	res, err := client.QueryDirectDebit(context.Background(), i)

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("res", res)
}

Direct Debit Create

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/Golang-Tanzania/mpesa"
)

func main() {
	
	client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24) 
	if err != nil {
		panic(err)
	}


	f := mpesa.DirectDBCreateReq{
		CustomerMSISDN:         "000000000001",
		Country:                "TZN",
		ServiceProviderCode:    "000000",
		ThirdPartyReference:    "3333",
		ThirdPartyConversationID: "asv02e5958774f7ba228d83d0d689761",
		AgreedTC:               "1",
		FirstPaymentDate:       "20160324",
		Frequency:              "06",
		StartRangeOfDays:       "01",
		EndRangeOfDays:         "22",
		ExpiryDate:             "20161126",
	}

	res, err := client.DirectDebitCreate(context.Background(), f)

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("res", res)
}

Direct Debit Payment

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/Golang-Tanzania/mpesa"
)

func main() {

	client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24) 
	if err != nil {
		panic(err)
	}


	g := mpesa.DebitDBPaymentReq{
		MsisdnToken:              "AbCd123=",
		CustomerMSISDN:           "000000000001",
		Country:                  "TZN",
		ServiceProviderCode:      "000000",
		ThirdPartyReference:      "5db410b459bd433ca8e5",
		ThirdPartyConversationID: "AAA6d1f939c1005v2de053v4912jbasdj1j2kk",
		Amount:                   "10",
		Currency:                 "TZS",
		MandateID:                "15045",
	}

	res, err := client.DirectDebitPayment(context.Background(), g)

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("res", res)
}

Cancel Direct Debit

package main

import (
	"context"
	"fmt"
	"net/http"
	"time"

	"github.com/Golang-Tanzania/mpesa"
)

func main() {

    client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24) 
	if err != nil {
		panic(err)
	}


	j := mpesa.CancelDirectDBReq{
		MsisdnToken:              "cvgwUBZ3lAO9ivwhWAFeng==",
		CustomerMSISDN:           "000000000001",
		Country:                  "TZN",
		ServiceProviderCode:      "000000",
		ThirdPartyReference:      "00000000000000000001",
		ThirdPartyConversationID: "AAA6d1f939c1005v2de053v4912jbasdj1j2kk",
		MandateID:                "15045",
	}

	res, err := client.CancelDirectDebit(context.Background(), j)

	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("res", res)
}

Authors

This package is authored and maintained by Hopertz and Mojo. A list of all other contributors can be found here.

Contributing

Contributions are welcome. Please open an issue or submit a pull request.

License

MIT License

Copyright (c) 2023 Golang Tanzania