Skip to content

nullbase-dev/paystack-go

Repository files navigation

Paystack Golang SDK

Simple and un-obtrusive wrapper for Paystack APIs. Majorly exists cause of my distaste in existing packages.

Installation

go get github.com/Nullbase-Technologies/paystack-go

Usage

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/Nullbase-Technologies/paystack-go"
	"github.com/Nullbase-Technologies/paystack-go/transactions"
)

func main() {
	client, err := paystack.New(
		paystack.WithSecretKey("sk_test_xxxxxxxxx"),
		// Optionally initialize client with your own http client
		paystack.WithHTTPClient(CustomHTTPClient{}),
	)
	if err != nil {
		log.Fatal(err)
	}

	transaction := transactions.New(client)

	initResp, err := transaction.Initialize(context.TODO(), &transactions.InitializeTransactionOptions{
		Amount: 1000,
		Email:  "adedaramola@gmail.com",
	})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(initResp.AccessCode)
	fmt.Println(initResp.Reference)
	fmt.Println(initResp.AuthorizationURL)
}

Webhook Validation

The package also provides a simple helper function to validate incoming paystack webhooks. A use case could be something like below

func VerifyPaystackSignature(next http.Handler) http.Handler {
	return http.HandleFunc(func(w http.ResponseWriter, r *http.Request) {
		// Load paystack secret key from environment
		// or however you choose to do so, no sha hardcode am!
		secret := os.Getenv("PAYSTACK_SECRET_KEY")

		signature := r.Header.Get("X-Paystack-Signature")

		bodyBytes, err := io.ReadAll(r.Body)
		if err != nil {
			http.Error(w, "failed to read request body", http.StatusInternalServerError)
			return
		}

		if err := paystack.VerifyWebhookSignature(
			bodyBytes, signature, secret); err != nil {
			// You can type hint the actual invalid webhook error
			if errors.Is(err, paystack.ErrInvalidPaystackWebhook) {
				// do whatever you want
			}
			// maybe throw an internal server error
		}

		// everything is good
		// handle the webhook, however you will of course :)

		next.ServeHTTP(w, r)
	})
}

SDK Coverage

  • Transactions
    • Initialize Transaction
    • Verify Transaction
    • List Transactions
    • Fetch Transaction
    • Charge Authorization
    • View Transaction Timeline
    • Transaction Totals
    • Export Transactions
    • Partial Debit
  • Transaction Splits
    • Create Split
    • List/Search Split
    • Fetch Split
    • Update Split
    • Add/Update Split Subaccount
    • Remove Subaccount from Split
  • Customers
    • Create Customer
    • List Customer
    • Fetch Customer
    • Update Customer
    • Validate Customer
    • Whitelist/Blacklist Customer
    • Initialize Authorization
    • Verify Authorization
    • Deactivate Authorization
    • Initialize Direct Debit
    • Direct Debit Activation Charge
    • Fetch Mandate Authorization
  • Plans
    • Create Plan
    • List Plan
    • Fetch Plan
    • Update Plan
  • Miscellaneous
    • List Banks
    • List Countries
    • List States

About

golang sdk for paystack

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages