购买后收据中没有免费试用的 StoreKit 订阅(tx id 减 1)

StoreKit Subscription with free trial not present in receipt after purchase (tx id off by 1)

我在我的应用程序中提供订阅,购买后我根据收据验证该交易以获得其到期日期。我通过从磁盘加载收据,将其发送给 Apple 进行验证/解密(我知道这是不好的做法),然后遍历 JSON 响应中的 latest_receipt_info 来寻找交易匹配来做到这一点SKPaymentTransction 对象的 transactionIdentifier

这适用于大多数订阅,但不适用于提供免费试用/推介优惠的订阅。请看下面我的例子:

  1. 我购买的订阅返回到我的应用程序,transactionIdentifier1000000506350685originalTransaction.transactionIdentifiernull
  2. 我从 Apple 收到最新的 JSON 收据,其最新条目与我的产品标识符和时间戳相匹配,但 transaction_id1000000506350686original_transaction_id 也是.

因此我无法验证购买。

在没有免费试用/推介优惠的情况下购买订阅没有问题。返回的收据中没有其他对象与返回到我的应用程序的交易 ID 相匹配。

这是在沙箱环境中,我不确定生产中的行为。

有没有人有过类似的经历?我该怎么做才能解决这个问题?

我认为这可能是因为您之前已经在 Sandbox 中购买了该产品,并且在您购买之前在 Sandbox 中没有设备收据(与生产不同)。

无论哪种方式,您应该查看收据中的最新 product_idexpiration_date 以验证订阅是否有效。如果您将交易 ID 保存在数据库中,交易 ID 可以作为交易的关键,但不应用于验证购买。

这是一个非常好的博客 post,它介绍了您的应用内购买服务器应该如何验证和保持订阅状态最新:iOS Subscriptions are Hard

I do this by loading the receipt from disk, sending it to Apple for validation/ decryption (I know this is bad practice).

这实际上是 Apple 的 recommended approach