Skip to main content

Android Billing API

Overview

Google Play Developer API

Android アプリでサブスクリプションやアプリ内課金の状態をサーバーから取得・検証する場合は Google Play Developer API を利用する。

この API を利用することで以下のような情報を取得できる。

  • サブスクリプションの契約状態
  • 有効期限
  • 自動更新状態
  • 支払い状態
  • キャンセル状態

Android では ユーザー一覧を取得する API は存在せずpurchaseToken を基準に個別のサブスクリプション状態を取得する設計になっている。

purchaseToken

Google Play Developer API

Subscription Status

そのため実際のシステムでは以下のような構成になることが多い。

Google Play Billing

purchaseToken

Backend Server

Google Play Developer API

Authentication

Google Play Developer API は OAuth2 + Service Account を利用して認証を行う。

フローは次の通り。

Service Account

JWT生成

OAuth Token取得

access_token

Google Play Developer API

1. JWT を生成

Service Account の秘密鍵を使用して JWT を生成する。

Payload 例

{
"iss": "SERVICE_ACCOUNT_EMAIL",
"scope": "https://www.googleapis.com/auth/androidpublisher",
"aud": "https://oauth2.googleapis.com/token",
"exp": now + 3600,
"iat": now
}

署名アルゴリズム

RS256

2. OAuth Token を取得

エンドポイント

POST https://oauth2.googleapis.com/token

Body

grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer
assertion=<JWT>

レスポンス

{
"access_token": "...",
"expires_in": 3600,
"token_type": "Bearer"
}

Subscription API

サブスクリプション状態を取得する API。

GET
https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{packageName}/purchases/subscriptions/{subscriptionId}/tokens/{purchaseToken}

Header

Authorization: Bearer ACCESS_TOKEN

Response Example

{
"startTimeMillis": "1670000000000",
"expiryTimeMillis": "1672592000000",
"autoRenewing": true,
"priceCurrencyCode": "JPY",
"paymentState": 1
}

主要フィールド

FieldDescription
startTimeMillisサブスクリプション開始日時
expiryTimeMillis有効期限
autoRenewing自動更新状態
paymentState支払い状態

Payment State

ValueStatus
0Pending
1Purchased
2Free Trial
3Pending Upgrade

Important Notes

purchaseToken の保存

Android ではサブスクリプションを識別するために purchaseToken を保存する必要がある。

purchaseToken

このトークンを使用して API からサブスクリプション状態を取得する。


ユーザー一覧 API は存在しない

Google Play Developer API では 契約ユーザー一覧を取得する API は提供されていない。

そのため以下のような設計が一般的である。

Google Play Billing

purchaseToken

Database

Subscription Status API

References

Google Play Developer API

Google Play Developer API

Subscriptions API

Subscriptions API

Service Account OAuth

Service Account OAuth