flutterwave

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2023 License: MIT Imports: 10 Imported by: 0

README

flutterwave-go

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

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

Installation

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

go get github.com/NdoleStudio/flutterwave-go

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

import "github.com/NdoleStudio/flutterwave-go"

Implemented

  • Bills
    • POST /bills/: Create a bill payment
    • GET /bill-items/{item_code}/validate: Validate services like DStv smartcard number, Meter number etc.
    • GET /bills/{reference}: Get the verbose status of a bill payment
  • Payments
    • GET /v3/transactions/:id/verify: Verify a transaction
    • POST /v3/transactions/:id/refund: Create a Refund

Usage

Initializing the Client

An instance of the flutterwave client can be created using New().

package main

import (
	"github.com/NdoleStudio/flutterwave-go"
)

func main()  {
	client := flutterwave.New(
		flutterwave.WithSecretKey("" /* flutterwave Secret Key */),
	)
}
Error handling

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

data, httpResponse, err := flutterwaveClient.Bills.Create(context.Background(), request)
if err != nil {
    //handle error
}
BILLS
Create a bill payment

POST /bills/: Create a bill payment

response, _, err := flutterwaveClient.Bills.CreatePayment(context.Background(), &BillsCreatePaymentRequest{
    Country:    "NG",
    Customer:   "7034504232",
    Amount:     100,
    Recurrence: "ONCE",
    Type:       "DSTV",
    Reference:  uuid.New().String(),
    BillerName: "DSTV",
})

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

log.Println(response.Status) // success
Create a bill payment

GET /bill-items/{item_code}/validate: validate services like DSTV smartcard number, Meter number etc.

response, _, err := flutterwaveClient.Bills.Validate(context.Background(), "CB177", "BIL099", "08038291822")

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

log.Println(response.Status) // success
Get verbose status of a bill payment

GET /bills/{reference}: get the verbose status of a bill purchase

response, _, err := flutterwaveClient.Bills.GetStatusVerbose(context.Background(), "9300049404444")

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

log.Println(response.Status) // success

Testing

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

go test -v

License

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

Documentation

Index

Constants

View Source
const (
	// HeaderNameVerifHash is the name of the header used to verify your webhook requests.
	HeaderNameVerifHash = "verif-hash"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BillsCreatePaymentRequest

type BillsCreatePaymentRequest struct {
	Country    string `json:"country"`
	Customer   string `json:"customer"`
	Amount     int    `json:"amount"`
	Recurrence string `json:"recurrence,omitempty"`
	Type       string `json:"type"`
	Reference  string `json:"reference,omitempty"`
	BillerName string `json:"biller_name,omitempty"`
}

BillsCreatePaymentRequest is data needed to creat a payment

type BillsCreatePaymentResponse

type BillsCreatePaymentResponse struct {
	Status  string `json:"status"`
	Message string `json:"message"`
	Data    struct {
		PhoneNumber string `json:"phone_number"`
		Amount      int    `json:"amount"`
		Network     string `json:"network"`
		FlwRef      string `json:"flw_ref"`
		TxRef       string `json:"tx_ref"`
	} `json:"data"`
}

BillsCreatePaymentResponse is the data returned after creating a payment

func (BillsCreatePaymentResponse) IsSuccessfull

func (response BillsCreatePaymentResponse) IsSuccessfull() bool

IsSuccessfull determines if the bill payment was successfull

type BillsStatusVerboseResponse

type BillsStatusVerboseResponse struct {
	Status  string `json:"status"`
	Message string `json:"message"`
	Data    struct {
		Currency        string      `json:"currency"`
		CustomerID      string      `json:"customer_id"`
		Frequency       string      `json:"frequency"`
		Amount          string      `json:"amount"`
		Product         string      `json:"product"`
		ProductName     string      `json:"product_name"`
		Commission      int         `json:"commission"`
		TransactionDate time.Time   `json:"transaction_date"`
		Country         string      `json:"country"`
		TxRef           string      `json:"tx_ref"`
		Extra           interface{} `json:"extra"`
		ProductDetails  string      `json:"product_details"`
		Status          string      `json:"status"`
	} `json:"data"`
}

BillsStatusVerboseResponse is the verbose response of a bill payment

func (BillsStatusVerboseResponse) IsSuccessfull

func (response BillsStatusVerboseResponse) IsSuccessfull() bool

IsSuccessfull determines if the transaction was successfull

type BillsValidateResponse

type BillsValidateResponse struct {
	Status  string `json:"status"`
	Message string `json:"message"`
	Data    struct {
		ResponseCode    string      `json:"response_code"`
		Address         interface{} `json:"address"`
		ResponseMessage string      `json:"response_message"`
		Name            string      `json:"name"`
		BillerCode      string      `json:"biller_code"`
		Customer        string      `json:"customer"`
		ProductCode     string      `json:"product_code"`
		Email           interface{} `json:"email"`
		Fee             int         `json:"fee"`
		Maximum         int         `json:"maximum"`
		Minimum         int         `json:"minimum"`
	} `json:"data"`
}

BillsValidateResponse is the response after validating a bill service

func (BillsValidateResponse) IsSuccessfull

func (response BillsValidateResponse) IsSuccessfull() bool

IsSuccessfull determines if the bill validation was successfull

type Client

type Client struct {
	Bills        *billsService
	Payments     *paymentsService
	Transactions *transactionsService
	// contains filtered or unexported fields
}

Client is the flutterwave API client. Do not instantiate this client with Client{}. Use the New method instead.

func New

func New(options ...ClientOption) *Client

New creates and returns a new flutterwave.Client from a slice of flutterwave.ClientOption.

type ClientOption

type ClientOption interface {
	// contains filtered or unexported methods
}

ClientOption are options for constructing a client

func WithBaseURL

func WithBaseURL(baseURL string) ClientOption

WithBaseURL set's the base url for the flutterwave API

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) ClientOption

