Skip to content

vgichira/mpesa-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Safaricom Mpesa Golang SDK

This is an unofficial Mpesa Daraja API wrapper. You can easily install and reuse this package in you Go projects. This allows you to focus on the business logic by leaving the Mpesa APIs heavylifting.

Installation

This documentation assumes that you already have Go installed on your device. If you have not installed Go, check out the documentation on how to install it on your computer.

go get github.com/vgichira/mpesa-wrapper

Initialize instance of the gateway

To start using the SDK, we first need to create a new instance of the service with the daraja app credentials and the environment. Environment is either SANDBOX or PRODUCTION. Default environment is SANDBOX.

mpesaService, err := mpesa.Init("CONSUMER_KEY_HERE", "CONSUMER_SECRET_HERE", "ENVIROMENT (SANDBOX / LIVE)")

Generate Lipa Na Mpesa (STK Push Password

When sending an STK Push request to a client phone number, one of the parameters required if the password. The password is generated by Base64 encoding the shortcode, LNM passkey and the timestamp (YYYYMMDDHHMMSS).

{
    "BusinessShortCode": "",
    "Password": "", // This password
    "Timestamp": "",
    "TransactionType": "",
    "Amount": "",
    "PartyA": "",
    "PartyB": "",
    "PhoneNumber": "",
    "CallBackURL": ""
    "AccountReference": "",
    "TransactionDesc": "" ,
}

To generate the password, you need to call the GenerateLipaNaMpesaPassword method from the SDK and pass shortcode, passkey and the timestamp as parameters.

password := mpesaService.GenerateLipaNaMpesaPassword("SHORTCODE", "PASSKEY", "TIMESTAMP")

Send Lipa Na Mpesa (STK Push) Request

To send STK Push to client's phone number, you need to call the LipaNaMpesaOnline method and pass the payload as a parameter.

requestBody := &mpesa.LipaNaMpesaRequest {
    BusinessShortCode: "",
    Password: "",
    Timestamp: "",
    TransactionType: "",
    Amount: "",
    PartyA: "",
    PartyB: "",
    PhoneNumber: "",
    CallBackURL: "",
    AccountReference: "",
    TransactionDesc: "" ,
}

response, err := mpesaService.LipaNaMpesaOnline(requestBody)

Register C2B Confirmation and Validation URLs

requestBody:= &mpesa.RegisterURL {
    ValidationURL: "",
    ConfirmationURL: "",
    ResponseType: "", // Cancelled / Completed
    ShortCode: "",
}

response, err := mpesaService.RegisterURL(requestBody)

Simulate C2B Transaction (Sandbox)

    requestBody := &mpesa.C2BTransaction {
        CommandID: "", // CustomerPayBillOnline / CustomerBuyGoodsOnline
        Amount: "",
        MSISDN: "",
        BillRefNumber: "",
        ShortCode: "",
    }

    response, err:= mpesaService.SimulateC2B(requestBody)

Generate Security Credential

securityCredential, err := mpesaService.GenerateSecurityCredential("PUBLIC_CERT_LOCATION", "INITIATOR_PASSWORD")

Initiate B2C Transaction (Business to Customer)

requestBody := &mpesa.B2C {
    InitiatorName: "",
    SecurityCredential: "", // Security credential generated above
    CommandID: "", // Salary Payments / Business Payments / Promotion Payments
    Amount: "",
    PartyA: "",
    PartyB: "",
    Remarks: "",
    QueueTimeoutURL: "",
    ResultURL: "",
    Occassion: "",
}

response, err := mpesaService.InitiateB2C(requestBody)

Reverse Transaction

requestBody := &mpesa.Reversal {
    Initiator: "",
    SecurityCredential: "",
    CommandID: "",
    TransactionID: "",
    Amount: "",
    ReceiverParty: "",
    ReceiverIdentifierType: "",
    ResultURL: "",
    QueueTimeoutURL: "",
    Remarks: "",
    Occasion: "",
}

response, err := mpesaService.ReverseTransaction(requestBody)

Check Transaction Status

requestBody := &mpesa.TransactionStatus {
    Initiator: "",
    SecurityCredential: "",
    CommandID: "",
    TransactionID: "",
    PartyA: "",
    IdentifierType: "",
    ResultURL: "",
    QueueTimeoutURL: "",
    Remarks: "",
    Occasion: "",
}

response, err := mpesaService.CheckTransactionStatus(requestBody)

About

This is an Mpesa Daraja API wrapper

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages