Skip to navigation

Go SDK middleware

What the Charge middleware does with each request, and how it ties payments to routes.
View as Markdown

The Charge method wraps your handler in standard net/http middleware. The middleware stands between the caller and your handler, and it only lets a request through after Mitu has settled a payment for it. This page explains exactly what the middleware does, so you can predict how your routes will behave.

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

Request lifecycle

Every request that reaches a charged route falls into one of the cases below. The middleware recognises which case applies and responds accordingly.

Incoming requestWhat the middleware does
No paymentIt asks Mitu to create a payment for this method, path and price, and returns Mitu’s 402 response with the MPP and x402 options, so the caller learns exactly what to pay and how.
A payment for this route at this priceIt asks Mitu to settle the payment, then runs your handler with the payment in its context and adds Mitu’s receipt headers to the response. Your handler only ever runs after the money has settled.
A payment for a different route or priceIt returns a new 402 for this route and price, so a small payment on one endpoint cannot unlock another.
A payment that Mitu refusesIt returns Mitu’s response unchanged, so the caller can see why the payment was not accepted. Your handler does not run.
A request whose price is freeIt runs your handler without creating a payment. PaymentFromContext reports false, so your handler knows no payment was taken.

How payments are tied to routes

When the middleware creates a payment, Mitu records the request method, the path, the amount and the currency alongside it. On the paid retry, the middleware compares all four against the route being called and only accepts the payment if every one of them matches.

This is why a price set with AmountFunc must give the same answer on both requests. If the price changes between the unpaid request and the retry, the stored amount no longer matches and the caller is asked to pay again. Base dynamic prices on stable information you control, such as a stored order total, and never on anything the caller sends in the request body.

When something goes wrong

The middleware is designed so that a failure never lets a request through unpaid.

ConditionWhat happens
Mitu is unreachable, rate limiting or failingThe route replies with 503 Service Unavailable and a Retry-After header, telling the caller to try again shortly. Your handler does not run.
Mitu refuses a call from the SDK itself, for example because the key was revokedThe route replies with 500 Internal Server Error and the cause is written to the log.
AmountFunc returns an error or an invalid amountThe route replies with 500 Internal Server Error.
A fixed Price is invalidCharge panics when the route is registered, with an error wrapping ErrConfig. This normally happens at startup, before any traffic arrives.

Using the middleware with a framework

Charge returns the standard func(http.Handler) http.Handler middleware shape, so it works with net/http and with any router that accepts that shape. The example below shows it with chi.

r := chi.NewRouter()
r.With(m.Charge(mitu.Price{Amount: "0.05"})).Get("/v1/search", search)

Gin, Echo and Fiber use their own middleware types, so the SDK provides an adapter module for each. The adapters behave exactly like the core middleware.

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

In Echo and Fiber, any error your handler returns is passed back to the framework as usual, so your existing error handling keeps working.

When your handler fails after payment

Once a payment has settled it is final. If your handler fails after the caller has paid, do not ask the caller to pay again. Retry the work, or refund the payment, so that the caller is never charged twice for one result.