Files
purchases-ios/Sources/Purchasing/TransactionsManager.swift
T
NachoSoto 23afeb96d0 Added StoreKit2Setting to replace useStoreKit2IfAvailable (#1467)
This adds a third case `disabled` that allows tests to explicitly disable StoreKit 2.
Some types, like `TrialOrIntroPriceEligibilityChecker` and `TransactionsManager` always used SK2 because the implementation was better. But this meant that tests couldn't explicitly disable it (other than calling the internal methods directly).

This allows #1464 to be a lot simpler but being able to use the public `Purchases` methods, but forcing the implementation to use SK1.

Note that this is purely an implementation detail. `StoreKit2Setting` is `internal`, and the public API still only exposes the same boolean.

This basically solves [CF-497] too, since those tests will now force `TrialOrIntroPriceEligibilityChecker` to run the SK1 implementation.

[CF-497]: https://revenuecats.atlassian.net/browse/CF-497?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
2022-04-11 11:41:47 -07:00

48 lines
1.5 KiB
Swift

//
// 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
//
// TransactionsManager.swift
//
// Created by Juanpe Catalán on 9/12/21.
import StoreKit
class TransactionsManager {
private let storeKit2Setting: StoreKit2Setting
private let receiptParser: ReceiptParser
init(storeKit2Setting: StoreKit2Setting,
receiptParser: ReceiptParser) {
self.storeKit2Setting = storeKit2Setting
self.receiptParser = receiptParser
}
func customerHasTransactions(receiptData: Data, completion: @escaping (Bool) -> Void) {
// Note: this uses SK2 (unless it's explicitly disabled) because its implementation is more accurate.
if #available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *), self.storeKit2Setting != .disabled {
_ = Task<Void, Never> {
completion(await sk2CheckCustomerHasTransactions())
}
} else {
completion(sk1CheckCustomerHasTransactions(receiptData: receiptData))
}
}
func sk1CheckCustomerHasTransactions(receiptData: Data) -> Bool {
receiptParser.receiptHasTransactions(receiptData: receiptData)
}
@available(iOS 15.0, tvOS 15.0, watchOS 8.0, macOS 12.0, *)
func sk2CheckCustomerHasTransactions() async -> Bool {
await StoreKit.Transaction.all.contains { _ in true }
}
}