|
| 1 | +# Google Play Bridge |
| 2 | + |
| 3 | +Package: `@drakkar.software/doubloon-bridge-google` |
| 4 | + |
| 5 | +Handles Google Play Real-Time Developer Notifications (RTDN) delivered via Google Cloud Pub/Sub push subscriptions. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +pnpm add @drakkar.software/doubloon-bridge-google |
| 13 | +``` |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +## Configuration |
| 18 | + |
| 19 | +```ts |
| 20 | +import { GoogleBridge } from '@drakkar.software/doubloon-bridge-google'; |
| 21 | + |
| 22 | +const google = new GoogleBridge({ |
| 23 | + packageName: 'com.example.app', |
| 24 | + serviceAccountKey: process.env.GOOGLE_SERVICE_ACCOUNT_KEY!, // JSON string |
| 25 | + productResolver, |
| 26 | + walletResolver, |
| 27 | +}); |
| 28 | +``` |
| 29 | + |
| 30 | +### `GoogleBridgeConfig` |
| 31 | + |
| 32 | +| Field | Type | Required | Description | |
| 33 | +|---|---|---|---| |
| 34 | +| `packageName` | `string` | ✓ | Android application package name (e.g. `com.example.app`). Validated against incoming RTDNs. | |
| 35 | +| `serviceAccountKey` | `string` | ✓ | Google service account credentials JSON as a string. Must have `androidpublisher` API access for receipt verification calls. Pass `'{}'` in tests. | |
| 36 | +| `productResolver` | `StoreProductResolver` | ✓ | Maps Google subscription IDs / SKUs to on-chain product IDs. See [Product Resolver](#product-resolver). | |
| 37 | +| `walletResolver` | `WalletResolver` | ✓ | Resolves user wallet from a purchase token. See [Wallet Resolver](#wallet-resolver). | |
| 38 | +| `environment` | `'production' \| 'sandbox'` | | Environment override. Defaults to `'production'`. Google's RTDN schema carries no per-event environment signal — use this flag (or a separate bridge instance) for test traffic. | |
| 39 | +| `logger` | `Logger` | | Optional structured logger. | |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## Environment Detection |
| 44 | + |
| 45 | +Google RTDNs do **not** carry a per-event environment field. The bridge uses `config.environment ?? 'production'`. The only automatic environment override is the top-level `testNotification` object — those always resolve to `'sandbox'`. |
| 46 | + |
| 47 | +Recommended practice for isolating sandbox traffic: configure a **separate Pub/Sub topic** for test purchases and point it at a bridge instance with `environment: 'sandbox'`. With per-namespace servers you can do: |
| 48 | + |
| 49 | +```ts |
| 50 | +const server = createNamespacedServer({ |
| 51 | + namespaces: { |
| 52 | + 'app-prod': { bridges: { google: new GoogleBridge({ environment: 'production', ... }) }, mode: 'production', ... }, |
| 53 | + 'app-test': { bridges: { google: new GoogleBridge({ environment: 'sandbox', ... }) }, mode: 'sandbox', ... }, |
| 54 | + }, |
| 55 | + ... |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## Product Resolver |
| 62 | + |
| 63 | +```ts |
| 64 | +interface StoreProductResolver { |
| 65 | + resolveProductId(store: string, storeSku: string): Promise<string | null>; |
| 66 | + resolveStoreSku(store: string, productId: string): Promise<string[]>; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +`store` is `'google'`. For subscription notifications, `storeSku` is `subscriptionNotification.subscriptionId`. For one-time products, it is `oneTimeProductNotification.sku`. Return the 64-char hex on-chain product ID, or `null` (throws `PRODUCT_NOT_MAPPED`). |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## Wallet Resolver |
| 75 | + |
| 76 | +```ts |
| 77 | +interface WalletResolver { |
| 78 | + resolveWallet(store: string, storeUserId: string): Promise<string | null>; |
| 79 | + linkWallet(store: string, storeUserId: string, wallet: string): Promise<void>; |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +`storeUserId` is the Pub/Sub message `purchaseToken`. You must store the mapping between your user's wallet and the purchase token at checkout time (e.g., via `obfuscatedExternalAccountId` in Google's billing API). |
| 84 | + |
| 85 | +Return `null` to throw `WALLET_NOT_LINKED`. Accepted wallet formats: Solana base58 (32–44 chars) or EVM `0x...` (42 chars). |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## Notification Types |
| 90 | + |
| 91 | +### Subscription notifications (`subscriptionNotification`) |
| 92 | + |
| 93 | +| `notificationType` | Normalized type | |
| 94 | +|---|---| |
| 95 | +| 1 — RECOVERED | `billing_recovery` | |
| 96 | +| 2 — RENEWED | `renewal` | |
| 97 | +| 3 — CANCELED | `cancellation` | |
| 98 | +| 4 — PURCHASED | `initial_purchase` | |
| 99 | +| 5 — ON_HOLD | `grace_period_start` | |
| 100 | +| 6 — IN_GRACE_PERIOD | `grace_period_start` | |
| 101 | +| 7 — RESTARTED | `resume` | |
| 102 | +| 8 — PRICE_CHANGE_CONFIRMED | `price_increase_consent` | |
| 103 | +| 9 — DEFERRED | `renewal` | |
| 104 | +| 10 — PAUSED | `pause` | |
| 105 | +| 11 — PAUSE_SCHEDULE_CHANGED | `pause` | |
| 106 | +| 12 — REVOKED | `revocation` | |
| 107 | +| 13 — EXPIRED | `expiration` | |
| 108 | + |
| 109 | +### One-time product notifications (`oneTimeProductNotification`) |
| 110 | + |
| 111 | +| `notificationType` | Normalized type | |
| 112 | +|---|---| |
| 113 | +| 1 — ONE_TIME_PRODUCT_PURCHASED | `initial_purchase` | |
| 114 | +| 2 — ONE_TIME_PRODUCT_CANCELED | `cancellation` | |
| 115 | + |
| 116 | +### Test notifications (`testNotification`) |
| 117 | + |
| 118 | +Always normalized to `'test'`, `instruction: null`, `requiresAcknowledgment: false`. |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +**Mint instruction** produced for: `initial_purchase`, `renewal`, `billing_recovery`, `resume`. |
| 123 | +**Revoke instruction** produced for: `revocation`, `expiration`. |
| 124 | +**No instruction** for: `cancellation`, `grace_period_start`, `billing_retry_start`, `price_increase_consent`, `pause`, `test`. |
| 125 | + |
| 126 | +`requiresAcknowledgment: true` only for `initial_purchase` subscription events. Deadline: 3 days from store timestamp. |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## Google Cloud Pub/Sub Setup |
| 131 | + |
| 132 | +1. In **Google Play Console → Monetization → Real-time developer notifications**, create a Cloud Pub/Sub topic and point it at your endpoint. |
| 133 | +2. Create a **push subscription** on that topic with the URL `https://your-server.com/webhook` (or your namespace path). |
| 134 | +3. The Pub/Sub push delivers a JSON envelope: |
| 135 | + ```json |
| 136 | + { |
| 137 | + "message": { |
| 138 | + "data": "<base64-encoded RTDN JSON>", |
| 139 | + "messageId": "...", |
| 140 | + "publishTime": "..." |
| 141 | + }, |
| 142 | + "subscription": "..." |
| 143 | + } |
| 144 | + ``` |
| 145 | + Your HTTP server must base64-decode `message.data` and pass the result as the request body to the bridge. |
| 146 | +4. Create a **service account** with the `Android Publisher` role and download its JSON key. Pass the JSON string as `serviceAccountKey`. |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +## Error Codes |
| 151 | + |
| 152 | +| Code | Cause | |
| 153 | +|---|---| |
| 154 | +| `INVALID_RECEIPT` | Malformed body, missing `packageName`, or no recognized notification type. | |
| 155 | +| `PRODUCT_NOT_MAPPED` | `productResolver.resolveProductId` returned `null`. | |
| 156 | +| `WALLET_NOT_LINKED` | No wallet resolved, or address format invalid. | |
0 commit comments