Subscription

The Kiwi controls the user subscriptions. You only need to register the subscription, so you can recovery the subscriptions when you need to check if can enable some content/feature for the user.

The kiwi have some providers for purchase flows, that can be found here. They help in some flows like Google Play, for subscription creation, that is not handled by kiwi sdk directly.

Registering

To register a subscription you need to do the following:

kiwiSDK.subscription().register(new RegisterSubscriptionCommand()
                .withSubscriptionType(SubscriptionType.AUTO_RENEWABLE)
                .withSubscriptionPlatform(SubscriptionPlatform.GOOGLE)
                .withSku("com.example.monthly.1")
                .withTransactionId(token)
                .withReceiptData(receiptData),
                new RegisterSubscriptionListener() {
                    @Override
                    public void onRegisteredSubscription(Subscription subscription) {
                        // the subscription was registered successfully
                    }

                    @Override
                    public void onRestoredSubscription(Subscription subscription) {
                        // the subscription already exists and was restored successfully
                    }

                    @Override
                    public void onProcessingSubscription() {
                        // the subscription could not be verified right now, but will be processed after
                    }

                    @Override
                    public void onInvalidSubscription(Subscription subscription) {
                        // the subscription was verified and recognized as invalid
                    }

                    @Override
                    public void onError() {
                        // an error occurred. You should try again
                    }
                });

NOTE: This operation will be performed on background.

Restoring

The restore process is similar to register. You only need to use kiwiSDK#subscription#restore method instead of register.

Recovering

You must check the subscriptions in two ways:

  • Subscription already registerd on kiwi backend.

  • Subscriptions that were not registered/verified on kiwi backend YET, that are called unregistered.

Query unregistered subscriptions (locally)

    Set<UnregisteredSubscription> unregisteredSubscriptions = kiwiSDK.subscription().getUnregisteredSubscriptions();

Query registered subscriptions (locally)

    Set<Subscription> subscriptions = kiwiSDK.subscription().getLocalSubscriptions();

Query registered subscriptions (remotelly)

    kiwiSDK.subscription().fetchSubscriptions(new FetchSubscriptionsListener() {
                    @Override
                    public void onSuccess(final Set<Subscription> subscriptions) {
                    }

                    @Override
                    public void onError() {
                    }
                });

NOTE: This operation will be performed on background.

Synchronizing the subscription

You can synchronize the subscription with external provider, like Google or SBS. To do this, you must call:

    Set<Subscription> subscriptions = kiwiSDK.getLocalSubscriptions();
    // choose one subscription in subscriptions
    // and call the sync method
    kiwiSDK.subscription().syncSubscription(subscription, new SyncSubscriptionListener() {
                        @Override
                        public void onSuccess(final Subscription subscription) {
                        }

                        @Override
                        public void onNetworkUnavailable() {
                        }

                        @Override
                        public void onError() {
                        }
                    });

Apendix: The Subscription object

The Subscription object is where you can find any details about the existent user's subscription.

Fields:

  • id: the identifier of the subscription

  • sku: string representing the package of the subscription (price, renew period, etc)

  • externalTransactionId: the identifier of the subscription in the external system. i.e. Google Play, SBS, etc.

  • subscriptionPlatform:The platform that owns the subscription. (APPLE, GOOGLE, MOZCA, SBS, MOZCA_FORTUMO, MOZCA_BOKU, SMARTCOIN, UNKNOWN)

  • subscriptionStatus: The current status of the subscription.

    • ACTIVE(1): Kiwi consider a subscription active if it's expirationDate is not reached.

      • OBS: In the expiration day, the subscription will be active the whole day, Kiwi doesn't care about the hour.

    • EXPIRED(2): Kiwi and the external system are considering the subscription expired, it means that the user weren't renewed and it's subscription is not longer valid (and, therefore, not being charged).

      • OBS: Some external systems (i.e. SBS) keep trying to charge the subscription even if it is already expired. In this case, the expirationDate will be postponed (i.e. set to the future), but it's status is kept as EXPIRED . It is up to the app decide if you will allow the user to access premium content or not.

    • CANCELLED(3): The subscription were cancelled, it is no longer valid.

    • TRIAL(4): The user is in promotional period, it is not being charged yet, but the subscription is valid.

    • UNKNOWN(-1): Kiwi wasn't able to detect the status. Perhaps a problematic situation.

  • price: the value that the user is being charged.

  • currencyCode: the currency of the subscription's price.

  • createdAsTrial: if the subscription was in TRIAL state at any point of time, the value of this field will be true.

  • trialDurationInDays: the period of trial.

  • activatedAt: the date when the subscription was activated.

  • expiresAt: the subscription is ACTIVE until this date. After that, the user may be renewed or expired, for example.

  • lastRenewAt: the date when the last renew of the subscription happened.

  • extraData: general informations given by the external system.

Last updated