Skip to main content
Question

iOS sync / restore offer code purchase not working

  • August 8, 2024
  • 1 reply
  • 109 views

Forum|alt.badge.img

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.all["premium"]?.isActive == true

    }

}

This post has been closed for comments

1 reply

Forum|alt.badge.img+8
  • RevenueCat Staff
  • 576 replies
  • November 13, 2024

Hi, apologies that we missed this post. It sounds like what might be happening in this case is that Apple has in the receipt the subscription with the expiration for the 11th of August which is why in RevenueCat you are seeing that subscription event. Apple will then probably update the receipt on the 11th of August with the trial period where RevenueCat should then generate a trial event with the new expiration of the 11th of September. 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings