-
Notifications
You must be signed in to change notification settings - Fork 290
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
Offline Entitlements
: create CustomerInfo
from offline entitlements
#2358
Changes from 8 commits
5f7a766
dfaadbb
37c4388
ce3c26c
089861c
8dd1506
72033e0
39b0b5e
6d4d23b
db6f8d6
a44cbff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// Copyright RevenueCat Inc. All Rights Reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// CustomerInfo+OfflineEntitlements.swift | ||
// | ||
// Created by Nacho Soto on 3/21/23. | ||
|
||
import Foundation | ||
|
||
@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *) | ||
extension CustomerInfo { | ||
|
||
convenience init( | ||
from purchasedSK2Products: [PurchasedSK2Product], | ||
mapping: ProductEntitlementMapping, | ||
userID: String, | ||
sandboxEnvironmentDetector: SandboxEnvironmentDetector = BundleSandboxEnvironmentDetector.default | ||
) { | ||
let subscriber = CustomerInfoResponse.Subscriber( | ||
originalAppUserId: userID, | ||
managementUrl: Self.defaultManagementURL, | ||
originalApplicationVersion: SystemInfo.buildVersion, | ||
originalPurchaseDate: Date(), | ||
firstSeen: Date(), | ||
subscriptions: purchasedSK2Products | ||
.dictionaryAllowingDuplicateKeys { $0.productIdentifier } | ||
.mapValues { $0.subscription }, | ||
nonSubscriptions: [:], | ||
entitlements: Self.createEntitlements(with: purchasedSK2Products, mapping: mapping) | ||
) | ||
|
||
let content: CustomerInfoResponse = .init( | ||
subscriber: subscriber, | ||
requestDate: Date(), | ||
rawData: (try? subscriber.asDictionary()) ?? [:] | ||
) | ||
|
||
self.init( | ||
response: content, | ||
entitlementVerification: Self.verification, | ||
sandboxEnvironmentDetector: sandboxEnvironmentDetector | ||
) | ||
} | ||
|
||
private static func createEntitlements( | ||
with products: [PurchasedSK2Product], | ||
mapping: ProductEntitlementMapping | ||
) -> [String: CustomerInfoResponse.Entitlement] { | ||
var result: [String: CustomerInfoResponse.Entitlement] = .init(minimumCapacity: products.count) | ||
|
||
for product in products { | ||
for entitlement in mapping.entitlements(for: product.productIdentifier) { | ||
result[entitlement] = product.entitlement | ||
NachoSoto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
/// Purchases are verified with StoreKit 2. | ||
private static let verification: VerificationResult = .verified | ||
|
||
static let defaultManagementURL = URL(string: "https://apps.apple.com/account/subscriptions")! | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// Copyright RevenueCat Inc. All Rights Reserved. | ||
// | ||
// Licensed under the MIT License (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// PurchasedProductsFetcher.swift | ||
// | ||
// Created by Andrés Boedo on 3/17/23. | ||
|
||
import Foundation | ||
import StoreKit | ||
|
||
/// A type that can fetch purchased products from StoreKit 2. | ||
@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *) | ||
class PurchasedProductsFetcher { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to add tests to this class? (Not sure how difficult it is to mock StoreKit transactions in swift) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can sort of mock transactions by making purchases. We could mock |
||
|
||
func fetchPurchasedProducts() async throws -> [PurchasedSK2Product] { | ||
var result: [PurchasedSK2Product] = [] | ||
|
||
try await Self.forceSyncToEnsureAllTransactionsAreAccountedFor() | ||
|
||
for await transaction in StoreKit.Transaction.currentEntitlements { | ||
switch transaction { | ||
case let .unverified(unverifiedTransaction, verificationError): | ||
Logger.appleWarning( | ||
Strings.offlineEntitlements.found_unverified_transactions_in_sk2(unverifiedTransaction, | ||
verificationError) | ||
) | ||
case let .verified(verifiedTransaction): | ||
result.append(.init(from: verifiedTransaction)) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
private static func forceSyncToEnsureAllTransactionsAreAccountedFor() async throws { | ||
try await AppStore.sync() | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Started this @aboedo