# Class CommerceManager

java.lang.Object
com.codename1.payment.CommerceManager

public final class CommerceManager extends Object

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`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `List`
  `getActiveEntitlements()`
  Snapshot of the entitlement ids currently considered active.
  `String`
  `getAppUserId()`
  The current (explicit or generated-anonymous) app user id.
  `static CommerceManager`
  `getInstance()`
  The shared instance.
  `boolean`
  `isCloudEnabled()`
  Whether cloud commerce is active in this build.
  `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.
  `boolean`
  `isEntitled(String entitlementId)`
  Whether the user currently holds `entitlementId`.
  `void`
  `purchase(String sku)`
  `void`
  `refresh()`
  Validates the device's current receipts with the cloud and updates the entitlement cache.
  `void`
  `setAppUserId(String id)`
  Sets the stable per-user id the cloud keys entitlements on.
  `void`
  `subscribe(String sku)`
  `void`
  `unsubscribe(String sku)`

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### getInstance

    public static 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.