The VTPass Go library provides an interface to interact with the VTPass API for various services such as querying transaction status, purchasing products (electricity), verifying meter numbers, and fetching service variations and categories.
To install the VTPass Go library, use the following command:
go get github.com/CeoFred/vtpass-go
First, initialize the service by providing your API credentials and environment:
import (
"context"
"fmt"
"log"
"os"
vt "github.com/CeoFred/vtpass-go"
"github.com/joho/godotenv"
)
var apiKey, publicKey, secretKey string
var service *vt.VTService
func init() {
if err := godotenv.Load(".env"); err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
apiKey = os.Getenv("API_KEY")
publicKey = os.Getenv("PUBLIC_KEY")
secretKey = os.Getenv("SECRET_KEY")
}
func main() {
service = vt.NewVTService(apiKey, publicKey, secretKey, vt.EnvironmentSandbox)
available, err := service.Ping(context.Background())
if err != nil {
fmt.Println(err)
}
fmt.Println("service available:", available)
// Example usage
CheckBalance()
PurchaseElectricityPrepaid()
VerifyMeterNumber()
}
func CheckBalance() {
walletBalance, err := service.Balance(context.Background())
if err != nil {
fmt.Println(err)
}
fmt.Printf("wallet balance: %f\n", walletBalance.Contents.Balance)
}
func PurchaseElectricityPrepaid() {
id := service.GenerateRequestID()
response, err := service.PurchaseElectricity(context.Background(), vt.ElectricityPurchase{
RequestID: id,
ServiceID: "enugu-electric",
BillersCode: "1111111111111",
VariationCode: "prepaid",
Amount: 1000,
Phone: "8160583193",
})
if err != nil {
fmt.Println(err)
}
fmt.Println("prepaid purchase code:", response.PurchasedCode)
}
func VerifyMeterNumber() {
customer, err := service.VerifyMeterNumber(context.Background(), "1111111111111", "prepaid", "enugu-electric")
if err != nil {
fmt.Println(err)
}
fmt.Println("Name:", customer.CustomerName)
fmt.Println("Business Unit:", customer.BusinessUnit)
fmt.Println("Account Number:", customer.AccountNumber)
}
Creates a new instance of the VTService with the provided API credentials and environment (sandbox or live).
Checks the service availability.
Example Usage:
available, err := service.Ping(context.Background())
if err != nil {
fmt.Println(err)
}
fmt.Println("service available:", available)
Fetches the wallet balance.
Example Usage:
walletBalance, err := service.Balance(context.Background())
if err != nil {
fmt.Println(err)
}
fmt.Printf("wallet balance: %f\n", walletBalance.Contents.Balance)
Generates a unique request ID based on the current time and a UUID.
Example Usage:
requestID := service.GenerateRequestID()
fmt.Println("Request ID:", requestID)
Queries the status of a transaction using the request ID.
Example Usage:
txn, err := service.QueryTransaction(context.Background(), "request_id_here")
if err != nil {
fmt.Println(err)
}
fmt.Println("Transaction:", txn)
Purchases electricity by providing the necessary details.
Example Usage:
id := service.GenerateRequestID()
response, err := service.PurchaseElectricity(context.Background(), vt.ElectricityPurchase{
RequestID: id,
ServiceID: "enugu-electric",
BillersCode: "1111111111111",
VariationCode: "prepaid",
Amount: 1000,
Phone: "8160583193",
})
if err != nil {
fmt.Println(err)
}
fmt.Println("prepaid purchase code:", response.PurchasedCode)
VerifyMeterNumber(ctx context.Context, meter_number, meter_type, service_id string) (*CustomerInfo, error)
Verifies a meter number.
Example Usage:
customer, err := service.VerifyMeterNumber(context.Background(), "1111111111111", "prepaid", "enugu-electric")
if err != nil {
fmt.Println(err)
}
fmt.Println("Name:", customer.CustomerName)
fmt.Println("Business Unit:", customer.BusinessUnit)
fmt.Println("Account Number:", customer.AccountNumber)
Fetches the service variations for a given service ID.
Example Usage:
services, err := service.ServiceVariations(context.Background(), "enugu-electric")
if err != nil {
fmt.Println(err)
}
for _, service := range services {
fmt.Printf("Service: %s, Min Amount: %s\n", service.Name, service.VariationCode)
}
Fetches services by their identifier.
Example Usage:
services, err := service.ServiceByIdentifier(context.Background(), vt.IdentifierElectricityBill)
if err != nil {
fmt.Println(err)
}
for _, service := range services {
fmt.Printf("Service: %s, Min Amount: %s\n", service.Name, service.MaximumAmount)
}
Fetches all service categories.
Example Usage:
services, err := service.ServiceCategories(context.Background())
if err != nil {
fmt.Println(err)
}
for _, service := range services {
fmt.Println("Service:", service.Name)
}
All service methods return an error as the second return value. Check this error to handle any issues that arise during the API call.
Example Usage:
available, err := service.Ping(context.Background())
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Service available:", available)
}