Feat: [P2-208] Add Restore purchases logic

Refs: https://gitlab.protontech.ch/apple/shared/protoncore/-/merge_requests/2071
This commit is contained in:
Tiziano Bruni 2025-02-14 16:16:45 +00:00
commit 53bbc24be5
3 changed files with 93 additions and 0 deletions

View file

@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1620"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ProtonCorePaymentsUIV2"
BuildableName = "ProtonCorePaymentsUIV2"
BlueprintName = "ProtonCorePaymentsUIV2"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ProtonCorePaymentsUIV2"
BuildableName = "ProtonCorePaymentsUIV2"
BlueprintName = "ProtonCorePaymentsUIV2"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View file

@ -48,6 +48,17 @@ final public class PaymentsV2: Sendable {
//MARK: Public functions
//MARK: Restore Purchases
public func restorePurchases(sessionId: String,
token: String,
doh: DoHInterface & ServerConfig,
appVersion: String) async throws -> CurrentSubscriptionResponse {
let plansManager = ProtonPlansManager(doh: doh,
remoteManager: RemoteManager(sessionID: sessionId, authToken: token, appVersion: appVersion))
return try await plansManager.restorePurchases()
}
//MARK: Presentation
public func availablePlansView(sessionID: String,
accessToken: String,

View file

@ -44,6 +44,7 @@ public enum ProtonPlansManagerError: Error {
case unableToFetchProductsFromStore
case unableToMatchProtonPlanToStoreProduct
case unableToGetUserTransactionUUID
case unableToRestorePurchases
// Transaction error
case transactionNotFound
@ -185,6 +186,20 @@ public final class ProtonPlansManager: NSObject, ProtonPlansManagerProviding, @u
}
}
public func restorePurchases() async throws -> CurrentSubscriptionResponse {
do {
try await AppStore.sync()
return try await getCurrentPlan()
} catch {
debugPrint(error)
if error is PlansComposerError {
throw error
} else {
throw(ProtonPlansManagerError.unableToRestorePurchases)
}
}
}
private func generateUserTransactionUUID() async throws -> UUID {
guard let request = try? paymentsAPI.url(for: .userTransactionUUID) else {
throw ProtonPlansManagerError.unableToGetUserTransactionUUID