Go SDK middleware
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.
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.
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.
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.
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.
Gin
Echo v5
Fiber v3
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.