Java SDK for the Machine Payments Protocol
Note: This is an experimental SDK and the API is subject to change.
implementation 'com.stripe:mpp-java:0.1.0'<dependency>
<groupId>com.stripe</groupId>
<artifactId>mpp-java</artifactId>
<version>0.1.0</version>
</dependency>// Token contract address for the currency you accept
String currency = TempoDefaults.TESTNET_PATH_USD;
// Your server's realm — identifies this API in payment challenges
String realm = "api.example.com";
// Secret used to secure payment challenges
// https://mpp.dev/protocol/challenges#challenge-binding
String mppSecret = System.getenv("MPP_SECRET_KEY");
TempoMethod tempo = TempoMethod.of().testnet().build();
MppHandler mppHandler = Mpp.create(tempo, realm, mppSecret);
// In your HTTP handler: verify if MPP payment has been provided
VerifyResult result = mppHandler.charge(
request.getHeader("Authorization"),
ChargeRequest.of(tempo.chargeIntent(), "0.50", currency, "0xYourWalletAddress")
.description("Paid endpoint")
);
if (result instanceof VerifyResult.Challenged) {
response.setHeader("WWW-Authenticate",
((VerifyResult.Challenged) result).challenge().toWwwAuthenticate());
response.setStatus(402);
} else {
VerifyResult.Verified verified = (VerifyResult.Verified) result;
response.setHeader("Payment-Receipt", verified.receipt().toPaymentReceipt());
// serve your content
}Test the endpoint using the Tempo CLI:
tempo request https://your-api.com/your-endpoint// Token contract address for the currency you accept
String currency = TempoDefaults.MAINNET_USDC;
// Your server's realm — identifies this API in payment challenges
String realm = "api.example.com";
// Secret used to secure payment challenges
// https://mpp.dev/protocol/challenges#challenge-binding
String mppSecret = System.getenv("MPP_SECRET_KEY");
TempoMethod tempo = TempoMethod.of().build(); // mainnet is the default
MppHandler mppHandler = Mpp.create(tempo, realm, mppSecret);
// In your HTTP handler: verify if MPP payment has been provided
VerifyResult result = mppHandler.charge(
request.getHeader("Authorization"),
ChargeRequest.of(tempo.chargeIntent(), "0.50", currency, "0xYourWalletAddress")
.description("Paid endpoint")
);
if (result instanceof VerifyResult.Challenged) {
response.setHeader("WWW-Authenticate",
((VerifyResult.Challenged) result).challenge().toWwwAuthenticate());
response.setStatus(402);
} else {
VerifyResult.Verified verified = (VerifyResult.Verified) result;
response.setHeader("Payment-Receipt", verified.receipt().toPaymentReceipt());
// serve your content
}