Skip to main content
Question

iOS sync / restore offer code purchase not working

  • 8 August 2024
  • 0 replies
  • 21 views

Hi, in appstore my subscription is valid until 11th of August, after which i have a trial period that will last until 11th of September. However, in the app I just see end date as 11th of August, and examining active entitlements and purchases logs, calling sync and restore doesnt change the end date and sync the purchase properly. Any help is welcome, thank you!

On restore purchase I tried with both appstore restore function and revenue cats restore and sync, but no luck so far.
 

    func restorePurchases() {

        Task {

            do {

                try? await AppStore.sync()

                var customerInfo = try await PurchaseManager.restorePurchases()

                self.onAppear()

 

                AlertService.showSuccessAlert(message: "Purchases Restored")

            } catch {

                print("Error restoring purchases: \(error.localizedDescription)")

                AlertService.showGenericAlert()

            }

        }

    }



I am initializing revenue cat in main like this and when needed, I login the user so revenue cat knows which user the purchase is connected with. My app is in production, and I tried to create code redeem feature, but when i redeem it inside the app, I am led straight to appstore. Code was redeemed there, but no luck syncing or restoring the purchases yet. 

    private func initializeRevenueCat() {

        Purchases.logLevel = .debug

 

        if let userId = getCurrentUserId(){

            Purchases.configure(withAPIKey: Constants.revenueCatKey, appUserID: userId)

            PurchaseManager.loginUser()

        } else {

            Purchases.configure(withAPIKey: Constants.revenueCatKey)

        }

    }


Code related to my purchase manager and subscriptions can be seen below
 

class PurchaseManager: NSObject, PurchasesDelegate {

    // MARK: - Private properties

 

    private static let customerInfoSubject = CurrentValueSubject<CustomerInfo?, Never>(nil)

 

    override init() {

        super.init()

        Purchases.shared.delegate = self

    }

 

    static func getCurrentUserId() -> String? {

        return Auth.auth().currentUser?.uid

    }

 

    static func getSubscriptionEndDate(completion: @escaping (Date?) -> Void) {

        Purchases.shared.getCustomerInfo { info, _ in

            if let entitlement = info?.entitlements "premium"], entitlement.isActive {

                let endDate = entitlement.expirationDate

                completion(endDate)

            } else {

                completion(nil)

            }

        }

    }

 

    static func isPremium(completion: @escaping (Bool) -> Void) {

        Purchases.shared.getCustomerInfo { info, error in

            guard let entitlements = info?.entitlements, error == nil else {

                completion(false)

                return

            }

 

            if (info?.isProSubscriptionActive) != nil {

                completion(info!.isProSubscriptionActive)

                return

            }

            completion(false)

        }

    }

 

    func getCurrentUserId() -> String? {

        return Auth.auth().currentUser?.uid

    }

 

    static func logOut() async {

        do {

            let customerInfo = try await Purchases.shared.logOut()

            customerInfoSubject.send(customerInfo)

        } catch {

            print("Failed logging out user for purchases: \(error)")

        }

    }

 

    static func login() async {

        do {

            guard let userId = getCurrentUserId() else {

                return

            }

            let result = try await Purchases.shared.logIn(userId)

            customerInfoSubject.send(result.customerInfo)

        } catch {

            print("Failed logging in user for purchases: \(error)")

        }

    }

 

    static func restorePurchases() async throws -> CustomerInfo {

        let customerInfo = try await Purchases.shared.restorePurchases()

        customerInfoSubject.send(customerInfo)

        return customerInfo

    }

}

 

extension CustomerInfo {

    var isProSubscriptionActive: Bool {

        entitlements.allo"premium"]?.isActive == true

    }

}