Verify Google Play In-App Purchases with Cloud Functions
My notes about how to verify Google Play in-app purchase (subscription) with Google Cloud Functions.
Validating in-app purchases is crucial for any app that facilitates purchases and subscriptions.
It serves as a way to ensure the legitimacy of purchase tokens and transaction records. This validation process can be applied to newly generated transactions or used to confirm the ongoing validity of a subscription.
Create Service Account
To verify the purchase, we need to communicate with Google Play Developer API.
For using this API we need to create a service account. It is for use programmatically.
For creating a service account, you can check this guide.
After creating the service account, there will be an email address like xxx@yyy.iam.gserviceaccount.com.
Then find the Google Play Android Developer API and enable it. Don’t forget to give related permissions to use this API to your service account.
Go to your Google Play console and open the “Users and Permissions” page. Then add this email to your account with proper permissions.
Please select these permissions while adding the account;
Don’t forget to add your app to your related email address.
Now you are available to use Google Play Developer API.
After that create a Firebase cloud function.
This function can verify the token that is related to your purchase. You can check your subscriptions with an hourly or daily cron.
You need to specify your service account key and scope to androidpublisher API.
If you are already using Firebase cloud functions you can directly add this file to your directory and run this command for deploying;
firebase deploy - only functions;
Insufficient permissions — Google Play Developer API
If you get an error about Insufficient permissions please check;
- The service account has related permissions
- Service account added to Google Play Console with correct permissions
- The service account has been added to the correct application in Google Play Console
If everything is fine, go to an item that you are selling and update the description or title. Check out this post.
Atakan Demircioğlu is a Full Stack Developer currently working at Jotform, a leading online form-builder platform.
He is passionate about blogging, learning, and creating. He shares his experiences on Medium and GitHub.
Twitter: https://twitter.com/atakde
Github: https://github.com/atakde