Question

How do I know when my state changes form Auto-Renewable Subscription?


Badge

Hi,

I've the following class that handles everything regarding RevenueCat. My current problem is that I just don't get the current state of my subscription. So the point after purchase was clicked. Is it useful to have this class as a singleton like I've it here? Or rather as an EnvironmentObject?

If there's still missing information - please don't hesitate to ask.


enum EUserSubscriptionState {
case SUBSCRIBED
case UNSUBSCRIBED
case NONE
}

enum EUserSubscriptionType {
case TRIAL
case BASIC
case NONE
}

class SubscriptionManager: ObservableObject {
@Published var subscriptionType: EUserSubscriptionType = .NONE
@Published var subscriptionState: EUserSubscriptionState = .NONE
@Published var isSubscriptionSheetShown: Bool = false

static let shared = SubscriptionManager()

private init() {
Purchases.shared.getCustomerInfo { info, error in
guard let info = info, error == nil else { return }

if info.entitlements.all["Basic"]?.isActive == true {
self.subscriptionState = .SUBSCRIBED
} else {

}
}
}

var currentSubscriptionState: Bool {
get {
switch subscriptionState {
case .SUBSCRIBED:
return true
case .UNSUBSCRIBED:
return false
case .NONE:
return false
}
}
}

func fetchPackage(completion: @escaping (Package) -> Void) {
Purchases.shared.getOfferings { offerings, error in
guard let offerings = offerings, error == nil else { return }

guard let package = offerings.all.first?.value.availablePackages.first else { return }

completion(package)
}
}

func onSubscriptionClick() {
fetchPackage { [weak self] package in
self?.purchase(package: package)
print("yyyy subscribed in click method")
}
}

func purchase(package: Package) {
Purchases.shared.purchase(package: package) { transaction, info, error, userCancelled in
guard let transaction = transaction, let info = info, error == nil, !userCancelled else { return }
print("yyyy subscribed in purchase")

print("yyyy \(transaction)")
print("yyyy \(info.entitlements)")
}
}

func restorePurchases() {
Purchases.shared.restorePurchases { info, error in
guard let info = info, error == nil else { return }

if info.entitlements.all["Basic"]?.isActive == true {
self.subscriptionState = .UNSUBSCRIBED

} else {

}
}
}

func onTapHalfYearRestore() {
restorePurchases()
}
func fetchCustomerSubscriptionState() {
Purchases.shared.getCustomerInfo { info, error in
guard let info = info, error == nil else { return }

if info.entitlements.all["Basic"]?.isActive == true {
self.subscriptionState = .SUBSCRIBED
} else {

}
}
}
}

extension AppDelegate: PurchasesDelegate {
func purchases(_ purchases: Purchases, receivedUpdated customerInfo: CustomerInfo) {
let subscriptionManager = SubscriptionManager.shared
print("yyyy")
if customerInfo.entitlements.all["Basic"]?.isActive == true {
subscriptionManager.subscriptionState = .SUBSCRIBED
subscriptionManager.subscriptionType = .BASIC
print("yyyy basic subscribed")
} else {
subscriptionManager.subscriptionState = .UNSUBSCRIBED
subscriptionManager.subscriptionType = .NONE
print("unsubscribed basic subscribed")
}
}
}

 


0 replies

Be the first to reply!

Reply