实现应用内购买与订阅权益管理
围绕商店支付设计客户端与服务端实现,交付交易校验、订阅状态同步及沙盒测试方案。
完整提示词
<context>
The user sells digital goods inside an app on {{platform}}. Store billing has rules a web payments engineer does not expect: digital content consumed in the app generally must use store billing; transactions must be finished (StoreKit) or acknowledged within three days (Google Play) or they are refunded; unlocking features from the client alone is trivially bypassed; a subscription's state changes outside the app (renewals, billing retry, grace period, refunds, revocation, upgrades and downgrades, family sharing) and only arrives through server notifications; and users expect "Restore purchases" to work on a new device. Experts store entitlements on their server, keyed to their own user id, driven by verified store data, and treat the client as a cache.
</context>
<task>
<products>
{{products}}
</products>
1. If it is unclear what each product unlocks, whether users have accounts, or whether there is a backend, ask and stop. Note that a serverless app can rely on on-device verification (StoreKit 2 signed transactions) with stated weaker guarantees.
2. Design the product catalogue: product ids that never get reused, type, subscription groups and levels (iOS) or base plans and offers (Google Play), trials and intro offers, and how prices are shown from store data, never hard-coded.
3. Design the entitlement model: a server table of user, entitlement, source (store, original transaction id or purchase token), status, expiry, and the rule that maps store state to access, including grace period and billing retry.
4. Write the purchase flow: load products, show localized price, buy with an account token (appAccountToken or obfuscatedAccountId) linking to your user, handle pending (Ask to Buy, deferred payment), cancellation and errors, send the signed transaction or purchase token to the server, grant access only after the server confirms, then finish or acknowledge.
5. Write server validation: verify App Store signed transactions (JWS) and use the App Store Server API; verify Google Play purchases with the Play Developer API; reject reused or mismatched tokens; idempotent processing keyed on transaction id.
6. Handle lifecycle events from App Store Server Notifications V2 and Google Play Real-time Developer Notifications: renewal, failed renewal, grace period, expiry, refund and revocation, upgrade and downgrade, pause (Android), with an event-to-state table. Reconcile periodically in case notifications are missed.
7. Add restore purchases and cross-device access through the user's account.
8. Write a sandbox test plan: StoreKit configuration file and sandbox accounts, Play license testers and test cards, accelerated renewal, refunds, interrupted purchases, and app killed mid-purchase.
</task>
<constraints>
- Never grant paid access from client-side state alone when a backend exists.
- Do not state commission rates, pricing tiers or store policy details as current fact; say to check the latest App Store Review Guidelines and Google Play policies, including rules on external payment links, which vary by country.
- Do not invent API endpoints or SDK methods; state the StoreKit and Play Billing Library versions assumed.
{{> output/uncertainty}}
</constraints>
<output_format>
## Assumptions
Bullets.
## Product catalogue
Table: Product id | Type | Store config | Unlocks.
## Entitlement model
Schema and the access rule.
## Purchase flow
Numbered steps for the client and server.
## Server validation
Code and the checks performed.
## Lifecycle events
Table: Store event (iOS / Android) | New state | Access | Action.
## Code
Client files with names.
## Sandbox test plan
Table: Scenario | How to simulate | Expected.
</output_format>可替换内容
- {{platform}}
- {{products}}
- {{> output/uncertainty}}
分类与标签
编程开发、应用内购买、订阅管理、交易校验、恢复购买、沙盒测试