GET /billing/plans and /packs then report store.apple.enabled or store.google.enabled as true. Merging the code changes nothing for users while the secrets are unset, and the store routes answer 503 store_billing_not_configured.
Environment variables
Set these on the production Cloud API (api.allternit.com) host.
Apple turns on with the first three. Google turns on with the service account JSON and the push token.
APPLE_BUNDLE_ID, GOOGLE_PLAY_PACKAGE_NAME and STORE_SANDBOX_GRANT_USER_IDS do not switch anything on.
Apple
- In App Store Connect create the seven products from the product table. The three plans are auto-renewing subscriptions in one subscription group so upgrades and downgrades replace each other. The four packs are consumables.
- In App Store Connect, App Information, App Store Server Notifications, set both URLs to version 2 notifications:
- Production:
https://api.allternit.com/api/v1/billing/store/apple/notifications - Sandbox:
https://api.allternit.com/api/v1/billing/store/apple/notifications/sandbox
- Production:
- Set the three
APPLE_IAP_*variables. - Check: buy a product in sandbox as a listed account and confirm
granted: truefromPOST /billing/store/apple/transactions.
- In Play Console create the three subscriptions and four in-app products with the ids in the product table.
- In Google Cloud create a service account, download its JSON key, and invite its email in Play Console, Users and permissions.
-
In Play Console, Monetization setup, enable real-time developer notifications on a Pub/Sub topic. Create a push subscription on that topic with this endpoint, using the token you chose:
https://api.allternit.com/api/v1/billing/store/google/rtdn?token=THE_TOKEN -
Set
GOOGLE_PLAY_SERVICE_ACCOUNT_JSONandGOOGLE_PLAY_RTDN_TOKEN. -
Check: send a test notification from Play Console. The route answers
{ "received": true, "kind": "test" }.
POST /billing/store/google/purchases for a token claims it for that account, and a second account claiming it gets 409 owned_by_another_account. A notification for a purchase nobody has claimed yet is stored as unclaimed. The Play app posts every purchase it finds on each launch, which claims leftovers. Google refunds a purchase that is not acknowledged within 3 days, so nobody pays for nothing. The server acknowledges (and consumes packs) only after the grant.
Migration 148
cmd/allternit-cloud-api/migrations_pg/148_store_billing.sql is additive, so old code keeps running on it:
billing_subscriptionsgainsprovider(defaultstripe),store_product_id,store_environment,store_checked_atandstore_purchase_token.store_purchase_tokenholds the Google purchase token on Google rows only, because the Play Developer API reads a subscription by its token and the row key holds only a hash. Apple and Stripe rows leave it empty. The token is useless without this server’s service account.billing_store_accounts: one account token (UUID) per person.billing_store_transactions: the ledger, one row per store transaction. It is the idempotency key and the refund reference. It has no foreign key to users, so an erased account keeps its money trail.
deploy-contabo.sh and ops_applied_migrations; see Cloud database migrations.
Day to day
- Missed notifications heal.
GET /billing/subscriptionre-reads a store plan from the store when its period end has passed, at most once per 10 minutes per plan. There is no scheduled job. The call waits for the store, up to 15 seconds. - Refunds. An Apple refund or revoke, or a Google voided purchase, takes back the credits granted by that transaction once and drops a refunded plan to the default tier. The balance can go negative. A reversal gives the credits back once. A refund recorded for a transaction that never granted anything takes nothing.
- Erased accounts keep paying the store. Erasing an account closes it in Allternit, but Apple and Google keep charging the person until they cancel in App Store or Play subscription settings. Allternit cannot cancel a store subscription. Tell the person to cancel there. Later store notifications for that account answer
account_closedand grant nothing. - Double billing. If an account ends up with a Stripe plan and a store plan, both are recorded,
conflictis true and the team gets an alert. The person cancels one in its own billing page. - Sandbox. Sandbox and test purchases arrive on the production server. Only accounts in
STORE_SANDBOX_GRANT_USER_IDSget credits.