Skip to content

stripe/mpp-java

mpp-java

Java SDK for the Machine Payments Protocol

Note: This is an experimental SDK and the API is subject to change.

License

Installation

Gradle

implementation 'com.stripe:mpp-java:0.1.0'

Maven

<dependency>
    <groupId>com.stripe</groupId>
    <artifactId>mpp-java</artifactId>
    <version>0.1.0</version>
</dependency>

Usage

Testnet

// 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

Mainnet

// 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
}

About

Java SDK for the Machine Payments Protocol

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors