Skip to content

NdoleStudio/campay-go-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Campay Go SDK

Build codecov Scrutinizer Code Quality Go Report Card GitHub contributors GitHub license PkgGoDev

This package provides a go client for interacting with the CamPay API

Installation

campay-go-sdk is compatible with modern Go releases in module mode, with Go installed:

go get github.com/NdoleStudio/campay-go-sdk

Alternatively the same can be achieved if you use import in a package:

import "github.com/NdoleStudio/campay-go-sdk"

Implemented

  • Token
    • POST /token: Get access token
  • Collect
    • POST /collect: Request Payment
  • Withdraw
    • POST /withdraw: Withdraw funds to a mobile money account
  • Transaction
    • GET /transaction/{reference}/: Get the status of a transaction
  • Utilities
    • POST /api/utilities/airtime/transfer/: Transfers airtime to a mobile number
    • GET /api/utilities/transaction/{reference}/: Get the status of a transaction

Usage

Initializing the Client

An instance of the campay client can be created using New(). The http.Client supplied will be used to make requests to the API.

package main

import (
	"github.com/NdoleStudio/campay-go-sdk"
	"net/http"
)

func main()  {
	client := campay.New(
		campay.WithAPIUsername("" /* campay API Username */),
		campay.WithAPIPassword("" /* campay API Password */),
		campay.WithEnvironment(campay.DevEnvironment),
	)
}

Error handling

All API calls return an error as the last return object. All successful calls will return a nil error.

payload, response, err := campayClient.Token(context.Background())
if err != nil {
  //handle error
}

Token

This handles all API requests whose URL begins with /token/

Get access token

POST /token/: Get access token

token, _, err := campayClient.Token(context.Background())

if err != nil {
    log.Fatal(err)
}

log.Println(token.Token) // e.g eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsInVpZCI6Mn0...

Collect

This handles all API requests whose URL begins with /collect/

POST /collect/: Request Payment

This endpoint is used to request payment from users.

collectResponse, httpResponse, err := campayClient.Collect(context.Background(), campay.CollectOptions{
    Amount: 100,
    Currency: "XAF",
    From: "2376XXXXXXXX",
    Description: "Test",
    ExternalReference: "",
})

if err != nil {
    log.Fatal(err)
}

log.Prinln(collectResponse.Reference) // e.g 26676007-1c31-46d7-9c71-acb031cf0de4

Withdraw

This handles all API requests whose URL begins with /withdraw/

POST /withdraw: Withdraw funds to a mobile money account

Withdraw funds from an app to a mobile money account.

withdrawResponse, response, err := client.Withdraw(context.Background(), &WithdrawParams{
    Amount:            100,
    To:                "2376XXXXXXXX",
    Description:       "Test",
    ExternalReference: nil,
})

if err != nil {
    log.Fatal(err)
}

log.Println(withdrawResponse.Reference) // e.g 26676007-1c31-46d7-9c71-acb031cf0de4

Transaction

This handles all API requests whose URL begins with /transaction/

GET /transaction/{reference}/: Get the status of a transaction

Use this endpoint to check for the status of an initiated transaction.

transaction, httpResponse, err := campayClient.Transaction.Get(
	context.Background(),
	"bcedde9b-62a7-4421-96ac-2e6179552a1a"
)

if err != nil {
    log.Fatal(err)
}

log.Println(transaction.Reference) // e.g 26676007-1c31-46d7-9c71-acb031cf0de4

Utilities

POST /api/utilities/airtime/transfer/: Transfers airtime to a mobile number

transaction, httpResponse, err := client.Utilities.AirtimeTransferSync(context.Background(), &AirtimeTransferParams{
  Amount:            "100",
  To:                "237677777777",
  ExternalReference: "sample-external-ref",
})

if err != nil {
    log.Fatal(err)
}

log.Println(transaction.Reference) // e.g 26676007-1c31-46d7-9c71-acb031cf0de4

GET /api/utilities/transaction/{reference}/: Get the status of a transaction

transaction, httpResponse, err := client.Utilities.AirtimeTransferSync(context.Background(), "" /* Transaction reference */)
if err != nil {
    log.Fatal(err)
}

log.Println(transaction.Status) // e.g "SUCCESSFUL"

Testing

You can run the unit tests for this client from the root directory using the command below:

go test -v

Security

If you discover any security related issues, please email [email protected] instead of using the GitHub issues.

Credits

License

This project is licensed under the MIT License - see the LICENSE file for details