WithHTTPClient sets the underlying HTTP client used for API requests. By default, http.DefaultClient is used.

func WithSecretKey

func WithSecretKey(secretKey string) ClientOption

WithSecretKey set's the secret key used to authorize requests to the flutterwave API See: https://developer.flutterwave.com/docs/api-keys

type GetPaymentLinkCustomer added in v0.0.4

type GetPaymentLinkCustomer struct {
	Email       string `json:"email"`
	Name        string `json:"name"`
	PhoneNumber string `json:"phonenumber"`
}

GetPaymentLinkCustomer contains the customer details.

type GetPaymentLinkCustomizations added in v0.0.4

type GetPaymentLinkCustomizations struct {
	Title string `json:"title"`
}

GetPaymentLinkCustomizations contains options to customize the look of the payment modal.

type GetPaymentLinkRequest added in v0.0.4

type GetPaymentLinkRequest struct {
	TransactionReference string                       `json:"tx_ref"`
	Amount               string                       `json:"amount"`
	Currency             string                       `json:"currency"`
	Meta                 map[string]interface{}       `json:"meta"`
	RedirectURL          string                       `json:"redirect_url"`
	Customer             GetPaymentLinkCustomer       `json:"customer"`
	Customizations       GetPaymentLinkCustomizations `json:"customizations"`
}

GetPaymentLinkRequest is data needed to create a payment link

type GetPaymentLinkResponse added in v0.0.4

type GetPaymentLinkResponse struct {
	Status  string `json:"status"`
	Message string `json:"message"`
	Data    struct {
		Link string `json:"link"`
	} `json:"data"`
}

GetPaymentLinkResponse is the data returned after creating a payment link

type PaymentEventV3 added in v0.0.4

