根据这份文档Complete Transaction,一旦成功购买StoreKit交易,我应该将收据发送到我的后端。但是经过一些测试,收据在第一时间为零,有时必须经过3分钟才能使收据停止为零。
public func fetchReceipt() -> String? {
if let receiptURL = Bundle.main.appStoreReceiptURL,
let data = try? Data(contentsOf: receiptURL) {
let data64 = data.base64EncodedString()
return data64
} else {
return nil
}
}
private func processTransactions(_ queue: SKPaymentQueue,
updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchased:
complete(transaction: transaction)
case .failed:
fail(transaction: transaction)
case .restored:
restore(transaction: transaction)
case .deferred:
print("IAPHelper: deferred...")
case .purchasing:
print("IAPHelper: purchasing...")
default:
break
}
}
}
private func complete(transaction: SKPaymentTransaction) {
SKPaymentQueue.default().finishTransaction(transaction)
if let receipt = fetchReceipt() {
deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
} else {
print("receipt is nil")
}
}