You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
PadelClubData/PadelClubData/Subscriptions/Guard.swift

330 lines
12 KiB

//
// Guard.swift
// Poker Analytics 6
//
// Created by Laurent Morvillier on 20/04/2022.
//
import Foundation
import StoreKit
import LeStorage
import Combine
@available(iOS 15, *)
@objc public class Guard: NSObject {
public static var main: Guard = Guard()
@Published public private(set) var purchasedTransactions = Set<StoreKit.Transaction>()
public var currentBestPurchase: Purchase? = nil
var updateListenerTask: Task<Void, Never>? = nil
public let freeTournaments: Int = 3
override init() {
super.init()
self.updateListenerTask = self.listenForTransactions()
Task {
do {
try await self.refreshPurchasedAppleProducts()
} catch {
Logger.error(error)
}
Logger.log("plan = \(String(describing: currentBestPurchase?.productId))")
}
NotificationCenter.default.addObserver(self, selector: #selector(collectionDidLoad), name: NSNotification.Name.CollectionDidLoad, object: nil)
}
deinit {
self.updateListenerTask?.cancel()
NotificationCenter.default.removeObserver(self)
}
@objc func collectionDidLoad(notification: Notification) {
if let _ = notification.object as? BaseCollection<Purchase> {
self._updateBestPlan()
}
}
func productIds() async -> [String] {
var productIds: [String] = []
for await result in Transaction.all {
do {
let verified = try self.checkVerified(result)
productIds.append(verified.productID)
} catch {
Logger.error(error)
}
}
return productIds
}
public func refreshPurchasedAppleProducts() async throws {
// Iterate through the user's purchased products.
for await verificationResult in Transaction.currentEntitlements {
let transaction = try await self.processTransactionResult(verificationResult)
print("processs product id = \(transaction.productID)")
DispatchQueue.main.async {
NotificationCenter.default.post(name: Notification.Name.StoreEventHappened, object: nil)
}
await transaction.finish()
}
}
func listenForTransactions() -> Task<Void, Never> {
return Task(priority: .background) {
//Iterate through any transactions which didn't come from a direct call to `purchase()`.
for await result in Transaction.updates {
// Logger.log(">>> update = \(result)")
do {
let transaction = try self.checkVerified(result)
//Deliver content to the user.
await self.updatePurchasedIdentifiers(transaction)
//Always finish a transaction.
await transaction.finish()
} catch {
Logger.error(error)
//StoreKit has a receipt it can read but it failed verification. Don't deliver content to the user.
print("Transaction failed verification")
}
}
}
}
func checkVerified<T>(_ result: VerificationResult<T>) throws -> T {
//Check if the transaction passes StoreKit verification.
switch result {
case .unverified:
//StoreKit has parsed the JWS but failed verification. Don't deliver content to the user.
throw StoreManagerError.failedVerification
case .verified(let safe):
//If the transaction is verified, unwrap and return it.
return safe
}
}
@MainActor
func updatePurchasedIdentifiers(_ transaction: StoreKit.Transaction) async {
// Logger.log("\(transaction.productID) > purchase = \(transaction.originalPurchaseDate), exp date= \(transaction.expirationDate), rev date = \(transaction.revocationDate)")
// Logger.log("purchase date = \(transaction.purchaseDate)")
do {
if transaction.revocationDate == nil {
// If the App Store has not revoked the transaction, add it to the list of `purchasedIdentifiers`.
purchasedTransactions.insert(transaction)
} else {
// If the App Store has revoked this transaction, remove it from the list of `purchasedIdentifiers`.
purchasedTransactions.remove(transaction)
}
try self._updatePurchase(transaction: transaction)
} catch {
Logger.error(error)
}
self._updateBestPlan()
}
fileprivate func _updatePurchase(transaction: StoreKit.Transaction) throws {
let purchases = DataStore.shared.purchases
if let purchase = self._purchaseByTransactionId(transaction.originalID) {
purchase.revocationDate = transaction.revocationDate
purchase.expirationDate = transaction.expirationDate
purchase.purchaseDate = transaction.purchaseDate
purchase.productId = transaction.productID
try purchases.addOrUpdate(instance: purchase)
} else {
let purchase: Purchase = try transaction.purchase()
try purchases.addOrUpdate(instance: purchase)
}
}
fileprivate func _purchaseByTransactionId(_ transactionId: UInt64) -> Purchase? {
let purchases = DataStore.shared.purchases
return purchases.first(where: { $0.id == transactionId })
}
func processTransactionResult(_ result: VerificationResult<StoreKit.Transaction>) async throws -> StoreKit.Transaction {
let transaction = try checkVerified(result)
// Deliver content to the user.
await updatePurchasedIdentifiers(transaction)
return transaction
}
public var currentPlan: StoreItem? {
#if DEBUG
if let plan = PListReader.readString(plist: "local", key: "plan"), !plan.isEmpty {
return StoreItem(rawValue: plan)
}
if let currentBestPurchase = self.currentBestPurchase, let plan = StoreItem(rawValue: currentBestPurchase.productId) {
return plan
}
#elseif TESTFLIGHT
return .monthlyUnlimited
#elseif PRODTEST
return .monthlyUnlimited
#else
if let currentBestPurchase = self.currentBestPurchase, let plan = StoreItem(rawValue: currentBestPurchase.productId) {
return plan
}
#endif
return nil
}
public func userFilteredPurchases() -> [StoreKit.Transaction] {
// Logger.log("self.purchasedTransactions = \(self.purchasedTransactions.count)")
guard let userId = StoreCenter.main.userId else {
return []
}
let userTransactions = self.purchasedTransactions.filter { userId == $0.appAccountToken?.uuidString || $0.appAccountToken == nil }
let now: Date = Date()
// print("now = \(now)")
return userTransactions.filter { transaction in
if let expirationDate = transaction.expirationDate {
// print("exp = \(expirationDate)")
return expirationDate > now
} else {
return true
}
}
}
/// Update best plan by filtering Apple purchases with registered purchases by the user
fileprivate func _updateBestPlan() {
var purchases: [Purchase] = []
// Make sure the purchase has been done with the logged user
let userPurchases = self.userFilteredPurchases().compactMap { try? $0.purchase() }
purchases.append(contentsOf: userPurchases)
let validPurchases = DataStore.shared.purchases.filter { $0.isValid() }
Logger.log("valid purchases = \(validPurchases.count)")
purchases.append(contentsOf: validPurchases)
if let purchase = purchases.first(where: { $0.productId == StoreItem.monthlyUnlimited.rawValue }) {
self.currentBestPurchase = purchase
} else if let purchase = purchases.first(where: { $0.productId == StoreItem.fivePerMonth.rawValue }) {
self.currentBestPurchase = purchase
}
}
fileprivate func _purchasedTournamentCount() -> Int {
let purchases = DataStore.shared.purchases
let units = purchases.filter { $0.productId == StoreItem.unit.rawValue }
return units.reduce(0) { $0 + ($1.quantity ?? 0) }
// let units = self.userFilteredPurchases().filter { $0.productID == StoreItem.unit.rawValue }
// return units.reduce(0) { $0 + $1.purchasedQuantity }
}
public func paymentForNewTournament() -> TournamentPayment? {
switch self.currentPlan {
case .monthlyUnlimited:
return TournamentPayment.unlimited
case .fivePerMonth:
if let purchaseDate = self.currentBestPurchase?.purchaseDate {
let count = DataStore.shared.subscriptionUnitlyPayedTournaments(after: purchaseDate)
if count < StoreItem.five {
return TournamentPayment.subscriptionUnit
}
}
return self._paymentWithoutSubscription()
default:
return self._paymentWithoutSubscription()
}
}
fileprivate func _paymentWithoutSubscription() -> TournamentPayment? {
let freelyPayed: Int = DataStore.shared.tournaments.filter { $0.payment == .free && $0.isCanceled == false }.count
if freelyPayed < self.freeTournaments {
return TournamentPayment.free
}
let tournamentCreditCount: Int = self._purchasedTournamentCount()
let unitlyPayed = DataStore.shared.tournaments.filter { $0.payment == .unit && $0.isCanceled == false }.count
if tournamentCreditCount > unitlyPayed {
return TournamentPayment.unit
}
return nil
}
public var remainingTournaments: Int {
let unitlyPayed = DataStore.shared.tournaments.filter { $0.payment == TournamentPayment.unit }.count
let tournamentCreditCount = self._purchasedTournamentCount()
return tournamentCreditCount - unitlyPayed
}
public var remainingFreeTournaments: Int {
let freelyPayed = DataStore.shared.tournaments.filter { $0.payment == TournamentPayment.free }.count
return self.freeTournaments - freelyPayed
}
func disconnect() {
let purchases = DataStore.shared.purchases
purchases.reset()
}
}
public struct PurchaseRow: Identifiable {
public var id: UInt64
public var name: String
public var item: StoreItem
public var quantity: Int?
public var expirationDate: Date?
public var remainingCount: Int? = nil
public init(id: UInt64, name: String, item: StoreItem, quantity: Int? = nil, expirationDate: Date? = nil, remainingCount: Int? = nil) {
self.id = id
self.name = name
self.item = item
self.quantity = quantity
self.expirationDate = expirationDate
self.remainingCount = remainingCount
}
}
fileprivate extension StoreKit.Transaction {
func purchase() throws -> Purchase {
guard let userId = StoreCenter.main.userId else {
throw StoreError.missingUserId
}
return Purchase(transactionId: self.originalID,
user: userId,
purchaseDate: self.purchaseDate,
productId: self.productID,
quantity: self.purchasedQuantity,
revocationDate: self.revocationDate,
expirationDate: self.expirationDate)
}
}