How to detect if user cancel auto-renewable subscriptions during the free trial period?

前端 未结 4 1544
时光取名叫无心
时光取名叫无心 2020-12-09 05:50

Apple\'s document does not seem to mention this. So if user cancels an auto-renewable subscription purchased during the free trial period, how do we detect?

In apps

相关标签:
4条回答
  • 2020-12-09 06:29

    Here is how I got the json data about receipt:

    let sharedSecret = "..."   //you can find this string on AppStoreConnect-IAP
    
    if let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
    FileManager.default.fileExists(atPath: appStoreReceiptURL.path) {
    
    do {
       let receiptData = try Data(contentsOf: appStoreReceiptURL, options: .alwaysMapped)
    
       let receiptString = receiptData.base64EncodedString(options: [])
       AF.request("https://sandbox.itunes.apple.com/verifyReceipt", method: .post, parameters: ["receipt-data":receiptString, "password": sharedSecret], encoding: JSONEncoding.default, headers: nil)
       .responseJSON(completionHandler: { (response) -> Void in
             print(response)
        })
     }
    catch { 
    print("Couldn't read receipt data with error: " + error.localizedDescription) }
    }
    

    If you want a production URL, not the Sandbox, change the url to https://buy.itunes.apple.com/verifyReceipt
    In the response you will find all data you need.
    Note that you need to include this in the Podfile : pod 'Alamofire', '~> 5.2'

    0 讨论(0)
  • 2020-12-09 06:30

    The correct way to do this is to check the auto-renew preference on the receipt. If you want to get notified of this even if the user doesn't open your app (or deletes it) you'll need to store and refresh the receipt on your server. There are 3 fields that you should be concerned with to detect a cancellation.

    1. Expiration Date (lets you know if subscription is still active)
    2. Auto-renew status (lets you know if the user "cancelled")
    3. Cancellation Date (tells you why subscription cancelled by support)

    You should check for receipts that are not expired, not cancelled and have an auto-renew status of "0". These will be users that are in a free trial, but have auto-renew turned off. Unfortunately, the App Store Connect Subscription Status Notifications don't report this to you.

    Here's a good blog post that goes over a little more of the the details: iOS Subscriptions are Hard

    0 讨论(0)
  • 2020-12-09 06:39

    In order to support auto-renewing subscriptions, your app needs to periodically submit the app receipt obtained from NSBundle.mainBundle().appStoreReceiptURL?.path to Apple's receipt validation service.

    Contained in the JSON response from this service is the latest_receipt_info array.

    By examining this array you will be able to determine the currently active subscription(s).

    If a user turns off auto-renewal before the expiration of the free trial then latest_receipt_info won't contain a purchase with an expires_date after the free trial end date

    This means, that strictly speaking, you can't "detect a cancellation" as there is no "cancellation"; there just isn't a renewal at the end of the free trial period.

    0 讨论(0)
  • 2020-12-09 06:49

    This is possible with the web hook feature in iTunes Connect.

    When you set a path for the Subscription Status URL of your app the App Store server will call that URL when the subscription status changes.

    Currently the following key events will trigger the call:

    • INITIAL_BUY Initial purchase of the subscription.
    • CANCEL Subscription was canceled by Apple customer support.
    • RENEWAL Automatic renewal was successful for an expired subscription.
    • INTERACTIVE_RENEWAL Customer renewed a subscription interactively after it lapsed, either by using your app’s interface or on the App Store in account settings.
    • DID_CHANGE_RENEWAL_PREFERENCE Customer changed the plan that takes affect at the next subscription renewal.
    • DID_CHANGE_RENEWAL_STATUS Subscription has expired and customer resubscribed to the same or another plan.

    More can be found here and here.

    0 讨论(0)
提交回复
热议问题