Go SDK API reference
Import the core package under the name mitu.
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
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
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
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.
Options
The settings you pass to New. Each field is described on the Configuration page.
Price
Describes how much a route costs. Set exactly one of Amount or AmountFunc.
The examples below cover the common cases.
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
Describes a payment that Mitu has settled for a request.
PaymentFromContext
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
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.
The example below shows the Gin adapter.