验证购买

验证 App Store 或 Google Play 交易凭证,写入 Purchase 记录并发放 Entitlement。通常由 SDK 在客户端购买完成后调用,也可由服务端直接调用。

POST/v1/purchases/verify

参数

参数类型说明
Authorization必填stringBearer {secret_key} — 服务端 Secret Key。
app_id必填string应用 ID,如 app_01KFOCUSIOS。
member_id必填string成员 ID,由 SDK configure 后分配。
product_id必填string平台 Product ID,非商店 SKU。
provider必填stringapple 或 google。
receipt必填stringStoreKit 2 signed transaction 或 Google Play purchase token。

请求示例

HTTP
POST /v1/purchases/verify HTTP/1.1
Host: api.subhub.dev
Authorization: Bearer sk_live_neostudio_8f3k2m9x4l0p7q2r5s8u1v
Content-Type: application/json

{
  "app_id": "app_01KFOCUSIOS",
  "member_id": "mem_001",
  "product_id": "prod_01PROMO",
  "provider": "apple",
  "receipt": "eyJhbGciOiJFUzI1NiIsIng1YyI6W10..."
}

响应示例

HTTP
HTTP/1.1 200 OK
Content-Type: application/json

{
  "id": "pur_001",
  "member_id": "mem_001",
  "product_id": "prod_01PROMO",
  "provider": "apple",
  "status": "completed",
  "amount": 18,
  "currency": "CNY",
  "provider_transaction_id": "2000000123456789",
  "entitlements": [
    {
      "identifier": "premium",
      "is_active": true,
      "expires_at": "2026-07-27T18:32:00.000Z"
    }
  ],
  "created_at": "2026-06-27T18:32:00.000Z"
}

代码示例

import SubHub

// SDK 内部调用 — 开发者通常使用 purchase() 而非直接调 REST
let result = try await SubHub.purchase(productId: "com.neo.focustimer.promo")

// 服务端验证(Swift on Server / Vapor)
var request = URLRequest(url: URL(string: "https://api.subhub.dev/v1/purchases/verify")!)
request.httpMethod = "POST"
request.setValue("Bearer \(secretKey)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONEncoder().encode([
    "app_id": "app_01KFOCUSIOS",
    "member_id": "mem_001",
    "product_id": "prod_01PROMO",
    "provider": "apple",
    "receipt": signedTransaction
])