type PaymentEventV3 struct {
	Event string `json:"event"`
	Data  struct {
		ID                int64     `json:"id"`
		TxRef             string    `json:"tx_ref"`
		FlwRef            string    `json:"flw_ref"`
		DeviceFingerprint string    `json:"device_fingerprint"`
		Amount            int       `json:"amount"`
		Currency          string    `json:"currency"`
		ChargedAmount     int       `json:"charged_amount"`
		AppFee            float64   `json:"app_fee"`
		MerchantFee       int       `json:"merchant_fee"`
		ProcessorResponse string    `json:"processor_response"`
		AuthModel         string    `json:"auth_model"`
		IP                string    `json:"ip"`
		Narration         string    `json:"narration"`
		Status            string    `json:"status"`
		PaymentType       string    `json:"payment_type"`
		CreatedAt         time.Time `json:"created_at"`
		AccountID         int       `json:"account_id"`
		Customer          struct {
			ID          int         `json:"id"`
			Name        string      `json:"name"`
			PhoneNumber interface{} `json:"phone_number"`
			Email       string      `json:"email"`
			CreatedAt   time.Time   `json:"created_at"`
		} `json:"customer"`
		Card struct {
			First6Digits string `json:"first_6digits"`
			Last4Digits  string `json:"last_4digits"`
			Issuer       string `json:"issuer"`
			Country      string `json:"country"`
			Type         string `json:"type"`
			Expiry       string `json:"expiry"`
		} `json:"card"`
	} `json:"data"`
	EventType string `json:"event.type"`
}

PaymentEventV3 is the payload for webhook requests after a payment

func (PaymentEventV3) IsFailed added in v0.0.4

func (event PaymentEventV3) IsFailed() bool

IsFailed checks if the payment failed

func (PaymentEventV3) IsSuccessful added in v0.0.4

func (event PaymentEventV3) IsSuccessful() bool

IsSuccessful checks if the payment event is successfull

type RefundTransactionResponse added in v0.0.6

type RefundTransactionResponse struct {
	Status  string `json:"status"`
	Message string `json:"message"`
	Data    struct {
		ID             int    `json:"id"`
		AccountID      int    `json:"account_id"`
		TxID           int    `json:"tx_id"`
		FlwRef         string `json:"flw_ref"`
		WalletID       int    `json:"wallet_id"`
		AmountRefunded int    `json:"amount_refunded"`
		Status         string `json:"status"`
		Destination    string `json:"destination"`
		Meta           struct {
			Source string `json:"source"`
		} `json:"meta"`
		CreatedAt time.Time `json:"created_at"`
	} `json:"data"`
}

RefundTransactionResponse is the payload generated when a transaction is refunded

type Response

type Response struct {
	HTTPResponse *http.Response
	Body         *[]byte
}

Response captures the http response

func (*Response) Error

func (r *Response) Error() error

Error ensures that the response can be decoded into a string inc ase it's an error response

type TransactionResponse added in v0.0.5

type TransactionResponse struct {
	Status  string `json:"status"`
	Message string `json:"message"`
	Data    struct {
		ID                int64     `json:"id"`
		TxRef             string    `json:"tx_ref"`
		FlwRef            string    `json:"flw_ref"`
		DeviceFingerprint string    `json:"device_fingerprint"`
		Amount            int       `json:"amount"`
		Currency          string    `json:"currency"`
		ChargedAmount     int       `json:"charged_amount"`
		AppFee            float64   `json:"app_fee"`
		MerchantFee       int       `json:"merchant_fee"`
		ProcessorResponse string    `json:"processor_response"`
		AuthModel         string    `json:"auth_model"`
		IP                string    `json:"ip"`
		Narration         string    `json:"narration"`
		Status            string    `json:"status"`
		PaymentType       string    `json:"payment_type"`
		CreatedAt         time.Time `json:"created_at"`
		AccountID         int       `json:"account_id"`
		Card              struct {
			First6Digits string `json:"first_6digits"`
			Last4Digits  string `json:"last_4digits"`
			Issuer       string `json:"issuer"`
			Country      string `json:"country"`
			Type         string `json:"type"`
			Token        string `json:"token"`
			Expiry       string `json:"expiry"`
		} `json:"card"`
		Meta          interface{} `json:"meta"`
		AmountSettled float64     `json:"amount_settled"`
		Customer      struct {
			ID          int       `json:"id"`
			Name        string    `json:"name"`
			PhoneNumber string    `json:"phone_number"`
			Email       string    `json:"email"`
			CreatedAt   time.Time `json:"created_at"`
		} `json:"customer"`
	} `json:"data"`
}

TransactionResponse is data returned when querying a transaction

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL