Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading Customer Center when entitlement is granted by another Apple app #4603

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -208,21 +208,29 @@ private extension CustomerCenterViewModel {
func createPurchaseInformation(for transaction: Transaction,
entitlement: EntitlementInfo?) async throws -> PurchaseInformation {
if transaction.store == .appStore {
guard let product = await purchasesProvider.products([transaction.productIdentifier]).first else {
Logger.warning(Strings.could_not_find_subscription_information)
throw CustomerCenterError.couldNotFindSubscriptionInformation
if let product = await purchasesProvider.products([transaction.productIdentifier]).first {
return PurchaseInformation(
entitlement: entitlement,
subscribedProduct: product,
transaction: transaction
)
} else {
Logger.warning(
Strings.could_not_find_product_loading_without_product_information(transaction.productIdentifier)
)

return PurchaseInformation(
entitlement: entitlement,
transaction: transaction
)
}
return PurchaseInformation(
entitlement: entitlement,
subscribedProduct: product,
transaction: transaction
)
} else {
return PurchaseInformation(
entitlement: entitlement,
transaction: transaction
)
}
Logger.warning(Strings.active_product_is_not_apple_loading_without_product_information(transaction.store))

return PurchaseInformation(
entitlement: entitlement,
transaction: transaction
)
}

}
9 changes: 9 additions & 0 deletions RevenueCatUI/Data/Strings.swift
Original file line number Diff line number Diff line change
@@ -68,6 +68,8 @@ enum Strings {
case error_fetching_promotional_offer(Error)
case promo_offer_not_loaded
case could_not_determine_type_of_custom_url
case active_product_is_not_apple_loading_without_product_information(Store)
case could_not_find_product_loading_without_product_information(String)

}

@@ -211,6 +213,13 @@ extension Strings: CustomStringConvertible {
case .could_not_determine_type_of_custom_url:
return "Could not determine the type of custom URL, the URL will be opened externally."

case .active_product_is_not_apple_loading_without_product_information(let store):
return "Active product for user is not an Apple subscription (\(store))." +
" Loading without product information."

case .could_not_find_product_loading_without_product_information(let product):
return "Could not find product with id \(product). Loading without product information."

}
}

Original file line number Diff line number Diff line change
@@ -635,6 +635,56 @@ class CustomerCenterViewModelTests: TestCase {
}
}

func testShouldShowActiveSubscription_withoutProductInformation() async throws {
// If product can't load because maybe it's from another app in same project

let productId = "com.revenuecat.product"
let purchaseDate = "2022-04-12T00:03:28Z"
let expirationDate = "2062-04-12T00:03:35Z"

let customerInfo = CustomerInfoFixtures.customerInfo(
subscriptions: [
CustomerInfoFixtures.Subscription(
id: productId,
store: "app_store",
purchaseDate: purchaseDate,
expirationDate: expirationDate
)
],
entitlements: [
CustomerInfoFixtures.Entitlement(
entitlementId: "premium",
productId: productId,
purchaseDate: purchaseDate,
expirationDate: expirationDate
)
]
)

let viewModel = CustomerCenterViewModel(customerCenterActionHandler: nil,
purchasesProvider: MockCustomerCenterPurchases(
customerInfo: customerInfo,
products: []
))

await viewModel.loadScreen()

expect(viewModel.state) == .success

let purchaseInformation = try XCTUnwrap(viewModel.purchaseInformation)
expect(purchaseInformation.title).to(beNil())
expect(purchaseInformation.durationTitle).to(beNil())
expect(purchaseInformation.explanation) == .earliestRenewal
expect(purchaseInformation.store) == .appStore
expect(purchaseInformation.price) == .unknown

let expirationOrRenewal = try XCTUnwrap(purchaseInformation.expirationOrRenewal)
expect(expirationOrRenewal.label) == .nextBillingDate
expect(expirationOrRenewal.date) == .date(reformat(ISO8601Date: expirationDate))

expect(purchaseInformation.productIdentifier) == productId
}

func testLoadScreenNoActiveSubscription() async throws {
let customerInfo = CustomerInfoFixtures.customerInfoWithExpiredAppleSubscriptions
let mockPurchases = MockCustomerCenterPurchases(customerInfo: customerInfo)