1

我正在我的应用程序中实施不可再生购买。我仍在沙盒模式下使用。订阅该产品后,当我再次尝试订阅同一产品时,它会提示我“此应用内购买已被购买”。它将免费恢复。我不知道我应该如何让用户再次订阅。

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

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”函数。第一次应用内购买已完成,但当我再次尝试购买相同的产品时,它会显示产品已购买并且可以免费恢复的警报。

4

1 回答 1

0

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

如果在处理付款交易之前关闭视图控制器,那么您将没有机会完成交易。

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

在您的应用程序委托中创建支付队列观察者didFinishLaunching并持有对它的引用是您可以使用的一种方法。

于 2018-03-27T07:01:11.930 回答