Go
The sanwo Go package generates checkout HTML and JavaScript for any Go web application. It works with net/http, Gin, Echo, Fiber, or any framework that serves HTML.
Installation
Section titled “Installation”go get github.com/Sanwohq/gohttps://pkg.go.dev/github.com/Sanwohq/gohttps://github.com/Sanwohq/gopackage main
import ( "net/http" "html/template"
sanwo "github.com/Sanwohq/go")
var tmpl = template.Must(template.New("page").Parse(`<!DOCTYPE html><html><body> {{.Scripts}} {{.Checkout}}</body></html>`))
func main() { s := sanwo.New(sanwo.Config{ Provider: sanwo.ProviderPaystack, PublicKey: "pk_test_xxxxx", Currency: "NGN", })
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { tmpl.Execute(w, map[string]template.HTML{ "Scripts": s.RenderScript(), "Checkout": s.RenderCheckout(sanwo.CheckoutOptions{ Amount: 500000, Email: "customer@example.com", ButtonText: "Pay ₦5,000", }), }) })
http.ListenAndServe(":8080", nil)}sanwo.New(cfg Config) *Sanwo
Section titled “sanwo.New(cfg Config) *Sanwo”Create a new Sanwo client.
s := sanwo.New(sanwo.Config{ Provider: sanwo.ProviderPaystack, PublicKey: "pk_test_xxxxx", Currency: "NGN", // default "NGN" Debug: false, // default false})RenderScript() template.HTML
Section titled “RenderScript() template.HTML”Returns a <script> tag that loads the Sanwo embed script from CDN. Include once per page.
RenderCheckout(opts CheckoutOptions) template.HTML
Section titled “RenderCheckout(opts CheckoutOptions) template.HTML”Returns a checkout button with inline JavaScript.
html := s.RenderCheckout(sanwo.CheckoutOptions{ Amount: 500000, Email: "user@example.com", ButtonText: "Pay Now", ButtonClass: "sanwo-button", Description: "Order #123", Currency: "NGN", FirstName: "John", LastName: "Doe", Callback: "onPaymentComplete",})CheckoutOptions
Section titled “CheckoutOptions”| Field | Type | Required | Default | Description |
|---|---|---|---|---|
Amount |
int |
Yes | — | Amount in minor units (kobo, cents) |
Email |
string |
Yes | — | Customer email |
ButtonText |
string |
No | "Pay Now" |
Button label |
ButtonClass |
string |
No | "sanwo-button" |
CSS class |
Description |
string |
No | — | Payment description |
Reference |
string |
No | Auto-generated | Transaction reference |
Currency |
string |
No | Config value | Currency code |
FirstName |
string |
No | — | Customer first name |
LastName |
string |
No | — | Customer last name |
Phone |
string |
No | — | Customer phone |
Callback |
string |
No | — | JS callback function name |
RenderCustomAmount(opts CustomAmountOptions) template.HTML
Section titled “RenderCustomAmount(opts CustomAmountOptions) template.HTML”Returns a custom-amount widget where users enter their own amount.
html := s.RenderCustomAmount(sanwo.CustomAmountOptions{ Email: "donor@example.com", ButtonText: "Donate", Placeholder: "Enter amount",})GetConfig() map[string]interface{}
Section titled “GetConfig() map[string]interface{}”Returns the current configuration as a map.
Providers
Section titled “Providers”sanwo.ProviderPaystacksanwo.ProviderFlutterwavesanwo.ProviderRazorpaysanwo.ProviderMonnifysanwo.ProviderInterswitchsanwo.ProviderCustomCustom provider templates
Section titled “Custom provider templates”s := sanwo.New(sanwo.Config{ Provider: sanwo.ProviderCustom, PublicKey: "your_key", TemplateURL: "https://example.com/my-provider-template.html", // or: Template: `<div id="payment-form">...</div>`,})Handling results
Section titled “Handling results”<script> function onPaymentComplete(result) { if (result.status === 'successful') { window.location.href = '/thank-you?ref=' + result.reference; } }</script>s.RenderCheckout(sanwo.CheckoutOptions{ Amount: 500000, Email: "user@example.com", Callback: "onPaymentComplete",})Source code
Section titled “Source code”Sanwohq/go on GitHub.