Files
keyboard/keyBoard/Class/Pay/StoreKit2Manager/Converts/StoreKitStateConverter.swift

132 lines
4.4 KiB
Swift
Raw Normal View History

2025-12-16 13:10:50 +08:00
//
// StoreKitStateConverter.swift
// StoreKit2Manager
//
// Created by xiaopin on 2025/12/6.
//
import Foundation
import StoreKit
/// StoreKitState
/// StoreKitState Dictionary/JSON
public struct StoreKitStateConverter {
/// StoreKitState Dictionary JSON
/// - Parameter state: StoreKitState
/// - Returns: Dictionary
public static func toDictionary(_ state: StoreKitState) -> [String: Any] {
var dict: [String: Any] = [:]
switch state {
case .idle:
dict["type"] = "idle"
case .loadingProducts:
dict["type"] = "loadingProducts"
// case .productsLoaded(let products):
// dict["type"] = "productsLoaded"
// dict["products"] = ProductConverter.toDictionaryArray(products)
case .loadingPurchases:
dict["type"] = "loadingPurchases"
case .purchasesLoaded:
dict["type"] = "purchasesLoaded"
case .purchasing(let productId):
dict["type"] = "purchasing"
dict["productId"] = productId
case .purchaseSuccess(let productId):
dict["type"] = "purchaseSuccess"
dict["productId"] = productId
case .purchasePending(let productId):
dict["type"] = "purchasePending"
dict["productId"] = productId
case .purchaseCancelled(let productId):
dict["type"] = "purchaseCancelled"
dict["productId"] = productId
case .purchaseFailed(let productId, let error):
dict["type"] = "purchaseFailed"
dict["productId"] = productId
dict["error"] = String(describing: error)
// case .subscriptionStatusChanged(let renewalState):
// dict["type"] = "subscriptionStatusChanged"
// dict["renewalState"] = renewalStateToString(renewalState)
case .restoringPurchases:
dict["type"] = "restoringPurchases"
case .restorePurchasesSuccess:
dict["type"] = "restorePurchasesSuccess"
case .restorePurchasesFailed(let error):
dict["type"] = "restorePurchasesFailed"
dict["error"] = String(describing: error)
case .purchaseRefunded(let productId):
dict["type"] = "purchaseRefunded"
dict["productId"] = productId
case .purchaseRevoked(let productId):
dict["type"] = "purchaseRevoked"
dict["productId"] = productId
case .subscriptionCancelled(let productId, let isFreeTrialCancelled):
dict["type"] = "subscriptionCancelled"
dict["productId"] = productId
dict["isFreeTrialCancelled"] = isFreeTrialCancelled
case .error(let error):
dict["type"] = "error"
dict["error"] = String(describing: error)
}
return dict
}
/// StoreKitState JSON
/// - Parameter state: StoreKitState
/// - Returns: JSON
public static func toJSONString(_ state: StoreKitState) -> String? {
let dict = toDictionary(state)
return dictionaryToJSONString(dict)
}
// MARK: -
///
private static func renewalStateToString(_ state: Product.SubscriptionInfo.RenewalState) -> String {
switch state {
case .subscribed:
return "subscribed"
case .expired:
return "expired"
case .inBillingRetryPeriod:
return "inBillingRetryPeriod"
case .inGracePeriod:
return "inGracePeriod"
case .revoked:
return "revoked"
default:
return "unknown"
}
}
/// Dictionary JSON
private static func dictionaryToJSONString(_ dict: [String: Any]) -> String? {
guard let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted),
let jsonString = String(data: jsonData, encoding: .utf8) else {
return nil
}
return jsonString
}
}