diff --git a/Sources/BraveVPN/BraveVPN.swift b/Sources/BraveVPN/BraveVPN.swift index 26729ba0b55..9e2423c4066 100644 --- a/Sources/BraveVPN/BraveVPN.swift +++ b/Sources/BraveVPN/BraveVPN.swift @@ -295,6 +295,29 @@ public class BraveVPN { helper.mainCredential?.hostnameDisplayValue } + /// Type of vpnsubscription + public enum SubscriptionType: Equatable { + case monthly, yearly, other + } + + /// Type of the active purchased vpn plan + public static var activeSubscriptionType: SubscriptionType { + guard let credential = GRDSubscriberCredential.current() else { + logAndStoreError("subscriptionName: failed to retrieve subscriber credentials") + return .other + } + let productId = credential.subscriptionType + + switch productId { + case VPNProductInfo.ProductIdentifiers.monthlySub: + return .monthly + case VPNProductInfo.ProductIdentifiers.yearlySub: + return .yearly + default: + return .other + } + } + /// Name of the purchased vpn plan. public static var subscriptionName: String { guard let credential = GRDSubscriberCredential.current() else { @@ -652,7 +675,7 @@ public class BraveVPN { // MARK: - Promotion - /// Editing product promotiuon order first yearly and monthly after + /// Editing product promotion order first monthly and yearly after @MainActor public static func updateStorePromotionOrder() async { let storePromotionController = SKProductStorePromotionController.default() // Fetch Products @@ -664,12 +687,44 @@ public class BraveVPN { // Update the order do { - try await storePromotionController.update(promotionOrder: [yearlyProduct, monthlyProduct]) + try await storePromotionController.update(promotionOrder: [monthlyProduct, yearlyProduct]) } catch { Logger.module.debug("Error while opdating product promotion order ") } } + /// Hiding Store pormotion if the active subscription for the type + @MainActor public static func hideActiveStorePromotion() async { + let storePromotionController = SKProductStorePromotionController.default() + + // Fetch Products + guard let yearlyProduct = VPNProductInfo.yearlySubProduct, + let monthlyProduct = VPNProductInfo.monthlySubProduct else { + Logger.module.debug("Found empty while fetching SKProducts for promotion order") + return + } + + // Hide the promotion + let activeSubscriptionType = BraveVPN.activeSubscriptionType + + switch activeSubscriptionType { + case .monthly: + await hideSubscriptionType(monthlyProduct) + case .yearly: + await hideSubscriptionType(yearlyProduct) + default: + break + } + + func hideSubscriptionType(_ product: SKProduct) async { + do { + try await storePromotionController.update(promotionVisibility: .hide, for: product) + } catch { + Logger.module.debug("Error while opdating product promotion order ") + } + } + } + public static func activatePaymentTypeForStoredPromotion(savedPayment: SKPayment?) { if let payment = savedPayment { SKPaymentQueue.default().add(payment) diff --git a/Sources/BraveVPN/BraveVPNPreferences.swift b/Sources/BraveVPN/BraveVPNPreferences.swift index 9220c3de9a0..cc01992495b 100644 --- a/Sources/BraveVPN/BraveVPNPreferences.swift +++ b/Sources/BraveVPN/BraveVPNPreferences.swift @@ -9,8 +9,6 @@ import Preferences extension Preferences { final public class VPN { public static let popupShowed = Option(key: "vpn.popup-showed", default: false) - /// We get it from Guardian's servers. - public static let lastPurchaseProductId = Option(key: "vpn.last-purchase-id", default: nil) /// When the current subscription plan expires. It is nil if the user has not bought any vpn plan yet. /// In case of receipt expiration this date might be set to some old date(like year 1970) /// to make sure vpn expiration logic will be called.