为什么不续订订阅,显示“此应用内购买已被购买。它将免费恢复。”,再次购买商品时?

时间:2018-03-26 13:50:47

标签: ios in-app-purchase non-renewing-subscription

我正在我的应用中实施不可再生购买。我仍然在沙箱模式下使用。在我订购该产品后,当我再次尝试订阅相同的产品时,它会给我一个警告,说“此应用内购买已经被购买。它将免费恢复。'。我不知道如何让用户再次订阅。

如何在同一设备上处理多个用户?如果一个用户已经为订阅付费而另一个用户登录到我的应用程序的同一设备,则他/她不应该如上所述获得警报。

Code : 

import StoreKit
class className: SKProductsRequestDelegate
{
    var productIDs: Array<String?> = []
    var productsArray: Array<SKProduct?> = []

   override func viewDidLoad(){
       // product IDs for in-app purchase products
        productIDs.append(“monthly_subscription_id”) // Monthly
        productIDs.append(“yearly_subscription_id”) // Year
        requestProductInfo()
        SKPaymentQueue.default().add(self)
     }
     func requestProductInfo() {
            if SKPaymentQueue.canMakePayments() {
             let productIdentifiers = NSSet(array: productIDs)
                let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers as    Set<NSObject> as! Set<String>)
                productRequest.delegate = self
             productRequest.start()
         }
            else {
                print("Cannot perform In App Purchases.")
              }
    }   
    // MARK: SKProductsRequestDelegate method implementation
    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        if response.products.count != 0 {
            for product in response.products {
                productsArray.append(product )
            }
        }
        else {
            print("There are no products.")
        }
        if response.invalidProductIdentifiers.count != 0 {
            print(response.invalidProductIdentifiers.description)
        }
    }
// MARK: Buy Subscription button action
@IBAction func btn_purchase_Action(_ sender: Any){
        let payment = SKPayment(product: self.productsArray[productIndex]!)
        SKPaymentQueue.default().add(payment)
        self.transactionInProgress = true
    }
}

// MARK: - SKPaymentTransactionObserver
extension className: SKPaymentTransactionObserver {
    public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]){
        for transaction in transactions {
            switch (transaction.transactionState) {
            case .purchased:
                complete(transaction: transaction)
                break
            case .failed:
                fail(transaction: transaction)
                break
            case .restored:
                restore(transaction: transaction)
                break
            case .deferred:
                break
            case .purchasing:
                break
            }
        }
    }
    private func complete(transaction: SKPaymentTransaction){
        print("complete...")
        SKPaymentQueue.default().finishTransaction(transaction)
    }
    private func restore(transaction: SKPaymentTransaction){
        guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }
        print("restore... \(productIdentifier)")
        SKPaymentQueue.default().finishTransaction(transaction)
    }
    private func fail(transaction: SKPaymentTransaction){
        print("fail...")
        if let transactionError = transaction.error as? NSError {
            if transactionError.code != SKError.paymentCancelled.rawValue {
                print("Transaction Error: \(transaction.error?.localizedDescription)")
            }
        }
        SKPaymentQueue.default().finishTransaction(transaction)
    }
}

我可以看到弹出窗口说应用内购买成功,但是当我成功完成应用内购买过程时,不会调用“updatedTransaction”功能。 第一次应用内购买已完成,但当我尝试再次购买相同的产品时,它会显示已购买产品的警报,并且可以免费恢复。

1 个答案:

答案 0 :(得分:0)

从您的代码看,您的事务观察器看起来像是一个视图控制器。

如果在处理付款交易之前取消了视图控制器,那么您将无法完成交易。

您的付款队列观察者应该是一个应用程序启动时实例化的对象,并在应用程序的生命周期内保留在内存中。

didFinishLaunching中创建付款队列观察员并在您的应用委托中保留对其的引用是您可以使用的一种方法。