Siddharth
Siddharth

Reputation: 4312

Detect Apple Subscription Expiration

I want to detect that customer's subscription now over / complete so I can remove facilities I was providing to them.

On each launch of application, I have following kind of details from Apple Subscription:

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);
        }
    }

Because in detection of subscription expiration calculation, I can't take help of current iPhone date and time and my app was not using any of custom server related things so I am totally dependent on Apple for this.

Because only this reason I was using Auto Renewable Subscription so that I have information regarding subscription duration.

Now please share your thought regarding this, how to detect subscription is expired now?

Upvotes: 2

Views: 1387

Answers (1)

PlusInfosys
PlusInfosys

Reputation: 3436

You should valdate yoru receipt with Appstore first. use receipt data you are getting from following:

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

You can check how to validate receipt with apple server from here

However, don't relay on status received from status. for autoreniew receipt, it will return success even if receipt is valid but expired. In response, you will get receipts. Loop through all receipts, check for "expires_date" field for product id = your required product id. "expires_date" will contain epoch time. . You can check if this date is greater than current date and if you found any receipt where (productid = yourproducid && expireDate > currentEpocDate) , subscription is valid. else its expired or stopped or not purchased.

Create json,

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

and post it to apple's server - ( https://sandbox.itunes.apple.com/verifyReceipt). In response, You will get receipt with validaton status.

Note:

In the test environment, use https://sandbox.itunes.apple.com/verifyReceipt as the URL. In production, use https://buy.itunes.apple.com/verifyReceipt as the URL.

Upvotes: 3

Related Questions