264 lines
8.2 KiB
Swift
264 lines
8.2 KiB
Swift
//
|
||
// PIIAPPayment.swift
|
||
// YuMi
|
||
//
|
||
// Created by duoban on 2023/9/14.
|
||
//
|
||
|
||
import UIKit
|
||
import StoreKit
|
||
|
||
@available(iOS 15.0, *)
|
||
typealias Transaction = StoreKit.Transaction
|
||
@available(iOS 15.0, *)
|
||
typealias RenewalInfo = StoreKit.Product.SubscriptionInfo.RenewalInfo
|
||
@available(iOS 15.0, *)
|
||
typealias RenewalState = StoreKit.Product.SubscriptionInfo.RenewalState
|
||
|
||
|
||
|
||
enum PIStoreError: Error {
|
||
|
||
|
||
// 错误回调枚举
|
||
case failedVerification
|
||
case noProduct
|
||
|
||
}
|
||
|
||
@objc public enum StoreState: Int64 { // 支付状态
|
||
case start // 开始
|
||
case pay // 进行苹果支付
|
||
case verifiedServer // 服务器校验
|
||
case userCancelled // 用户取消
|
||
case pending // 等待(家庭用户才有的状态)
|
||
case unowned
|
||
case noProduct //没有商品
|
||
case failedVerification //验证失败
|
||
}
|
||
|
||
|
||
|
||
@available(iOS 15.0, *)
|
||
public class PIIAPPayment: NSObject {
|
||
public typealias KStateBlock = (_ state :StoreState,_ param:Dictionary<String,Any>?) ->()
|
||
@objc public var stateBlock: KStateBlock! // 状态回调
|
||
|
||
var updateListenerTask: Task<Void, Error>? = nil // 支付事件监听
|
||
|
||
var transactionMap :[String:Transaction] // 用于完成Id的缓存map
|
||
|
||
var name: String = "iosStore" // 单例的写法
|
||
|
||
@objc public static let shared = {
|
||
let instance = PIIAPPayment()
|
||
return instance
|
||
}()
|
||
|
||
private override init() { // 单例需要保证private的私有性质
|
||
|
||
transactionMap = [:] // 初始化
|
||
super.init()
|
||
Task {
|
||
updateListenerTask = listenForTransactions()
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
// 退订
|
||
@objc public func refunRequest(view: UIView,transactionId:UInt64) async{
|
||
do {
|
||
if let scene = await view.window?.windowScene{
|
||
try await Transaction.beginRefundRequest(for:transactionId , in: scene)
|
||
}
|
||
|
||
}catch{
|
||
print("iap error")
|
||
}
|
||
}
|
||
|
||
|
||
// 购买某个产品
|
||
@objc public func requestBuyProduct(productId:String, uuid: String) async throws {
|
||
if(stateBlock != nil ){
|
||
stateBlock(StoreState.start,nil)
|
||
}
|
||
do {
|
||
let list:[String] = [productId]
|
||
let storeProducts = try await Product.products(for: Set.init(list))
|
||
|
||
|
||
|
||
|
||
if storeProducts.count > 0 {
|
||
try await purchase(storeProducts[0],uuid)
|
||
}else {
|
||
print("iap: no found product")
|
||
if(stateBlock != nil ){
|
||
stateBlock(StoreState.noProduct,nil)
|
||
}
|
||
throw PIStoreError.noProduct // 没有该产品
|
||
}
|
||
} catch {
|
||
print("Failed product request from the App Store server: \(error)")
|
||
if(stateBlock != nil ){
|
||
stateBlock(StoreState.noProduct,nil)
|
||
}
|
||
throw PIStoreError.noProduct // 没有该产品
|
||
}
|
||
}
|
||
|
||
// 购买
|
||
private func purchase(_ product: Product, _ uuid: String) async throws -> Transaction? {
|
||
if(stateBlock != nil ){
|
||
stateBlock(StoreState.pay,nil)
|
||
}
|
||
|
||
guard let curUUID = UUID.init(uuidString: uuid) else{
|
||
|
||
if(stateBlock != nil ){
|
||
stateBlock(StoreState.failedVerification,nil)
|
||
}
|
||
return nil
|
||
}
|
||
let getUUID = Product.PurchaseOption.appAccountToken(curUUID)
|
||
let result = try await product.purchase(options: [getUUID])
|
||
|
||
switch result {
|
||
case .success(let verification): // 用户购买完成
|
||
let transaction = try await verifiedAndFinish(verification)
|
||
return transaction
|
||
case .userCancelled: // 用户取消
|
||
if(stateBlock != nil ){
|
||
stateBlock(StoreState.userCancelled,nil)
|
||
}
|
||
return nil
|
||
case .pending: // 此次购买被挂起
|
||
if(stateBlock != nil ){
|
||
stateBlock(StoreState.pending,nil)
|
||
}
|
||
return nil
|
||
default:
|
||
if(stateBlock != nil ){
|
||
stateBlock(StoreState.unowned,nil)
|
||
}
|
||
return nil
|
||
}
|
||
}
|
||
|
||
// 校验
|
||
func checkVerified<T>(_ result: VerificationResult<T>) throws -> T {
|
||
//Check whether the JWS passes StoreKit verification.
|
||
switch result {
|
||
case .unverified:
|
||
//StoreKit parses the JWS, but it fails verification.
|
||
if(stateBlock != nil ){
|
||
stateBlock(StoreState.failedVerification,nil)
|
||
}
|
||
throw PIStoreError.failedVerification
|
||
case .verified(let safe):
|
||
//The result is verified. Return the unwrapped value.
|
||
print("iap: verified success")
|
||
return safe
|
||
}
|
||
}
|
||
|
||
// 校验&完成后传给服务器
|
||
func verifiedAndFinish(_ verification:VerificationResult<Transaction>) async throws -> Transaction?{
|
||
//Check whether the transaction is verified. If it isn't,
|
||
//this function rethrows the verification error.
|
||
let transaction = try checkVerified(verification)
|
||
// 这里将订单提交给服务器进行验证 ~~~
|
||
let transactionId = try verification.payloadValue.id
|
||
|
||
// 添加进入待完成map
|
||
let key = String(transactionId)
|
||
transactionMap[key] = transaction
|
||
await uploadServer(for: transactionId)
|
||
|
||
// 这里不触发完成,等服务器验证再触发完成逻辑
|
||
await transaction.finish()
|
||
|
||
print("iap: finish")
|
||
return transaction
|
||
}
|
||
/*All transactions:全部的购买交易订单
|
||
Latest transactions:最新的购买交易订单。(分为订阅品项和除订阅品项外的所有类型二种)
|
||
Current entitlements:当前用户有购买的权限。(全部的订阅品项、和非消耗品项)
|
||
*/
|
||
func allTransaction(transactionId:String) async {
|
||
|
||
let transactionIntId = UInt64(transactionId)
|
||
for await result in Transaction.all {
|
||
do {
|
||
let tran = try checkVerified(result)
|
||
let resultId = try result.payloadValue.id
|
||
if transactionIntId == resultId {
|
||
await tran.finish()
|
||
break
|
||
}
|
||
} catch let error {
|
||
|
||
print("error:----\(error)")
|
||
}
|
||
|
||
}
|
||
|
||
|
||
//Transaction.latest(for: "pid")
|
||
|
||
}
|
||
// 事件完成处理
|
||
|
||
@objc public func transactionFinish(transaction:String) async{
|
||
if(transactionMap[transaction] != nil){
|
||
await transactionMap[transaction]!.finish()
|
||
transactionMap.removeValue(forKey: transaction)
|
||
print("transactionFinish end")
|
||
}else {
|
||
await allTransaction(transactionId: transaction)
|
||
}
|
||
}
|
||
|
||
@MainActor
|
||
func uploadServer(for transactionId:UInt64) async {
|
||
let dic :Dictionary<String,Any> = ["transactionId":transactionId]
|
||
if(stateBlock != nil ){
|
||
stateBlock(StoreState.verifiedServer,dic)
|
||
}
|
||
}
|
||
|
||
// 支付监听事件
|
||
func listenForTransactions() -> Task<Void, Error> {
|
||
return Task.detached {
|
||
//Iterate through any transactions that don't come from a direct call to `purchase()`.
|
||
// 修改update 为 unfinished?
|
||
for await result in Transaction.updates { //会导致二次校验?
|
||
do {
|
||
print("iap: updates")
|
||
print("result:\(result)")
|
||
try await self.verifiedAndFinish(result)
|
||
} catch {
|
||
//StoreKit has a transaction that fails verification. Don't deliver content to the user.
|
||
print("Transaction failed verification")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//获取推广内购商品
|
||
func Promotion() async -> [SKProduct]?{
|
||
let promotion = SKProductStorePromotionController()
|
||
|
||
let prodicts = try? await promotion.promotionOrder()
|
||
return prodicts
|
||
}
|
||
// 销毁调用
|
||
deinit {
|
||
updateListenerTask?.cancel()
|
||
}
|
||
|
||
|
||
}
|