Files
keyboard/keyBoard/Class/Pay/StoreKit2Manager/Converts/ProductConverter.swift
2025-12-16 13:10:50 +08:00

117 lines
4.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// ProductConverter.swift
// StoreKit2Manager
//
// Created by xiaopin on 2025/12/6.
//
import Foundation
import StoreKit
/// Product
/// Product Dictionary/JSON
public struct ProductConverter {
/// Product Dictionary JSON
/// - Parameter product: Product
/// - Returns: Dictionary
public static func toDictionary(_ product: Product) -> [String: Any] {
var dict: [String: Any] = [:]
//
dict["id"] = product.id
dict["displayName"] = product.displayName
dict["description"] = product.description
//
let priceDecimal = product.price
let priceDouble = NSDecimalNumber(decimal: priceDecimal).doubleValue
dict["price"] = Double(String(format: "%.2f", priceDouble)) ?? priceDouble
dict["displayPrice"] = product.displayPrice
//
dict["type"] = productTypeToString(product.type)
//
dict["isFamilyShareable"] = product.isFamilyShareable
// JSON
// jsonRepresentation Data Base64 便 JSON
let jsonData = product.jsonRepresentation
if let jsonString = String(data: jsonData, encoding: .utf8) {
dict["jsonRepresentation"] = jsonString
} else {
dict["jsonRepresentation"] = ""
}
//
if let subscription = product.subscription {
dict["subscription"] = SubscriptionConverter.subscriptionInfoToDictionary(subscription, product: product)
} else {
dict["subscription"] = NSNull()
}
return dict
}
/// Product Dictionary
/// - Parameter products: Product
/// - Returns: Dictionary
public static func toDictionaryArray(_ products: [Product]) -> [[String: Any]] {
return products.map { toDictionary($0) }
}
/// Product JSON
/// - Parameter product: Product
/// - Returns: JSON
public static func toJSONString(_ product: Product) -> String? {
let dict = toDictionary(product)
return dictionaryToJSONString(dict)
}
/// Product JSON
/// - Parameter products: Product
/// - Returns: JSON
public static func toJSONString(_ products: [Product]) -> String? {
let array = toDictionaryArray(products)
return arrayToJSONString(array)
}
// MARK: -
///
private static func productTypeToString(_ type: Product.ProductType) -> String {
switch type {
case .consumable:
return "consumable"
case .nonConsumable:
return "nonConsumable"
case .autoRenewable:
return "autoRenewable"
case .nonRenewable:
return "nonRenewable"
default:
return "unknown"
}
}
/// Dictionary JSON
internal 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
}
/// Array JSON
internal static func arrayToJSONString(_ array: [[String: Any]]) -> String? {
guard let jsonData = try? JSONSerialization.data(withJSONObject: array, options: .prettyPrinted),
let jsonString = String(data: jsonData, encoding: .utf8) else {
return nil
}
return jsonString
}
}