public final class CommerceManager

  1. Object
  2. CommerceManager

Client facade for the Codename One Commerce service. It does not replace the Purchase API – it wraps it: purchases still go through the platform store, and this class adds server-side validation and a store-agnostic entitlement check (“does this user have pro?”) instead of per-SKU receipt juggling.

Typical use:

  CommerceManager cm = CommerceManager.getInstance();
  cm.setAppUserId(myAccountId);          // optional; stable per user
  cm.subscribe("pro_monthly");           // delegates to Purchase
  // later, off the EDT:
  cm.refresh();                          // validate receipts with the cloud
  if (cm.isEntitled("pro")) { ... }      // entitlement, not SKU

Graceful degradation. When the cloud is unreachable, or the developer’s account is over its monthly validated-volume cap, the cloud stops validating and isDegraded() becomes true. Entitlement checks then fall back to the store-direct signal (the platform’s own receipt), so a paying user is never locked out – exactly the server-side behaviour, mirrored on the device.

The cloud endpoint, build key and package are stamped into the build by the Codename One build server when commerce is enabled (codename1.arg.commerce.cloud.enabled=true); when they are absent this class is inert and every call is a safe no-op that defers to Purchase.

Methods

public static synchronized CommerceManager getInstance()The shared instance.
public boolean isCloudEnabled()Whether cloud commerce is active in this build.
public boolean isDegraded()True after a refresh() that could not get a server-validated answer – the cloud was unreachable or the account is over its monthly cap.
public void setAppUserId(String id)Sets the stable per-user id the cloud keys entitlements on.
public String getAppUserId()The current (explicit or generated-anonymous) app user id.
public void purchase(String sku)
public void subscribe(String sku)
public void unsubscribe(String sku)
public boolean isEntitled(String entitlementId)Whether the user currently holds entitlementId.
public List getActiveEntitlements()Snapshot of the entitlement ids currently considered active.
public void refresh()Validates the device’s current receipts with the cloud and updates the entitlement cache.

Inherited methods

Method details

getInstance

public static synchronized CommerceManager getInstance()
The shared instance.

isCloudEnabled

public boolean isCloudEnabled()
Whether cloud commerce is active in this build. True for any cloud build (one that carries a build_key) unless the developer explicitly disabled it with codename1.commerce.cloud.enabled=false. In the simulator / a local build there is no build key, so this is false and every call safely defers to Purchase.

isDegraded

public boolean isDegraded()
True after a refresh() that could not get a server-validated answer – the cloud was unreachable or the account is over its monthly cap. Entitlement checks fall back to the store-direct signal while this is true.

setAppUserId

public void setAppUserId(String id)
Sets the stable per-user id the cloud keys entitlements on. Pass your own account id once the user signs in; if never set, an anonymous id is generated and persisted so entitlements survive restarts on the device.

getAppUserId

public String getAppUserId()
The current (explicit or generated-anonymous) app user id.

purchase

public void purchase(String sku)

subscribe

public void subscribe(String sku)

unsubscribe

public void unsubscribe(String sku)

isEntitled

public boolean isEntitled(String entitlementId)
Whether the user currently holds entitlementId. Uses the last cloud-validated answer when available; otherwise (cloud disabled or degraded) falls back to the store-direct signal, treating the entitlement id as a subscription SKU so a paying user keeps access.

getActiveEntitlements

public List getActiveEntitlements()
Snapshot of the entitlement ids currently considered active.

refresh

public void refresh()
Validates the device’s current receipts with the cloud and updates the entitlement cache. Blocking network call – invoke off the EDT. Safe no-op when the cloud is not enabled in this build. On any failure the manager flips to degraded and leaves the previous (or store-direct) answers in place.