项目作者: Paradox-3arthling

项目描述 :
Meant to be a wrapper library for the daraja API from safaricom Mpesa/Lipa na Mpesa
高级语言: Go
项目地址: git://github.com/Paradox-3arthling/daraja_wrapper.git
创建时间: 2020-06-20T09:44:46Z
项目社区:https://github.com/Paradox-3arthling/daraja_wrapper

开源协议:MIT License

下载


Go

daraja_wrapper

What it does

This is a wrapper library to the daraja API endpoints from safaricom Mpesa/Lipa na Mpesa

This is a project is still in dev, though working parts
should be ok.

Why is it useful

  • Making it easier to access the daraja API endpoints without actually having to know much about them.
  • Hopefully the project can be used to learn more about the endpoints and the ‘net/http’ package from golang.
  • Hopes to create a package that is able scale and while still being very fast.

How to get started

!!IMPORTANT!! do check for errors, am ignoring them for the sake of short examples.
This will be sandbox examples:

Lipa na M-pesa

Start by importing the project import "github.com/paradox-3arthling/daraja_wrapper":

  1. lipa_mpesa := daraja_wrapper.LipaNaMpesaPayStruct{
  2. BusinessShortCode: "",
  3. Timestamp: "yyyymmddhhmmss",
  4. Amount: "420",
  5. PartyA: "254XXXXXXXXX",
  6. PartyB: "",
  7. PhoneNumber: "254XXXXXXXXX",
  8. CallBackURL: CallBackURL,
  9. AccountReference: "test",
  10. TransactionDesc: "test",
  11. }
  12. // lipa na mpesa acc
  13. mpesa_auth := daraja_wrapper.Auth{
  14. Key: "some_key",
  15. Secret: "some_secret",
  16. }
  17. resp, _ := lipa_mpesa.LipaNaMpesaPayment(&mpesa_auth, "some_pass")
  18. // check for error 1st!
  19. fmt.Println("resp ->", resp)

Setting up callback url

  • A lot of the API endpoints from daraja have a field called CallBackURL this is, where if the process you are trying to do gets successful processed, safaricom will send extra information on the transaction you started was completed on the user side.
  • Side note is that you don’t have to use my callback to make the transaction there are a lot of alternatives.
    I’m using a side project(import import "github.com/paradox-3arthling/async_response_server") for this and requires a little set up for the tunnelling(done by node.js) but you can skip the tunnelling process if you are not behind a proxy.
    1. var succ_mess string = `{
    2. "ResponseCode": "00000000",
    3. "ResponseDesc": "success"
    4. }`
    This is the response we’ll send back to safaricom for successfully accepting the information sent.(Could do better than this probably :D)
    1. port := ":3310"
    2. ts := async_response_server.CreateHookServerAsync(port)
    3. fmt.Printf("The callback URL is '%s'\n", ts.Url)
    4. defer ts.Close()
    5. fmt.Println("waiting on info. from our saf.")
    6. feedback := <-ts.Feed_back
    7. ts.Feed_back <- succ_mess
    8. fmt.Println("saf message:\n", feedback)
    This creates a server that listens on ts.Url variable which listens for information and responds with the succ_mess message.

The tunneling, you can skip this if u have direct access to the internet. You also need ngrok installed.
Since the server we created listens on port 3310 we will run ngrok http 3310. Output on which url to use will come up, best to use the
https one. We’ll call this ngrok_ip

Finally our ts.Url will show ur local IP which would be wrong we need to replace the IP address of this URL with either ngrok_ip or your public IP for those connected directly to the internet. This will be your CallBackURL for transactions that need this.

WIP

  • Check issues for next feature updates to be done.

Who maintains and can contributes.

At the moment am the only maintainer and contributer open to help, contact me at floydqaranja@gmail.com