Integrate with Java
Install and configure the Commet Java SDK.
Install
<dependency>
<groupId>co.commet</groupId>
<artifactId>commet-java</artifactId>
<version>0.1.0</version>
</dependency>implementation("co.commet:commet-java:0.1.0")Configure
COMMET_API_KEY=ck_sandbox_xxximport co.commet.Commet;
import co.commet.Environment;
public class CommetClient {
public static final Commet commet = Commet.builder()
.apiKey(System.getenv("COMMET_API_KEY"))
.environment(Environment.SANDBOX)
.build();
}Create Customer and Subscribe
customers().create is idempotent — if a customer with the same externalId exists, it returns the existing record.
import co.commet.ApiResponse;
import java.util.Map;
commet.customers().create("user@example.com", "user_123");
ApiResponse subscription = commet.subscriptions().create(
null, "user_123", "pro",
null, null, null, null, null, null, null, null
);
Map<String, Object> data = (Map<String, Object>) subscription.getData();
String checkoutUrl = (String) data.get("checkout_url");The customer is redirected to checkout to complete payment.
Check Access
ApiResponse sub = commet.subscriptions().get("user_123");
Map<String, Object> subData = (Map<String, Object>) sub.getData();
String status = (String) subData.get("status");
ApiResponse access = commet.features().check("custom_branding", "user_123");
Map<String, Object> accessData = (Map<String, Object>) access.getData();
boolean allowed = (boolean) accessData.get("allowed");Track Usage
commet.usage().track(
"api_calls", null, "user_123",
1, null, null, null, null, null, null, null, null
);Usage is aggregated and billed at end of period.
Webhooks
import co.commet.resources.Webhooks;
import java.util.Map;
Webhooks webhooks = new Webhooks();
Map<String, Object> payload = webhooks.verifyAndParse(
rawBody, signature, webhookSecret
);
if (payload == null) {
// return 401
}
if ("subscription.activated".equals(payload.get("event"))) {
// handle activation
}Related
How is this guide?