【问题标题】:Detect Apple Subscription Expiration检测 Apple 订阅到期
【发布时间】:2017-02-03 08:34:24
【问题描述】:

我想检测该客户的订阅现已结束/完成,以便我可以删除我提供给他们的设施。

在每次启动应用程序时,我都会从 Apple 订阅中获得以下详细信息:

public void CheckIfSubscriptionIsActive(){
    ConfigurationBuilder builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    // Get a reference to IAppleConfiguration during IAP initialization.
    IAppleConfiguration appleConfig = builder.Configure<IAppleConfiguration>();
    if (!string.IsNullOrEmpty (appleConfig.appReceipt)) {
        Debug.Log (appleConfig.appReceipt);
//            InstantiateDebugText (DebugInfoPanel, "APP Receipt Base64 " + appleConfig.appReceipt);
        var receiptData = System.Convert.FromBase64String (appleConfig.appReceipt);
//            InstantiateDebugText (DebugInfoPanel, "receipt Data "+ receiptData);
        AppleReceipt receipt = new AppleValidator (AppleTangle.Data ()).Validate (receiptData);
        InstantiateDebugText (DebugInfoPanel, "Apple receipt " + receipt);
        foreach (AppleInAppPurchaseReceipt productReceipt in receipt.inAppPurchaseReceipts) {
            Debug.Log ("PRODUCTID: " + productReceipt.productID);
            Debug.Log ("PURCHASE DATE: " + productReceipt.purchaseDate);
            Debug.Log ("EXPIRATION DATE: " + productReceipt.subscriptionExpirationDate);
            Debug.Log ("CANCELDATE DATE: " + productReceipt.cancellationDate);
            InstantiateDebugText (DebugInfoPanel, "PRODUCTID: " + productReceipt.productID);
            InstantiateDebugText (DebugInfoPanel, "PURCHASE DATE: " + productReceipt.purchaseDate);
            InstantiateDebugText (DebugInfoPanel, "EXPIRATION DATE: " + productReceipt.subscriptionExpirationDate);
            InstantiateDebugText (DebugInfoPanel, "CANCELDATE DATE: " + productReceipt.cancellationDate);
        }
    }

因为在检测订阅到期计算时,我无法获取当前 iPhone 日期和时间的帮助,而且我的应用没有使用任何与自定义服务器相关的东西,所以我完全依赖 Apple。

正是因为这个原因,我才使用 Auto Renewable Subscription,以便获得有关订阅期限的信息。

现在请分享您对此的想法,如何检测订阅是否已过期?

【问题讨论】:

    标签: c# ios iphone unity3d


    【解决方案1】:

    您应该先在 Appstore 验证您的收据。 使用您从以下获得的收据数据:

    var receiptData = System.Convert.FromBase64String (appleConfig.appReceipt);
    

    您可以从here查看如何使用苹果服务器验证收据

    但是,不要转发从 status 收到的状态。对于 autoreniew 收据,即使收据有效但已过期,它也会返回成功。 作为回应,您将收到收据。遍历所有收据,检查产品 ID 的“expires_date”字段 = 您所需的产品 ID。 "expires_date" 将包含 epoch time. 。您可以检查此日期是否大于当前日期,如果您在 (productid = yourproducid && expireDate > currentEpocDate) 找到任何收据,则订阅有效。否则过期或停止或未购买。

    创建 json,

      {
        "receipt-data": "receiptData (string))"
    }
    

    并将其发布到苹果的服务器 - (https://sandbox.itunes.apple.com/verifyReceipt)。作为回应,您将收到带有验证状态的收据。

    注意:

    在测试环境中,使用 https://sandbox.itunes.apple.com/verifyReceipt 作为 URL。在 生产,使用https://buy.itunes.apple.com/verifyReceipt作为URL。

    【讨论】:

    • 感谢您的精彩回复。让我问一下其他疑问,1)为了检查订阅到期,我们必须查询我们的自定义服务器吗? 2)我们可以直接从苹果获取订阅到期信息吗?而不是继续依赖自定义服务器?
    • 您可以直接在苹果服务器上查看订阅状态。即使您使用自定义服务器,您的自定义服务器也需要向苹果服务器发送请求以验证收据。正如我在注释中添加的那样:您可以使用给定的 url 进行验证,请检查developer.apple.com/library/content/releasenotes/General/… 一次。
    • 上面你已经给出了过期日期和当前日期的比较来验证订阅目的,这是为了什么目的?如果苹果根据您的最后一条消息回复我有关订阅状态的信息。这件事让我很困惑。因为没有自定义服务器,我无法使用当前的 iphone 日期来验证订阅。
    • 1) 根据我分享的链接 (developer.apple.com/library/content/releasenotes/General/…) -- Apple 会为您提供有效性或收据-因此,即使收据已过期但有效,它也会返回成功。
    • 2) 为什么需要自定义服务器来获取 iphone 的当前日期?
    猜你喜欢
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 2016-03-02
    • 1970-01-01
    • 2011-01-06
    • 2012-06-02
    相关资源
    最近更新 更多