From bae843363cbb80b82beed343be949946731d6957 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Mon, 24 Jul 2023 23:32:05 +0200 Subject: [PATCH] Fall back sandbox receipt to release receipt (#337) Could be a way to enable paid features in TestFlight builds for those who originally bought them from the App Store. --- .../App/Managers/ProductManager.swift | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/Passepartout/App/Managers/ProductManager.swift b/Passepartout/App/Managers/ProductManager.swift index 4f3b979b..e74b69a5 100644 --- a/Passepartout/App/Managers/ProductManager.swift +++ b/Passepartout/App/Managers/ProductManager.swift @@ -222,11 +222,7 @@ final class ProductManager: NSObject, ObservableObject { } func reloadReceipt(andNotify: Bool = true) { - guard let url = Bundle.main.appStoreReceiptURL else { - pp_log.warning("No App Store receipt found!") - return - } - guard let receipt = Receipt(contentsOfURL: url) else { + guard let receipt else { pp_log.error("Could not parse App Store receipt!") return } @@ -281,8 +277,40 @@ extension ProductManager: SKPaymentTransactionObserver { } } -extension ProductManager { - private func detectRefunds(_ refunds: Set) { +private extension ProductManager { + var isMac: Bool { + #if targetEnvironment(macCatalyst) + true + #else + false + #endif + } + + var receipt: Receipt? { + guard let url = Bundle.main.appStoreReceiptURL else { + pp_log.warning("No App Store receipt found!") + return nil + } + let receipt = Receipt(contentsOfURL: url) + + // in TestFlight, attempt fallback to existing release receipt + if Bundle.main.isTestFlight { + guard let receipt else { + let releaseUrl = url.deletingLastPathComponent().appendingPathComponent("receipt") + guard releaseUrl != url else { + assertionFailure("How can release URL be equal to sandbox URL in TestFlight?") + return nil + } + pp_log.warning("Sandbox receipt not found, falling back to Release receipt") + return Receipt(contentsOfURL: releaseUrl) + } + return receipt + } + + return receipt + } + + func detectRefunds(_ refunds: Set) { let isEligibleForFullVersion = isFullVersion() let hasCancelledFullVersion: Bool let hasCancelledTrustedNetworks: Bool @@ -305,13 +333,3 @@ extension ProductManager { } } } - -extension ProductManager { - private var isMac: Bool { - #if targetEnvironment(macCatalyst) - true - #else - false - #endif - } -}