avatar

Niki Tech

受託開発のほかにも、独自に開発しているアプリもあります🚀

🛠Acknowledging Google Play In-App Purchases using Node.js (MJS)

2025-04-16

✅ Acknowledging Google Play In-App Purchases using Node.js (MJS)

This guide walks you through acknowledging a non-consumable in-app purchase on Google Play using a Service Account and Node.js (ESM format).


📌 Prerequisites

  • A published app on Google Play Console
  • Google Cloud Project linked to Google Play Console
  • Node.js (v16+ recommended)
  • google-auth-library and node-fetch installed
npm install google-auth-library node-fetch

🔐 1. Create and Configure Service Account in Google Cloud Console

1.1 Create Service Account in Google Cloud Console

Go to Google Cloud Console

Select correct the project

Click Create Service Account

In IAM select edit service account and edit Role:

Service Accounts/Service Account User

1.2 Download Credentials

After creating the account, click Create Key

Choose JSON → Download and save as credentials.json

🛠️ 2. Add Service Account to Play Console

In User&Permission add new user from Servive Account in step 1

Paste your service account email (e.g., serviceaccountgoogleplay@yourproject.iam.gserviceaccount.com)

Click Manage Access (アクセス権を管理) and assign:

✅ Manage orders and subscriptions (注文と定期購入の管理)

✅ View financial data (売上データの閲覧)

✅ Release (リリース)

✅ Apply to your target app (or all apps)

🧪 3. Code: Acknowledge Purchase using Node.js (ESM)

Create a file: acknowledge.mjs

import fetch from 'node-fetch'; import { GoogleAuth } from 'google-auth-library'; // 🔧 Replace these values: const PACKAGE_NAME = 'com.example.myapp'; const PRODUCT_ID = 'premium_upgrade'; const PURCHASE_TOKEN = 'your_real_purchase_token_here'; const SCOPES = ['https://www.googleapis.com/auth/androidpublisher']; async function getAccessToken() { const auth = new GoogleAuth({ keyFile: './credentials.json', scopes: SCOPES, }); const client = await auth.getClient(); const tokenResponse = await client.getAccessToken(); return tokenResponse.token; } async function acknowledgePurchase() { const accessToken = await getAccessToken(); const url = `https://androidpublisher.googleapis.com/androidpublisher/v3/applications/${PACKAGE_NAME}/purchases/products/${PRODUCT_ID}/tokens/${PURCHASE_TOKEN}:acknowledge`; const response = await fetch(url, { method: 'POST', headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ developerPayload: 'acknowledged via Node.js', }), }); if (response.ok) { console.log('✅ Purchase acknowledged successfully.'); } else { const error = await response.json(); console.error('❌ Failed to acknowledge purchase:', error); } } acknowledgePurchase().catch(console.error);