From fa27846b0d79640713c073aaf0424111a12cf4f3 Mon Sep 17 00:00:00 2001 From: Will Taylor Date: Wed, 11 Dec 2024 14:45:02 -0600 Subject: [PATCH] Always reload CustomerInfo when Customer Center is loaded --- .../CustomerCenterPurchasesType.swift | 4 +++- .../Data/CustomerCenterPurchases.swift | 6 ++++-- .../Data/LoadPromotionalOfferUseCase.swift | 2 +- .../ViewModels/CustomerCenterViewModel.swift | 2 +- .../CustomerCenterViewModelTests.swift | 15 +++++++++++++++ .../MockCustomerCenterPurchases.swift | 5 ++++- 6 files changed, 28 insertions(+), 6 deletions(-) diff --git a/RevenueCatUI/CustomerCenter/Abstractions/CustomerCenterPurchasesType.swift b/RevenueCatUI/CustomerCenter/Abstractions/CustomerCenterPurchasesType.swift index 5a8839389d..03fc70f92e 100644 --- a/RevenueCatUI/CustomerCenter/Abstractions/CustomerCenterPurchasesType.swift +++ b/RevenueCatUI/CustomerCenter/Abstractions/CustomerCenterPurchasesType.swift @@ -23,7 +23,9 @@ protocol CustomerCenterPurchasesType: Sendable { var isSandbox: Bool { get } @Sendable - func customerInfo() async throws -> CustomerInfo + func customerInfo( + fetchPolicy: CacheFetchPolicy + ) async throws -> CustomerInfo @Sendable func products(_ productIdentifiers: [String]) async -> [StoreProduct] diff --git a/RevenueCatUI/CustomerCenter/Data/CustomerCenterPurchases.swift b/RevenueCatUI/CustomerCenter/Data/CustomerCenterPurchases.swift index deb9a67d87..1504bd4a25 100644 --- a/RevenueCatUI/CustomerCenter/Data/CustomerCenterPurchases.swift +++ b/RevenueCatUI/CustomerCenter/Data/CustomerCenterPurchases.swift @@ -24,8 +24,10 @@ final class CustomerCenterPurchases: CustomerCenterPurchasesType { return Purchases.shared.isSandbox } - func customerInfo() async throws -> RevenueCat.CustomerInfo { - try await Purchases.shared.customerInfo() + func customerInfo( + fetchPolicy: CacheFetchPolicy + ) async throws -> RevenueCat.CustomerInfo { + try await Purchases.shared.customerInfo(fetchPolicy: fetchPolicy) } func products(_ productIdentifiers: [String]) async -> [StoreProduct] { diff --git a/RevenueCatUI/CustomerCenter/Data/LoadPromotionalOfferUseCase.swift b/RevenueCatUI/CustomerCenter/Data/LoadPromotionalOfferUseCase.swift index 1d480bfb77..9c4947414b 100644 --- a/RevenueCatUI/CustomerCenter/Data/LoadPromotionalOfferUseCase.swift +++ b/RevenueCatUI/CustomerCenter/Data/LoadPromotionalOfferUseCase.swift @@ -41,7 +41,7 @@ class LoadPromotionalOfferUseCase: LoadPromotionalOfferUseCaseType { promoOfferDetails: CustomerCenterConfigData.HelpPath.PromotionalOffer ) async -> Result { do { - let customerInfo = try await self.purchasesProvider.customerInfo() + let customerInfo = try await self.purchasesProvider.customerInfo(fetchPolicy: .default) let subscribedProduct = try await getActiveSubscription(customerInfo) let discount = try findDiscount(for: subscribedProduct, diff --git a/RevenueCatUI/CustomerCenter/ViewModels/CustomerCenterViewModel.swift b/RevenueCatUI/CustomerCenter/ViewModels/CustomerCenterViewModel.swift index 3a9a8829d8..6654919eaa 100644 --- a/RevenueCatUI/CustomerCenter/ViewModels/CustomerCenterViewModel.swift +++ b/RevenueCatUI/CustomerCenter/ViewModels/CustomerCenterViewModel.swift @@ -99,7 +99,7 @@ import RevenueCat func loadPurchaseInformation() async { do { - let customerInfo = try await purchasesProvider.customerInfo() + let customerInfo = try await purchasesProvider.customerInfo(fetchPolicy: .fetchCurrent) let hasActiveProducts = !customerInfo.activeSubscriptions.isEmpty || !customerInfo.nonSubscriptions.isEmpty diff --git a/Tests/RevenueCatUITests/CustomerCenter/CustomerCenterViewModelTests.swift b/Tests/RevenueCatUITests/CustomerCenter/CustomerCenterViewModelTests.swift index 0894a3a185..30f5c5d389 100644 --- a/Tests/RevenueCatUITests/CustomerCenter/CustomerCenterViewModelTests.swift +++ b/Tests/RevenueCatUITests/CustomerCenter/CustomerCenterViewModelTests.swift @@ -70,6 +70,21 @@ class CustomerCenterViewModelTests: TestCase { expect(viewModel.isLoaded) == true } + func testLoadPurchaseInformationAlwaysRefreshesCustomerInfo() async throws { + let mockPurchases = MockCustomerCenterPurchases( + customerInfo: CustomerCenterViewModelTests.customerInfoWithAppleSubscriptions + ) + + let viewModel = CustomerCenterViewModel( + customerCenterActionHandler: nil, + purchasesProvider: mockPurchases + ) + + await viewModel.loadPurchaseInformation() + + expect(mockPurchases.customerInfoFetchPolicy) == .fetchCurrent + } + func testLoadHasSubscriptionsApple() async throws { let mockPurchases = MockCustomerCenterPurchases(customerInfo: CustomerCenterViewModelTests.customerInfoWithAppleSubscriptions) diff --git a/Tests/RevenueCatUITests/CustomerCenter/MockCustomerCenterPurchases.swift b/Tests/RevenueCatUITests/CustomerCenter/MockCustomerCenterPurchases.swift index bcc2b2bfde..50ed6b40d7 100644 --- a/Tests/RevenueCatUITests/CustomerCenter/MockCustomerCenterPurchases.swift +++ b/Tests/RevenueCatUITests/CustomerCenter/MockCustomerCenterPurchases.swift @@ -50,7 +50,10 @@ final class MockCustomerCenterPurchases: @unchecked Sendable, CustomerCenterPurc self.beginRefundShouldFail = beginRefundShouldFail } - func customerInfo() async throws -> RevenueCat.CustomerInfo { + var customerInfoFetchPolicy: CacheFetchPolicy? + func customerInfo(fetchPolicy: CacheFetchPolicy) async throws -> RevenueCat.CustomerInfo { + customerInfoFetchPolicy = fetchPolicy + if let customerInfoError { throw customerInfoError }