Price provider
By default, the @X402PayUSDC annotation uses a fixed price, defined directly in the amount field:
@X402PayUSDC(amount = "5.00")
@GetMapping("/data")
public ResponseEntity<String> getData() {
return ResponseEntity.ok("Here is your protected data");
}In some cases, however, the required payment amount depends on runtime conditions. For example, query parameters, user tiers, or data size.
To handle this, you can use a dynamic pricing provider via the amountProvider field.
🧩 Step 1 — Implement an AmountProvider.
An AmountProvider is a simple Java interface:
public interface AmountProvider {
BigDecimal getAmount(HttpServletRequest request);
}It receives the current HTTP request and returns the amount.
Example : Simple fixed provider
public final class SimpleAmountProvider implements AmountProvider {
@Override
public BigDecimal getAmount(HttpServletRequest request) {
return new BigDecimal("0.10"); // always 10 cents
}
}Example : Provider based on query parameters
public final class TypeBasedProvider implements AmountProvider {
@Override
public BigDecimal getAmount(HttpServletRequest request) {
String type = request.getParameter("type");
return switch (type) {
case "image" -> new BigDecimal("0.20");
case "text" -> new BigDecimal("0.05");
default -> new BigDecimal("0.01");
};
}
}🧩 Step 2 — Reference your provider in the annotation
Simply reference the class in the annotation:
@X402PayUSDC(amount = "5.00", amountProvider = TypeBasedProvider.class)
@GetMapping("/generate")
public ResponseEntity<String> generate() {
return ResponseEntity.ok("Generated content");
}At runtime, Mogami’s resolver automatically:
Checks if the provider class is a Spring Bean (
@Component,@Service, etc.).Falls back to manual instantiation if it’s a plain Java class.
The
amountfield is ignored ifamountProvideris set
Last updated