Getting purchase token for a paid app from Google Play

2k Views Asked by At

We need to associate information about the app purchase with the user on our servers. For in-app products, this is easy to do because we can receive the purchase token.

How to achieve the same functionality for a paid app downloaded directly from Google Play? We cannot find a way to receive an order id or purchase token to record on our servers.

1

There are 1 best solutions below

1
COYG On

I came across this and your question while researching a problem of my own. I think this might be what your looking for:

Developer payload is supported

2.0 of the Google Play Billing library adds support for developer payload—arbitrary strings that can be attached to purchases. You can attach a developer payload parameter to a purchase, but only when the purchase is acknowledged or consumed. This is unlike developer payload in AIDL, where the payload could be specified when launching the purchase flow. Because purchases can now be initiated from outside of your app, this change ensures that you always have an opportunity to add a payload to purchases.

To access the payload in the new library, Purchase objects now include a getDeveloperPayload() method.

Note: You cannot modify a payload after it is assigned.

And here is some sample code:

Consumable

BillingClient client = ...
ConsumeResponseListener listener = ...

ConsumeParams consumeParams =
    ConsumeParams.newBuilder()
        .setPurchaseToken(/* token */)
        .setDeveloperPayload(/* payload */)
        .build();

client.consumeAsync(consumeParams, listener);

Non-consumable

BillingClient client = ...
AcknowledgePurchaseResponseListener listener = ...

AcknowledgePurchaseParams acknowledgePurchaseParams =
    AcknowledgePurchaseParams.newBuilder()
        .setPurchaseToken(/* token */)
        .setDeveloperPayload(/* payload */)
        .build();

client.acknowledgePurchase(acknowledgePurchaseParams, listener)