-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
140bdb6
commit 1735eb6
Showing
23 changed files
with
658 additions
and
284 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
70 changes: 0 additions & 70 deletions
70
DuckDuckGo/PrivacyPro/PurchaseFlows/AppStoreRestoreFlow.swift
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
DuckDuckGo/PrivacyPro/Subscription/Flows/AppStore/AppStoreRestoreFlow.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// AppStorePurchaseFlow.swift | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import StoreKit | ||
|
||
@available(macOS 12.0, iOS 15.0, *) | ||
public final class AppStoreRestoreFlow { | ||
|
||
// swiftlint:disable:next large_tuple | ||
public typealias RestoredAccountDetails = (authToken: String, accessToken: String, externalID: String, email: String?) | ||
|
||
public enum Error: Swift.Error { | ||
case missingAccountOrTransactions | ||
case pastTransactionAuthenticationError | ||
case failedToObtainAccessToken | ||
case failedToFetchAccountDetails | ||
case failedToFetchSubscriptionDetails | ||
case subscriptionExpired(accountDetails: RestoredAccountDetails) | ||
case somethingWentWrong | ||
} | ||
|
||
public static func restoreAccountFromPastPurchase() async -> Result<Void, AppStoreRestoreFlow.Error> { | ||
guard let lastTransactionJWSRepresentation = await PurchaseManager.mostRecentTransaction() else { return .failure(.missingAccountOrTransactions) } | ||
|
||
let accountManager = AccountManager() | ||
|
||
// Do the store login to get short-lived token | ||
let authToken: String | ||
|
||
switch await AuthService.storeLogin(signature: lastTransactionJWSRepresentation) { | ||
case .success(let response): | ||
authToken = response.authToken | ||
case .failure: | ||
return .failure(.pastTransactionAuthenticationError) | ||
} | ||
|
||
let accessToken: String | ||
let email: String? | ||
let externalID: String | ||
|
||
switch await accountManager.exchangeAuthTokenToAccessToken(authToken) { | ||
case .success(let exchangedAccessToken): | ||
accessToken = exchangedAccessToken | ||
case .failure: | ||
return .failure(.failedToObtainAccessToken) | ||
} | ||
|
||
switch await accountManager.fetchAccountDetails(with: accessToken) { | ||
case .success(let accountDetails): | ||
email = accountDetails.email | ||
externalID = accountDetails.externalID | ||
case .failure: | ||
return .failure(.failedToFetchAccountDetails) | ||
} | ||
|
||
var isSubscriptionActive = false | ||
|
||
switch await SubscriptionService.getSubscriptionDetails(token: accessToken) { | ||
case .success(let response): | ||
isSubscriptionActive = response.isSubscriptionActive | ||
case .failure: | ||
return .failure(.somethingWentWrong) | ||
} | ||
|
||
if isSubscriptionActive { | ||
accountManager.storeAuthToken(token: authToken) | ||
accountManager.storeAccount(token: accessToken, email: email, externalID: externalID) | ||
return .success(()) | ||
} else { | ||
let details = RestoredAccountDetails(authToken: authToken, accessToken: accessToken, externalID: externalID, email: email) | ||
return .failure(.subscriptionExpired(accountDetails: details)) | ||
} | ||
} | ||
} |
File renamed without changes.
91 changes: 91 additions & 0 deletions
91
DuckDuckGo/PrivacyPro/Subscription/Flows/Stripe/StripePurchaseFlow.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// StripePurchaseFlow.swift | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import StoreKit | ||
|
||
public final class StripePurchaseFlow { | ||
|
||
public enum Error: Swift.Error { | ||
case noProductsFound | ||
case accountCreationFailed | ||
} | ||
|
||
public static func subscriptionOptions() async -> Result<SubscriptionOptions, StripePurchaseFlow.Error> { | ||
|
||
guard case let .success(products) = await SubscriptionService.getProducts(), !products.isEmpty else { return .failure(.noProductsFound) } | ||
|
||
let currency = products.first?.currency ?? "USD" | ||
|
||
let formatter = NumberFormatter() | ||
formatter.numberStyle = .currency | ||
formatter.locale = Locale(identifier: "en_US@currency=\(currency)") | ||
|
||
let options: [SubscriptionOption] = products.map { | ||
var displayPrice = "\($0.price) \($0.currency)" | ||
|
||
if let price = Float($0.price), let formattedPrice = formatter.string(from: price as NSNumber) { | ||
displayPrice = formattedPrice | ||
} | ||
|
||
let cost = SubscriptionOptionCost(displayPrice: displayPrice, recurrence: $0.billingPeriod.lowercased()) | ||
|
||
return SubscriptionOption(id: $0.productId, | ||
cost: cost) | ||
} | ||
|
||
let features = SubscriptionFeatureName.allCases.map { SubscriptionFeature(name: $0.rawValue) } | ||
|
||
return .success(SubscriptionOptions(platform: SubscriptionPlatformName.stripe.rawValue, | ||
options: options, | ||
features: features)) | ||
} | ||
|
||
public static func prepareSubscriptionPurchase(emailAccessToken: String?) async -> Result<PurchaseUpdate, StripePurchaseFlow.Error> { | ||
|
||
var authToken: String = "" | ||
|
||
switch await AuthService.createAccount(emailAccessToken: emailAccessToken) { | ||
case .success(let response): | ||
authToken = response.authToken | ||
AccountManager().storeAuthToken(token: authToken) | ||
case .failure: | ||
return .failure(.accountCreationFailed) | ||
} | ||
|
||
return .success(PurchaseUpdate(type: "redirect", token: authToken)) | ||
} | ||
|
||
public static func completeSubscriptionPurchase() async { | ||
let accountManager = AccountManager() | ||
|
||
if let authToken = accountManager.authToken { | ||
print("Exchanging token") | ||
|
||
if case let .success(accessToken) = await accountManager.exchangeAuthTokenToAccessToken(authToken), | ||
case let .success(accountDetails) = await accountManager.fetchAccountDetails(with: accessToken) { | ||
accountManager.storeAuthToken(token: authToken) | ||
accountManager.storeAccount(token: accessToken, email: accountDetails.email, externalID: accountDetails.externalID) | ||
} | ||
} | ||
|
||
if #available(macOS 12.0, iOS 15.0, *) { | ||
await AppStorePurchaseFlow.checkForEntitlements(wait: 2.0, retry: 5) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.