I wanted to share some steps on how to set up Google Play In-App Purchases using Google_Service_AndroidPublisher. Here’s a brief walkthrough:
First, you'll need to use the following code snippet:
use Google_Service_AndroidPublisher;
$client = new Google_Client();
$client->setAuthConfig(resource_path('path_to_your_key.json')); // Use the credentials you used for your purchase setup
$client->setScopes('https://www.googleapis.com/auth/androidpublisher');
$packageName = 'com.bakadur.app';
$purchaseToken = $request->google_token; // Make sure you handle validations here
$productId = $request->premium_type; // And here as well
$service = new Google_Service_AndroidPublisher($client);
$response = $service->purchases_products->get($packageName, $productId, $purchaseToken);
In case you encounter the following error:
{
"error": {
"code": 401,
"message": "The current user has insufficient permissions to perform the requested operation.",
"errors": [
{
"message": "The current user has insufficient permissions to perform the requested operation.",
"domain": "androidpublisher",
"reason": "permissionDenied"
}
]
}
}
Here’s what you need to do to resolve it:
- Enable the API:
- Go to the Google Play Android Developer API page in Google Cloud Console and click on "Enable".
- Create a Service Account:
- Navigate to IAM & Admin in the Google Cloud Console and create a service account.
- Fill in the service account details (name the project as you like).
- Grant the service account access to the project with the following roles:
- Pub/Sub Admin (enables Platform Server Notifications)
- Monitoring Viewer (allows monitoring of the notification queue)
- Create and Download the Public Key:
- In the 'Service Accounts' section, select the three dots in the Actions dropdown menu, then Manage Keys.
- Add a new key, select JSON, then create and download the JSON key.
- Grant Access in Google Play Console:
- Go to Users and Permissions in the Google Play Console and invite the service account you created.
- Grant the following permissions:
- View app information and download bulk reports (read-only)
- View financial data, orders, and cancellation survey responses
- Manage orders and subscriptions
Once you have completed these steps, try running the code again. The error should disappear.
Good luck with your setup!