0

I am building an Android App, using Firebase as Backend. Firebase will be used for authentication, storage, some data in firebase database, and also using cloud functions.

There are subsriptions in my app with Google Play Billing Library v5.

After the subscription is purchased and acknowledged, I'd like to do some server side verification, where my problems begin. I have created cloud function - thank you guys - that listens to pub/sub topic and to verify the subscription I call the google api - Method: purchases.subscriptionsv2.get . After the successful response, I'd like to write this data to my database. Response I get is ok in the form of JSON object like here

This is the function:

exports.insertFromPubSubTopic = functions.pubsub.topic('mytopic').onPublish(async(message, context) => {
if (message.json.subscriptionNotification) {
    console.log("ok - we have message.json.subscriptionNotification")
    try {
        await authClient.authorize();
        const subscription = await playDeveloperApiClient.purchases.subscriptionsv2.get({
            packageName: data.packageName,
            token: data.subscriptionNotification.purchaseToken
        });

        if (subscription.status === 200) {
            // Subscription response is successful.
            console.log("subscription.status===200")
            var message = {
                'data': data
            }
            return firestore.collection('billing-verified').add(message)
        }
    } catch (error) {
          // Logging error for debugging
        console.log(error)
    }
    return {
        status: 500,
        message: "Failed to verify subscription, Try again!"
    }
} else {
    console.log('Test event... logging test');
    //return admin.firestore().collection('mp-billing-messages-error').add(data)
    return firestore.collection('mp-billing-messages-error').add(data)
}
})

My question is: How do I get user's data from successfully returned subscription object, so that I can store in firestore database something like - this purchase was made by this user.

I know Firebase and GooglePlayBilling are not related and have different purposes and functionalities. But let's say I have anonymous user who did not login in my app. So I don't know anything about her and cannot identify her in firebase so far. And then she gets and buys the subscription. Does anyone have any suggestions of how do I get the data about this user from verified purchased object?? Any suggestion would be helpful.

Gregor Sotošek
  • 357
  • 1
  • 4
  • 22
  • You can use [RevenueCat](https://firebase.google.com/products/extensions/revenuecat-firestore-revenuecat-purchases). Install this extension to use Firebase services as your RevenueCat backend for in-app purchases on Apple App Store, Google Play Store, and Amazon Appstore. This extension can makes in-app purchases and subscriptions, Store purchase lifecycle events (e.g., trial starts, purchases, subscription renewals) in Firestore and react to them. Store and update information about customers and their purchases in Firestore. – Priyashree Bhadra Jul 08 '22 at 08:16
  • Also, have a look at this [thread](https://stackoverflow.com/a/52220147/15803365). Thank me later! – Priyashree Bhadra Jul 08 '22 at 08:17
  • Thank you for your answer. I've checked suggested solutions, but still have the same problem. How do I identify the not logged in user, who makes the purchase via GooglePlayBilling system? I know how to verify the purchase (in terms of if there was purchase with same token in the past). However - I need to link user to this purchase. And I don't know how to get any user related data in this purchase returned object from " . . . android-publisher/api-ref/rest/v3/purchases.subscriptionsv2". I need to know (and store in FirebaseDB) the identity of user who made a purchase. – Gregor Sotošek Jul 08 '22 at 10:39
  • I think this [link](https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.subscriptions) provides the details of the JSON representation of purchases.subscriptions. It includes fields like profileName, givenName, emailAddress, familyName, profileId but [this](https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.subscriptionsv2) does not. Can you try doing a get on purchases.subscriptions and not purchases.subscriptionsv2? – Priyashree Bhadra Jul 11 '22 at 14:09
  • Thany you, I've tried your solution with purchases.subscription. However, in response I am not getting all the fields in the docs. At this point I am thinking of a solution that all the users that would want to purchase in app subscription should be registered, and then I can send users alongside token to googleplaybilling for verification and manage subscription data in firestore database. If someone can suggest better solution . . as for how can I monitor subscription data for (initialyi) non logged in users, would be great. – Gregor Sotošek Jul 14 '22 at 08:51

0 Answers0