gpt4 book ai didi

iOS IAP paymentQueue :updatedTransactions

转载 作者:行者123 更新时间:2023-11-29 00:08:11 26 4
gpt4 key购买 nike

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

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

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

最佳答案

迭代交易循环并检查每笔交易。

    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?

关于iOS IAP paymentQueue :updatedTransactions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47388711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com