Skip to content

stripe/mpp-java

Repository files navigation

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.1'

Maven

<dependency>
    <groupId>com.stripe</groupId>
    <artifactId>mpp-java</artifactId>
    <version>0.1.1</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
}

Releasing

  1. Create a release branch and update VERSION_NAME in gradle.properties and the version references in this README.
  2. Open a PR and let CI pass.
  3. Merge the PR to main.
  4. Tag the merge commit and push the tag:
    git checkout main && git pull
    git tag v<version>
    git push origin v<version>
  5. The Publish workflow will automatically build, test across Java 11/17/21, and publish to Maven Central.
  6. Verify the artifact appears on Maven Central.

About

Java SDK for the Machine Payments Protocol

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

4 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors