【问题标题】:iOS IAP paymentQueue:updatedTransactionsiOS IAP paymentQueue:updatedTransactions
【发布时间】:2018-05-03 11:27:14
【问题描述】:

SKPaymentTransactionObserver.paymentQueue:updatedTransactions 返回一个交易数组。当我付款时,我如何知道我的付款是哪笔交易?我付款时它总是返回一笔交易吗?

同时,在恢复交易时也会调用这个观察者函数。那么,处理 updatedTransactions 的最佳实践是什么?

顺便说一句,我的订阅产品是自动续订。

【问题讨论】:

  • 只需根据交易状态处理所有交易

标签: ios transactions in-app-purchase payment auto-renewable


【解决方案1】:

遍历交易循环并检查每个交易。

    public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch (transaction.transactionState) {
            case .purchased:
                completeTransaction(transaction: transaction)
                break
            case .failed:
                failedTransaction(transaction: transaction)
                break
            case .restored:
                restoreTransaction(transaction: transaction)
                break
            case .deferred:
                // TODO show user that is waiting for approval

                break
            case .purchasing:
                break
            }
        }
    }

    private func completeTransaction(transaction: SKPaymentTransaction) {

        print("completeTransaction...")

        deliverPurchaseForIdentifier(identifier: transaction.payment.productIdentifier)
        SKPaymentQueue.default().finishTransaction(transaction)

    }

    private func restoreTransaction(transaction: SKPaymentTransaction) {


        guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }

        print("restoreTransaction... \(productIdentifier)")


        deliverPurchaseForIdentifier(identifier: productIdentifier)
        SKPaymentQueue.default().finishTransaction(transaction)
    }

    private func failedTransaction(transaction: SKPaymentTransaction) {

        if let error = transaction.error as NSError? {
            if error.domain == SKErrorDomain {
                // handle all possible errors
                switch (error.code) {
                case SKError.unknown.rawValue:
                    print("Unknown error")

                    BaseViewController.currentViewController?.Alert(title: MFErrors.purchaseFaild.messgae.title, msg: MFErrors.purchaseFaild.messgae.body)

                case SKError.clientInvalid.rawValue:
                    print("client is not allowed to issue the request")

                    BaseViewController.currentViewController?.Alert(title: MFErrors.accountNotAllowedToMakePurchase.messgae.title, msg: MFErrors.accountNotAllowedToMakePurchase.messgae.body)

                case SKError.paymentCancelled.rawValue:
                    print("user cancelled the request")

                case SKError.paymentInvalid.rawValue:
                    print("purchase identifier was invalid")

                case SKError.paymentNotAllowed.rawValue:
                    print("this device is not allowed to make the payment")
                    BaseViewController.currentViewController?.Alert(title: MFErrors.purchaseFaild.messgae.title, msg: MFErrors.purchaseFaild.messgae.body)
                default:
                    break;
                }
            }

            ProgressViewManager.shared.hide()
        }

        SKPaymentQueue.default().finishTransaction(transaction)
    }

    private func deliverPurchaseForIdentifier(identifier: String?) {

        guard let identifier = identifier else { return }

       //Check if this transactions is a subscription
       //SubscriptionsProductIdentifiers is an array of subscriptions product ids you sent to the app store to get SKProducts

        //If subscription
        if SubscriptionsProductIdentifiers.contains(identifier) {


        }else{
           //If non-consumable, consumables etc... 

        }


    }

这是我之前回答中的完整商店经理: How to handle shouldAddStorePayment for In-App Purchases in iOS 11?

【讨论】:

  • 如果所有这些交易都是相同的产品,我怎么知道哪一个是最新的?还有,哪个是我刚才支付的款项?
  • 首先,您可以保存用户决定购买的产品 ID,然后通过将保存的产品 ID 与上例中的标识符进行比较,您将知道哪个产品,但这还不够,您仍然需要将收据发送到 Apple 服务器进行验证,以将收据解码为 JSON,并获得每个订阅的所有信息,例如购买日期和到期日期等。
  • 谢谢!它节省了我的时间。
猜你喜欢
  • 2017-03-26
  • 2016-06-24
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
  • 2016-10-03
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多