Search code examples
google-playgoogle-play-developer-apigoogle-play-billing

How can I use a Google subscription linkedPurchaseToken without the productId/sku?


Problem

Given a linkedPurchaseToken, I would like to get the associated subscription info from Google. But the Google Get Subscription API requires the product id (i.e. sku) of the subscription - which will probably have changed since new purchase tokens are only created when the customer purchases a new product. If you use the old sku in the url with the new purchase token, you'll get a purchaseTokenDoesNotMatchProductId error back: "The purchase token does not match the product ID." So is there actually a way to use just the linked purchase token?

More Details

In a Medium article linked from the Google docs, linked purchase tokens are explained in depth, though it only discusses managing them on your server, not using them to validate any info. The Google docs about upgrades/downgrades say "You can use the linkedPurchaseToken to look up the old subscription and identify the existing user account so that you can associate the new purchase with the same account." But as someone else asked in a response to the Medium article which was not answered, you need the sku in order to make the GET request.

So it seems that only way to successfully make this request is to have the sku recorded on your server. I'm already planning on doing this, but my concern is what to do in the case where something goes wrong and this info isn't there.

Admittedly Contrived Example

Suppose a customer upgrades their subscription during a server downtime, sees an error, and upgrades it again to a different sku. There would now be a linked purchase token chain like this: Upgrade 2 -> Upgrade 1 -> Original Sub. Suppose the server is down for the first RTDN, but is back up in time for the second one. Only the Original Sub is in the DB, so nothing will match the linkedPurchaseToken on Upgrade 2. We'll then be stuck - we can't call the API to get the linkedPurchaseToken for Upgrade 1 because we don't know the sku, so we can never complete the chain.

This scenario is unlikely but not impossible, and there could be another scenario where we'd end up in a similar state that I'm not thinking of right now. So I'm really looking to understand if I'm missing something basic about how the linkedPurchaseTokens can be used with the API, since the docs don't actually cover these details.


Solution

  • It's been years but you can now use a new API endpoint called subscriptionsv2 which doesn't require knowing the subscription/product ID anymore!

    https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.subscriptionsv2

    GET https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{packageName}/purchases/subscriptionsv2/tokens/{token}

    Parameters
    
    packageName: string
    The package of the application for which this subscription was purchased (for example, 'com.some.thing').
    
    token: string
    Required. The token provided to the user's device when the subscription was purchased.