mpesa

package module
v0.0.0-...-b756218 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2020 License: MIT Imports: 11 Imported by: 9

README

MPESA Golang API Wrapper CircleCI Maintainability Test Coverage

The wrapper provides convenient access to the Safaricom MPESA Daraja API for applications written in server-side Golang. 🚀

Installing

You can install the package by running:

go get github.com/AndroidStudyOpenSource/mpesa-api-go

Usage

The package needs to be configured with your appKey and appSecret which can be obtained from Safaricom.

const (
	appKey = "YOUR_APP_KEY"		    
	appSecret = "YOUR_APP_SECRET"	   
)

The following examples with show you how to make requests to the various api's available.

MPESAExpress (Formerly STKPush)

This api allows you to do Lipa Na M-Pesa payment using STK Push. This is a simple example:

package main

import (
	"log"
	"github.com/AndroidStudyOpenSource/mpesa-api-go"
)

const (
	appKey    = ""
	appSecret = ""
)

func main() {

	svc, err := mpesa.New(appKey, appSecret, mpesa.SANDBOX)
	if err != nil {
		panic(err)
	}

	res, err := svc.Simulation(mpesa.Express{
		BusinessShortCode: "",
		Password:          "",
		Timestamp:         "",
		TransactionType:   "",
		Amount:            0,
		PartyA:            "",
		PartyB:            "",
		PhoneNumber:       "",
		CallBackURL:       "",
		AccountReference:  "",
		TransactionDesc:   "",
	})

	if err != nil {
		log.Println(err)
	}
	log.Println(res)

}

C2B

This api allows you to register C2B Callback URLs to Safaricom, and also Simulate a C2B Transaction in Sandbox

This is a simple demo to show how to register C2B Callback URL:

package main

import (
	"log"
	"github.com/AndroidStudyOpenSource/mpesa-api-go"
)

const (
	appKey    = ""
	appSecret = ""
)

func main() {

	svc, err := mpesa.New(appKey, appSecret, mpesa.SANDBOX)
	if err != nil {
		panic(err)
	}

	res, err := svc.C2BRegisterURL(mpesa.C2BRegisterURL{
		ShortCode:       "",
		ResponseType:    "",
		ConfirmationURL: "",
		ValidationURL:   "",
	})

	if err != nil {
		log.Println(err)
	}
	log.Println(res)

}

To simulate a C2B Request, use this simple example:

package main

import (
	"log"
	"github.com/AndroidStudyOpenSource/mpesa-api-go"
)

const (
	appKey    = ""
	appSecret = ""
)

func main() {

	svc, err := mpesa.New(appKey, appSecret, mpesa.SANDBOX)
	if err != nil {
		panic(err)
	}

	res, err := svc.C2BSimulation(mpesa.C2B{
		ShortCode:     "",
		CommandID:     "",
		Amount:        "",
		Msisdn:        "",
		BillRefNumber: "",
	})

	if err != nil {
		log.Println(err)
	}
	log.Println(res)

}
B2C

This api allows you to do M-Pesa Transaction from company to client.

package main

import (
	"log"
	"github.com/AndroidStudyOpenSource/mpesa-api-go"
)

const (
	appKey    = ""
	appSecret = ""
)

func main() {

    var initiatorPassword = ""
	svc, err := mpesa.New(appKey, appSecret, mpesa.SANDBOX)
	if err != nil {
		panic(err)
	}
    
    securityCredential, err := svc.GetSecurityCredential(initiatorPassword)
    if err != nil {
        panic(err)
    }

	res, err := svc.B2CRequest(mpesa.B2C{
		InitiatorName:      "",
		SecurityCredential: securityCredential,
		CommandID:          "",
		Amount:             0,
		PartyA:             "",
		PartyB:             "",
		Remarks:            "",
		QueueTimeOutURL:    "",
		ResultURL:          "",
		Occassion:          "",
	})

	if err != nil {
		log.Println(err)
	}
	log.Println(res)

}
B2B

This api allows you to do M-Pesa Transaction from one company to another.

package main

import (
	"log"
	"github.com/AndroidStudyOpenSource/mpesa-api-go"
)

const (
	appKey    = ""
	appSecret = ""
)

func main() {

	svc, err := mpesa.New(appKey, appSecret, mpesa.SANDBOX)
	if err != nil {
		panic(err)
	}

	res, err := svc.B2BRequest(mpesa.B2B{
		Initiator:              "",
		SecurityCredential:     "",
		CommandID:              "",
		SenderIdentifierType:   "",
		RecieverIdentifierType: "",
		Remarks:                "",
		Amount:                 0,
		PartyA:                 "",
		PartyB:                 "",
		AccountReference:       "",
		QueueTimeOutURL:        "",
		ResultURL:              "",
	})

	if err != nil {
		log.Println(err)
	}
	log.Println(res)

}

Account Balance

This api allows you to do balance inquiry.

package main

import (
	"log"
	"github.com/AndroidStudyOpenSource/mpesa-api-go"
)

const (
	appKey    = ""
	appSecret = ""
)

func main() {

	svc, err := mpesa.New(appKey, appSecret, mpesa.SANDBOX)
	if err != nil {
		panic(err)
	}

	res, err := svc.BalanceInquiry(mpesa.BalanceInquiry{
		Initiator:          "",
		SecurityCredential: "",
		CommandID:          "",
		PartyA:             "",
		IdentifierType:     "",
		Remarks:            "",
		QueueTimeOutURL:    "",
		ResultURL:          "",
	})

	if err != nil {
		log.Println(err)
	}
	log.Println(res)

}
Transaction Status

This api allows you to check the status of transaction.

Reversal

This api allows you to do a transaction reversal

package main

import (
	"log"
	"github.com/AndroidStudyOpenSource/mpesa-api-go"
)

const (
	appKey    = ""
	appSecret = ""
)

func main() {

	svc, err := mpesa.New(appKey, appSecret, mpesa.SANDBOX)
	if err != nil {
		panic(err)
	}

	res, err := svc.Reversal(mpesa.Reversal{
		Initiator:              "",
		SecurityCredential:     "",
		CommandID:              "",
		TransactionID:          "",
		Amount:                 0,
		ReceiverParty:          "",
		ReceiverIdentifierType: "",
		QueueTimeOutURL:        "",
		ResultURL:              "",
		Remarks:                "",
		Occassion:              "",
	})

	if err != nil {
		log.Println(err)
	}
	log.Println(res)

}
Contributing

We’re glad you’re interested in MPESA Daraja Golang SDK, and we’d love to see where you take it. If you would like to contribute code to this project you can do so through GitHub by Forking the Repository and creating a Pull Request.

When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible. We look forward to you submitting a Pull Request.

Use gitflow. Always tag releases to develop and master.

Thanks, and please do take it for a joyride!

License
MIT License

Copyright (c) 2018 Android Study Open Source

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Index

Constants

View Source
const (

	// SANDBOX is the sandbox env tag
	SANDBOX = iota
	// PRODUCTION is the production env tag
	PRODUCTION
)

Variables

This section is empty.

Functions

func GeneratePassword

func GeneratePassword(shortCode, passkey, timestamp string) string

GeneratePassword by base64 encoding BusinessShortcode, Passkey, and Timestamp

Types

type B2B

type B2B struct {
	Initiator              string
	SecurityCredential     string
	CommandID              string
	SenderIdentifierType   string
	RecieverIdentifierType string
	Amount                 int
	PartyA                 string
	PartyB                 string
	Remarks                string
	AccountReference       string
	QueueTimeOutURL        string
	ResultURL              string
}

B2B is a model

type B2C

type B2C struct {
	InitiatorName      string
	SecurityCredential string
	CommandID          string
	Amount             int
	PartyA             string
	PartyB             string
	Remarks            string
	QueueTimeOutURL    string
	ResultURL          string
	Occassion          string
}

B2C is a model

type BalanceInquiry

type BalanceInquiry struct {
	Initiator          string
	SecurityCredential string
	CommandID          string
	PartyA             string
	IdentifierType     string
	Remarks            string
	QueueTimeOutURL    string
	ResultURL          string
}

BalanceInquiry is a model

type C2B

type C2B struct {
	ShortCode     string
	CommandID     string
	Amount        int
	Msisdn        string
	BillRefNumber string
}

C2B is a model

type C2BRegisterURL

type C2BRegisterURL struct {
	ShortCode       string
	ResponseType    string
	ConfirmationURL string
	ValidationURL   string
}

RegisterURL is a model

type Env

type Env string

Env is the environment type

type Express

type Express struct {
	BusinessShortCode string
	Password          string
	Timestamp         string
	TransactionType   string
	Amount            int
	PartyA            string
	PartyB            string
	PhoneNumber       string
	CallBackURL       string
	AccountReference  string
	TransactionDesc   string
}

Express is a model

func NewExpress

func NewExpress(shortCode string, amount int, phoneNumber, callbackURL, ref, desc, passkey string) *Express

NewExpress creates an express request object. Does the password generation and timestamp

type Pull

type Pull struct {
	ShortCode  string
	StartDate  string
	EndDate    string
	PageNumber string
}

Pull is a model

type Reversal

type Reversal struct {
	Initiator              string
	SecurityCredential     string
	CommandID              string
	TransactionID          string
	Amount                 int
	ReceiverParty          string
	ReceiverIdentifierType string
	QueueTimeOutURL        string
	ResultURL              string
	Remarks                string
	Occassion              string
}

Reversal is a model

type Service

type Service struct {
	AppKey    string
	AppSecret string
	Env       int
}

Service is an Mpesa Service

func New

func New(appKey, appSecret string, env int) (Service, error)

New return a new Mpesa Service

func (Service) B2BRequest

func (s Service) B2BRequest(b2b B2B) (string, error)

B2BRequest sends a new request

func (Service) B2CRequest

func (s Service) B2CRequest(b2c B2C) (string, error)

B2CRequest sends a new request

func (Service) BalanceInquiry

func (s Service) BalanceInquiry(balanceInquiry BalanceInquiry) (string, error)

BalanceInquiry sends a balance inquiry

func (Service) C2BRegisterURL

func (s Service) C2BRegisterURL(c2bRegisterURL C2BRegisterURL) (string, error)

C2BRegisterURL requests

func (Service) C2BSimulation

func (s Service) C2BSimulation(c2b C2B) (string, error)

C2BSimulation sends a new request

func (Service) GetSecurityCredential

func (s Service) GetSecurityCredential(initiatorPassword string) (string, error)

GetSecurityCredential generates a security credential The security credential depends on the environment set

func (Service) PullTransactions

func (s Service) PullTransactions(pull Pull) (string, error)

BalanceInquiry sends a balance inquiry

func (Service) Reversal

func (s Service) Reversal(reversal Reversal) (string, error)

Reversal requests a reversal?

func (Service) Simulation

func (s Service) Simulation(express Express) (string, error)

Simulation requests user device for payment

func (Service) TransactionStatus

func (s Service) TransactionStatus(express Express) (string, error)

TransactionStatus gets status of a transaction

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL