How to change iOS in app purchase when changing to lifetime membership instead of subscription?
Asked Answered
C

1

7

Our app allows users to either have a 1 month subscription, or a lifetime subscription. 1 Month is an auto-renew subscription, where lifetime is obviously a one time purchase.

Our issue arises when a user has a 1 month subscription, and wants to change to a lifetime subscription, we aren't sure how to cancel the user's monthly subscription for them, or if that is even possible.

Colvin answered 25/7, 2017 at 14:14 Comment(0)
G
-1

Here is an example, how you might implement the process :

  1. Prompting the User to Cancel the Monthly Subscription:

     func promptUserToCancelSubscription() {
     let alert = UIAlertController(title: "Cancel Monthly Subscription", 
                                   message: "To switch to a lifetime subscription, please cancel your current monthly subscription in the App Store settings.", 
                                   preferredStyle: .alert)
     alert.addAction(UIAlertAction(title: "Cancel Subscription", style: .default) { _ in
         if let url = URL(string: "https://apps.apple.com/account/subscriptions") {
             UIApplication.shared.open(url)
         }
     })
     alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
     present(alert, animated: true, completion: nil)
    

    }

  2. Validating the Receipt and Checking Subscription Status :

    func validateReceipt() {
    guard let receiptURL = Bundle.main.appStoreReceiptURL, let receipt = try? Data(contentsOf: receiptURL) else {
        // Handle error: receipt not found
        return
    }
    
    let receiptData = receipt.base64EncodedString(options: [])
    
    // Send receipt data to your server for validation
    let request = ["receipt-data": receiptData, "password": "your-shared-secret"]
    // Perform network request to your server to validate the receipt
    // Parse the response to determine the subscription status
    

    }

    // Example server response parsing

    func handleServerResponse(_ response: [String: Any]) { guard let receiptInfo = response["latest_receipt_info"] as? [[String: Any]] else { // Handle error return }

    var isLifetimeMember = false
    var hasActiveSubscription = false
    
    for purchase in receiptInfo {
        if let productId = purchase["product_id"] as? String {
            if productId == "your.lifetime.product.id" {
                isLifetimeMember = true
            }
            if productId == "your.monthly.subscription.id" {
                hasActiveSubscription = true
            }
        }
    }
    
    if isLifetimeMember {
        // Provide lifetime membership benefits
    } else if hasActiveSubscription {
        // Provide monthly subscription benefits
    } else {
        // No valid subscription
    }
    

    }

Gratify answered 4/6, 2024 at 6:46 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Elocution

© 2022 - 2025 — McMap. All rights reserved.