0

我正在使用 pod“TPInAppReceipt”来验证收据。我已经通过使用 Xcode 商店套件配置在我的项目中实现了自动更新订阅。需要帮助才能知道为什么我在卸载和重新安装应用程序时收到空收据。如何获取所有以前的购买和订阅状态。

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction in transactions {
        switch transaction.transactionState{
        case .purchasing:
            print("user purchasing")
        case .purchased:
            SKPaymentQueue.default().finishTransaction(transaction)
            
            InAppReceipt.refresh { [weak self](error) in
                if let err = error
                {
                    print("err....\(err)")
                } else {
                    print("fetching new receipt...")
                    self?.initializeReceipt()
                }
            }
            
            
            print("purchased")
        case .failed:
            SKPaymentQueue.default().finishTransaction(transaction)
            print("failed")
        case .restored:
            print("restored")
        case .deferred:
            print("deferred")
        default:
            break
        }
    }
}

public func initializeReceipt(){
    do {
        /// Initialize receipt
        let receipt = try InAppReceipt.localReceipt()
       
        /// Base64 Encoded Receipt
        //let base64Receipt = receipt.base64
          
        /// Check whether receipt contains any purchases
        let hasPurchases = receipt.hasPurchases

        /// All auto renewable `InAppPurchase`s,
        let purchases: [InAppPurchase] = receipt.autoRenewablePurchases

        /// all ACTIVE auto renewable `InAppPurchase`s,
        let activePurchases: [InAppPurchase] = receipt.activeAutoRenewableSubscriptionPurchases
        let activSub = receipt.hasActiveAutoRenewablePurchases
        print("1. hasPurchases..=> \(hasPurchases) ")
        print("2. purchases..=> \(purchases)")
        print("3. activePurchases ..=> \(activePurchases)")
        print("4. activeSub ..=> \(activSub)")
       // print("4. base64Receipt..=> \(base64Receipt)")
        do {
            try receipt.validate()
            print("successfully validated...")
        } catch IARError.validationFailed(reason: .hashValidation) {
            // Do smth
            print("error...1")
        } catch IARError.validationFailed(reason: .bundleIdentifierVerification) {
            // Do smth
            print("error...2")
        } catch IARError.validationFailed(reason: .signatureValidation) {
            // Do smth
            print("error...3")
        } catch {
            // Do smth
            print("error...4")
        }
        
        
    } catch {
        print(error)
    }
}

在 Appdelegate.swift 中

InAppReceipt.refresh { (error) in
        if let err = error
        {
            print("err....\(err)")
        } else {
            print("fetching new receipt...")
            IAPManager.shared.initializeReceipt()
            
        }
    }

输出:- 重新安装应用程序后

获取新收据...

  1. hasPurchases..=> 假
  2. 购买..=> []
  3. activePurchases ..=> []
  4. activeSub ..=> 错误错误...3
4

0 回答 0