如何检查订阅是否已被取消?

How to check if a subscription has been cancelled?

我已申请退还我自己应用的月度订阅费用。苹果给了我退款。但我的订阅仍然有效,我可以使用高级功能。

我的应用程序每次使用时都会检查收据的有效性,这证明收据的有效期一直有效。

这就是我检查它的方式。虽然它在我的服务器上 Python 中,但我希望代码足够清晰,让任何人都能理解:

def verify_receipt(receipt):
    r = requests.post(config.APPLE_STORE_URL, json=Subscription.produce_receipt_with_credentials(receipt))
    now = datetime.utcnow()
    if 'latest_receipt_info' in r.json():
        for item in r.json()['latest_receipt_info']:
            dt, tz = item['expires_date'].rsplit(maxsplit=1)
            expires_date = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S').replace(tzinfo=pytz.timezone(tz))
            expires_date = expires_date.astimezone(pytz.utc)
            expires_date = expires_date.replace(tzinfo=None)
            if expires_date > now:
                return True, expires_date, r.json()['latest_receipt'], item
    else:
        current_app.logger.info('latest_receipt_info not found: %s', r.json())
    return False, None, None, None

本质上,我是在“latest_receipt_info”的集合中检查每张收据的“expires_date”。如果以后至少设置了其中一项,则保费支票有效。

但就我而言,即使 Apple 已退还订阅费用,他们仍将其保持活动状态,直到下一次续订。

So what is the point of checking the receipt regularly then? If we can't catch early cancellation?

对于现有用户来说,将到期日期保存在 UserDefaults 中并在到期日期到期时在本地检查并然后检查有效性不会更高效和更快下一张收据?

SWIFT:

UserDefaults.standard.set(expiryDate, forKey: Constants.expiryDate)

更新:

所以根据我收到的答复,我想 cancellation_reasoncancellation_date 会在最新收据中的这些字段旁边?

"latest_receipt_info": [
    {
      "quantity": "1",
      "product_id": "com.x.sub.weekly",
      "transaction_id": "100000053x",
      "original_transaction_id": "100000053x",
      "purchase_date": "2019-06-03 19:52:05 Etc/GMT",
      "purchase_date_ms": "1559591525000",
      "purchase_date_pst": "2019-06-03 12:52:05 America/Los_Angeles",
      "original_purchase_date": "2019-06-03 19:52:06 Etc/GMT",
      "original_purchase_date_ms": "1559591526000",
      "original_purchase_date_pst": "2019-06-03 12:52:06 America/Los_Angeles",
      "expires_date": "2019-06-03 19:55:05 Etc/GMT",
      "expires_date_ms": "1559591705000",
      "expires_date_pst": "2019-06-03 12:55:05 America/Los_Angeles",
      "web_order_line_item_id": "10000000x",
      "is_trial_period": "false",
      "is_in_intro_offer_period": "false"
      "cancellation_reason": "0", 
      "cancellation_date" "2019-06-03 21:55:05 Etc/GMT"
    },

我希望有一种方法可以模拟这一点。我如何根据 docs 对此进行编码并在无法真正测试它的情况下投入生产?

对于退款,您需要检查 cancellation_reason 字段,这将表明客户支持已向用户退款。还会有一个 cancellation_date 指示取消发生的时间。

如果该字段存在,那么您的保费支票应该是无效的:

Treat a canceled receipt the same as if no purchase had ever been made.