$npx -y skills add hanamizuki/solopreneur --skill gplay-purchase-verificationServer-side purchase verification for in-app products and subscriptions using Google Play Developer API. Use when implementing receipt validation in your backend.
| 1 | # Purchase Verification for Google Play |
| 2 | |
| 3 | Use this skill when you need to verify in-app purchases or subscriptions from your backend server. |
| 4 | |
| 5 | ## Why Verify Purchases Server-Side? |
| 6 | |
| 7 | Client-side verification can be bypassed. Always verify purchases on your server: |
| 8 | - Prevent fraud and piracy |
| 9 | - Ensure user actually paid |
| 10 | - Check subscription status |
| 11 | - Handle refunds and cancellations |
| 12 | |
| 13 | ## Authentication Setup |
| 14 | |
| 15 | Your backend needs a service account with permissions to verify purchases. |
| 16 | |
| 17 | ### Create service account |
| 18 | 1. Go to [Google Cloud Console](https://console.cloud.google.com/iam-admin/serviceaccounts) |
| 19 | 2. Create service account |
| 20 | 3. Grant "Service Account User" role |
| 21 | 4. Download JSON key |
| 22 | |
| 23 | ### Grant API access |
| 24 | 1. Go to [Play Console](https://play.google.com/console) |
| 25 | 2. Users & Permissions → Service Accounts |
| 26 | 3. Grant service account access to your apps |
| 27 | |
| 28 | ## Verify In-App Product Purchase |
| 29 | |
| 30 | ### Get purchase details |
| 31 | ```bash |
| 32 | gplay purchases products get \ |
| 33 | --package com.example.app \ |
| 34 | --product-id premium_upgrade \ |
| 35 | --token <PURCHASE_TOKEN> |
| 36 | ``` |
| 37 | |
| 38 | ### Response |
| 39 | ```json |
| 40 | { |
| 41 | "kind": "androidpublisher#productPurchase", |
| 42 | "purchaseTimeMillis": "1706400000000", |
| 43 | "purchaseState": 0, |
| 44 | "consumptionState": 0, |
| 45 | "developerPayload": "user_123", |
| 46 | "orderId": "GPA.1234-5678-9012-34567", |
| 47 | "purchaseType": 0 |
| 48 | } |
| 49 | ``` |
| 50 | |
| 51 | ### Purchase states |
| 52 | - `0` = Purchased |
| 53 | - `1` = Canceled |
| 54 | - `2` = Pending |
| 55 | |
| 56 | ### Consumption states |
| 57 | - `0` = Yet to be consumed |
| 58 | - `1` = Consumed |
| 59 | |
| 60 | ## Acknowledge Purchase |
| 61 | |
| 62 | After verifying, acknowledge the purchase: |
| 63 | |
| 64 | ```bash |
| 65 | gplay purchases products acknowledge \ |
| 66 | --package com.example.app \ |
| 67 | --product-id premium_upgrade \ |
| 68 | --token <PURCHASE_TOKEN> |
| 69 | ``` |
| 70 | |
| 71 | **Important:** Unacknowledged purchases will be refunded after 3 days. |
| 72 | |
| 73 | ## Consume Purchase (for consumables) |
| 74 | |
| 75 | For consumable items (coins, gems, etc.): |
| 76 | |
| 77 | ```bash |
| 78 | gplay purchases products consume \ |
| 79 | --package com.example.app \ |
| 80 | --product-id coins_100 \ |
| 81 | --token <PURCHASE_TOKEN> |
| 82 | ``` |
| 83 | |
| 84 | ## Verify Subscription |
| 85 | |
| 86 | > **Prefer the v2 API.** For new integrations use `gplay purchases subscriptionsv2 get` |
| 87 | > (and `subscriptionsv2 cancel/defer/revoke`, `purchases productsv2 get`). The v2 |
| 88 | > `SubscriptionPurchaseV2` model reflects base plans and offers; the v1 endpoints |
| 89 | > below still work but are the legacy shape. |
| 90 | |
| 91 | ### Get subscription details (v2, recommended) |
| 92 | ```bash |
| 93 | gplay purchases subscriptionsv2 get \ |
| 94 | --package com.example.app \ |
| 95 | --token <SUBSCRIPTION_TOKEN> |
| 96 | ``` |
| 97 | |
| 98 | ### Get subscription details (v1, legacy) |
| 99 | ```bash |
| 100 | gplay purchases subscriptions get \ |
| 101 | --package com.example.app \ |
| 102 | --token <SUBSCRIPTION_TOKEN> |
| 103 | ``` |
| 104 | |
| 105 | ### Response |
| 106 | ```json |
| 107 | { |
| 108 | "kind": "androidpublisher#subscriptionPurchase", |
| 109 | "startTimeMillis": "1706400000000", |
| 110 | "expiryTimeMillis": "1709000000000", |
| 111 | "autoRenewing": true, |
| 112 | "priceCurrencyCode": "USD", |
| 113 | "priceAmountMicros": "4990000", |
| 114 | "paymentState": 1, |
| 115 | "cancelReason": null, |
| 116 | "userCancellationTimeMillis": null, |
| 117 | "orderId": "GPA.1234-5678-9012-34567", |
| 118 | "linkedPurchaseToken": null, |
| 119 | "subscriptionState": 0 |
| 120 | } |
| 121 | ``` |
| 122 | |
| 123 | ### Subscription states |
| 124 | - `0` = Active |
| 125 | - `1` = Canceled (still valid until expiry) |
| 126 | - `2` = In grace period |
| 127 | - `3` = On hold (payment failed, retrying) |
| 128 | - `4` = Paused |
| 129 | - `5` = Expired |
| 130 | |
| 131 | ### Payment states |
| 132 | - `0` = Payment pending |
| 133 | - `1` = Payment received |
| 134 | - `2` = Free trial |
| 135 | - `3` = Pending deferred upgrade/downgrade |
| 136 | |
| 137 | ## Backend Implementation Example |
| 138 | |
| 139 | ### Node.js/Express |
| 140 | ```javascript |
| 141 | const { google } = require('googleapis'); |
| 142 | |
| 143 | async function verifyPurchase(packageName, productId, token) { |
| 144 | const auth = new google.auth.GoogleAuth({ |
| 145 | keyFile: '/path/to/service-account.json', |
| 146 | scopes: ['https://www.googleapis.com/auth/androidpublisher'], |
| 147 | }); |
| 148 | |
| 149 | const androidpublisher = google.androidpublisher({ |
| 150 | version: 'v3', |
| 151 | auth: await auth.getClient(), |
| 152 | }); |
| 153 | |
| 154 | const result = await androidpublisher.purchases.products.get({ |
| 155 | packageName: packageName, |
| 156 | productId: productId, |
| 157 | token: token, |
| 158 | }); |
| 159 | |
| 160 | return result.data; |
| 161 | } |
| 162 | |
| 163 | // Endpoint |
| 164 | app.post('/verify-purchase', async (req, res) => { |
| 165 | const { packageName, productId, token } = req.body; |
| 166 | |
| 167 | try { |
| 168 | const purchase = await verifyPurchase(packageName, productId, token); |
| 169 | |
| 170 | if (purchase.purchaseState === 0) { |
| 171 | // Purchase is valid |
| 172 | // Grant access to user |
| 173 | // Acknowledge purchase |
| 174 | res.json({ valid: true, purchase }); |
| 175 | } else { |
| 176 | res.json({ valid: false }); |
| 177 | } |
| 178 | } catch (error) { |
| 179 | res.status(400).json({ error: error.message }); |
| 180 | } |
| 181 | }); |
| 182 | ``` |
| 183 | |
| 184 | ### Python/Flask |
| 185 | ```python |
| 186 | from google.oauth2 import service_account |
| 187 | from googleapiclient.discovery import build |
| 188 | |
| 189 | SCOPES = ['https://www.googleapis.com/auth/androidpublisher'] |
| 190 | SERVICE_ACCOUNT_FILE = '/path/to/service-account.json' |
| 191 | |
| 192 | credentials = service_account.Credentials.from_service_account_file( |
| 193 | SERVICE_ACCOUNT_FILE, scopes=SCO |