You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

StoreKit2中IAP续订后始终出现两条交易记录的原因?

问题

我开发的一款App包含一项订阅内购,购买及续订功能看似正常。但在后台监听IAP交易时,通过调试打印发现每次续订后都会生成两条几乎完全相同的交易记录。

用于监听后台交易的代码如下:

private func observeTransactionUpdates() -> Task<Void, Never> {
    Task(priority: .background) { [unowned self] in
        for await verificationResult in Transaction.updates {
            await self.updatePurchasedProducts()
            if case .verified(let transaction) = verificationResult {
                print("Verified")
                print(transaction)
                await transaction.finish() // Ensure finishing all transactions
            } else if case .unverified(let transaction, _) = verificationResult {
                print("Unverified")
                print(transaction)
                await transaction.finish() // Even finish unverified transactions
            }
        }
    }
}

沙箱环境下续订后每3分钟的调试输出如下:

Verified
{
  "bundleId" : "...app",
  "currency" : "EUR",
  "deviceVerification" : "...qZZf",
  "deviceVerificationNonce" : "...df9",
  "environment" : "Sandbox",
  "expiresDate" : 1718878102000,
  "inAppOwnershipType" : "PURCHASED",
  "originalPurchaseDate" : 1718536890000,
  "originalTransactionId" : "...1810",
  "price" : 2990,
  "productId" : "....premium.monthly",
  "purchaseDate" : 1718877922000,
  "quantity" : 1,
  "signedDate" : 1718877887210,
  "storefront" : "DEU",
  "storefrontId" : "...443",
  "subscriptionGroupIdentifier" : "...060",
  "transactionId" : "...5774",
  "transactionReason" : "RENEWAL",
  "type" : "Auto-Renewable Subscription",
  "webOrderLineItemId" : "...9876"
}

Verified
{
  "bundleId" : "...app",
  "currency" : "EUR",
  "deviceVerification" : "...ocx",
  "deviceVerificationNonce" : "...3bf",
  "environment" : "Sandbox",
  "expiresDate" : 1718878102000,
  "inAppOwnershipType" : "PURCHASED",
  "originalPurchaseDate" : 1718536890000,
  "originalTransactionId" : "...1810",
  "price" : 2990,
  "productId" : "....premium.monthly",
  "purchaseDate" : 1718877922000,
  "quantity" : 1,
  "signedDate" : 1718877888930,
  "storefront" : "DEU",
  "storefrontId" : "...443",
  "subscriptionGroupIdentifier" : "...060",
  "transactionId" : "...5774",
  "transactionReason" : "RENEWAL",
  "type" : "Auto-Renewable Subscription",
  "webOrderLineItemId" : "...9876"
}

原本预期续订后后台只会出现一条带有有效到期日的交易记录,而非两条状态为RENEWAL且到期日相同的交易记录,请问这是什么原因?

原因分析
  • 沙箱环境专属测试机制:苹果沙箱为便于开发者验证订阅续订逻辑,会主动重复推送同一笔续订交易的通知,这是沙箱独有的行为,生产环境不会出现该问题。
  • 重复签名包推送:两条记录的核心交易信息(transactionId、expiresDate、transactionReason等)完全一致,仅signedDate、deviceVerification等签名相关字段不同,说明是苹果服务器两次生成的签名包,本质对应同一笔续订交易。
  • 交易监听的特性:Transaction.updates会接收所有来自App Store的交易通知,包括重复推送的通知。沙箱的这种重复推送,是为了验证开发者是否能正确处理重复交易,避免重复给用户开通权益。

内容的提问来源于stack exchange,提问作者Uli68

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 03:37:03