This package provides a go client for interacting with the CamPay API
campay-go-sdk is compatible with modern Go releases in module mode, with Go installed:
go get github.com/NdoleStudio/campay-go-sdkAlternatively the same can be achieved if you use import in a package:
import "github.com/NdoleStudio/campay-go-sdk"- 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 numberGET /api/utilities/transaction/{reference}/: Get the status of a transaction
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),
)
}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
}This handles all API requests whose URL begins with /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...This handles all API requests whose URL begins with /collect/
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-acb031cf0de4This handles all API requests whose URL begins with /withdraw/
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-acb031cf0de4This handles all API requests whose URL begins with /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-acb031cf0de4transaction, 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-acb031cf0de4transaction, httpResponse, err := client.Utilities.AirtimeTransferSync(context.Background(), "" /* Transaction reference */)
if err != nil {
log.Fatal(err)
}
log.Println(transaction.Status) // e.g "SUCCESSFUL"You can run the unit tests for this client from the root directory using the command below:
go test -vIf you discover any security related issues, please email [email protected] instead of using the GitHub issues.
This project is licensed under the MIT License - see the LICENSE file for details
