简体   繁体   中英

How to programmatically know when a user cancels an in-app subscription?

I have implemented in-app purchase in my app but how to known users cancel subscription programmatically.

I want to call API when a user cancels the subscription.

You should use the Subscriptions and In-App purchases status API, which is a part of Google Play Developer API v2.0.

It allows you to track all the in-app purchases and subscriptions by different users.

A simple request would let you know about the status of the user's subscription.

I run the following code every time the app is launched.

        billingClient.queryPurchases( BillingClient.SkuType.SUBS );

    Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases( BillingClient.SkuType.SUBS );
    if (purchasesResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
        ArrayList<String> skus = new ArrayList<>( Arrays.asList( getResources().getStringArray( R.array.array_subs_sku ) ) );
        for (String sku : skus) {
            setFlag( mContext, sku + KEY_SKU_PURCHASED, false );
        }

        for (Purchase purchase : purchasesResult.getPurchasesList()) {

            setFlag( mContext, purchase.getSku() + KEY_SKU_PURCHASED, true );
            setFlag( mContext, FLAG_SUBSCRIBED, true );
            getIsSubscribed(); // check here if subscription status has changed
            //Log.d( TAG, "onPurchaseHistoryResponse: SUBS" + purchase.getSku() + " / " + getDateStampString( String.valueOf( purchase.getPurchaseTime() / 1000 ) ) + " / State : " + purchase.getPurchaseState() );
        }
    }

let me know if you need more help with this. But it should be pretty straight forward. My code has some of my own functions and variables, but it will give you the idea of how to achieve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM