Skip to navigation

Go SDK API reference

Every exported function and type in the Go SDK and its adapters, with what each one does.
View as Markdown

Import the core package under the name mitu.

import mitu "github.com/mitulabs/payments/go"

The core package exports a client type, a function to create it, the types that describe prices and payments, a helper for reading the payment in a handler, and one error value. The Gin, Echo and Fiber adapters each live in their own module and export a single function. For the options you can set, see Configuration. For what happens on each request, see Middleware. For failures, see Errors.

New

func New(ctx context.Context, opts Options) (*Mitu, error)

Creates a client from your options. The function validates the options immediately and returns an error wrapping ErrConfig if any of them is invalid. If the context is already done, it returns ctx.Err() instead. It does not make any network calls.

Mitu

type Mitu struct {
// contains unexported fields
}

The client that charges for your routes. Always create it with New. A single client is safe to use from many goroutines at once and holds no background resources, so you can create it once and share it across your whole program.

Mitu.Charge

func (m *Mitu) Charge(p Price) func(http.Handler) http.Handler

Returns standard net/http middleware that serves a request only after Mitu has settled a payment for it at price p. The middleware also works with chi and any other router that accepts this middleware shape.

Charge checks the price when you call it. If the price is invalid, for example because the amount has three decimal places, it panics with an error wrapping ErrConfig. Because routes are usually registered when the program starts, this means a bad price is caught before any traffic arrives.

mux.Handle("GET /v1/search", m.Charge(mitu.Price{Amount: "0.05"})(http.HandlerFunc(search)))

Options

type Options struct {
APIKey string
DefaultCurrency string
HTTPClient *http.Client
Logger *slog.Logger
}

The settings you pass to New. Each field is described on the Configuration page.

Price

type Price struct {
Amount string
AmountFunc func(r *http.Request) (string, error)
Currency string
Description string
}

Describes how much a route costs. Set exactly one of Amount or AmountFunc.

FieldDescription
AmountA fixed price as a decimal string, such as "5000" or "12.50". It must be greater than zero and can have at most two decimal places.
AmountFuncA function that works out the price for each request. Returning an empty string serves that request free. Returning an error, or an invalid amount, causes the route to reply with 500.
CurrencyThe currency code for this price. If you leave it empty, Options.DefaultCurrency is used.
DescriptionA short label shown to the caller in the payment challenge.

The examples below cover the common cases.

// A fixed price in the default currency
mitu.Price{Amount: "0.05"}
// A fixed price in a named currency, with a description
mitu.Price{Amount: "2500", Currency: "NGN", Description: "Search"}
// A price that depends on the caller's plan
mitu.Price{AmountFunc: func(r *http.Request) (string, error) {
if isPremium(r) {
return "1.00", nil
}
return "0.10", nil
}}

When you use AmountFunc, it must return the same amount on the first, unpaid request and on the paid retry, otherwise the payment will not match and the caller will be asked to pay again. Base the price on information you control, such as a stored order total, and never read it from the request body.

Payment

type Payment struct {
ID string
Reference string
TxHash string
AmountPaid string
Currency string
Network string
PaymentMethod string
}

Describes a payment that Mitu has settled for a request.

FieldDescription
IDMitu’s identifier for the payment. It matches the identifier in the payment.completed webhook, so you can link the two.
ReferenceThe unique reference the payment was created with.
TxHashThe hash of the settlement transaction, when the payment method settles onchain.
AmountPaidThe amount the caller paid, as a decimal string in Currency.
CurrencyThe currency of AmountPaid.
NetworkThe network the payment settled on.
PaymentMethodThe payment method the caller used, as Mitu names it.

PaymentFromContext

func PaymentFromContext(ctx context.Context) (Payment, bool)

Returns the payment for a request that was served through Charge. The second value is false when the request was served free, in which case there is no payment to read.

ErrConfig

var ErrConfig = errors.New("mitu: invalid configuration")

The error that every configuration problem wraps. New returns it for invalid options, and Charge panics with it for an invalid price. Use errors.Is to check for it.

Framework adapters

Each adapter is a separate module under github.com/mitulabs/payments/go with a single function, Charge(m *mitu.Mitu, p mitu.Price), which returns the middleware type of its framework. It behaves exactly like the core middleware, and your handler reads the payment with mitu.PaymentFromContext using the request’s context.

FrameworkPackageRead the payment from
Ginmituginc.Request.Context()
Echomituechoc.Request().Context()
Fibermitufiberc.Context()

The example below shows the Gin adapter.

import mitugin "github.com/mitulabs/payments/go/gin"
router.GET("/v1/search", mitugin.Charge(m, mitu.Price{Amount: "0.05"}), search)