Мы сталкиваемся с серьезными проблемами с покупками в приложении в нашем приложении.
Мы предлагаем 3 IAP: подписка с автоматическим продлением 3M, подписка с автоматическим продлением 1Y, одноразовая покупка без расхода (доступ к LifeTime))
В нашем случае 70-80% транзакций завершаются неудачно, и мы в основном получаем код SKError = 0 или code = 2 - Невозможно подключиться к iTunes Store.Согласно документации SKError, это неизвестная ошибка (код 0) или отмененная транзакция (ошибка 2).
Иногда покупка несколько раз не удалась для одного и того же пользователя, поэтому очень трудно поверить, что пользователь намеренно отменяет транзакцию для одного и того же продукта 3или 4 раза подряд.
Это происходит независимо от версии iOS, модели устройства, версии нашего приложения.Ниже приведен код, используемый для получения продуктов и совершения транзакции.
Мы проверили несколько потоков на одну и ту же проблему, но не нашли решения.
Мы не предлагаем никаких рекламных акций,Идентификаторы продукта действительны ... Некоторые пользователи могут совершать покупки без проблем.
Есть идеи?
import Foundation
import SwiftyStoreKit
import StoreKit
final class IAPService: NSObject {
static let shared = IAPService()
public var isSubscriptionAvailable = false
private var identifiers = ["product_x_id",
"product_y_id",
"product_z_id"]
var products: [SKProduct] = []
var purchaseProducts: [PurchaseProduct] = []
private var successBlock: ((String?,String?)->())? //product Id, receipt
private var errorBlock: ((String)->())?
private var productsRequest: SKProductsRequest?
private var productsRequestCompletionHandler: (()->())?
override init() {
super.init()
SKPaymentQueue.default().add(self)
self.loadProducts()
}
func loadProducts(completion: (()->())? = nil) {
productsRequest?.cancel()
productsRequestCompletionHandler = completion
productsRequest = SKProductsRequest(productIdentifiers: Set(identifiers))
productsRequest?.delegate = self
productsRequest?.start()
}
func purchaseProduct(identifier: String, onSuccess: ((String?,String?) -> ())?, onError: ((String?) -> ())?) {
guard products.count > 0 else {
loadProducts {
self.purchaseProduct(identifier: identifier, onSuccess: onSuccess, onError: onError)
}
return
}
guard let product = self.products.first(where: { (skProduct) -> Bool in
return skProduct.productIdentifier == identifier
}) else {
onError?("IAP error: cannot find product id")
return
}
clearHandlers()
self.successBlock = onSuccess
self.errorBlock = onError
print("Buying \(product.productIdentifier)...")
let payment = SKPayment(product: product)
SKPaymentQueue.default().add(payment)
}
public func restorePurchases(onSuccess: ((String?,String?) -> ())?, onError: ((String?) -> ())?) {
clearHandlers()
self.successBlock = onSuccess
self.errorBlock = onError
SKPaymentQueue.default().restoreCompletedTransactions()
}
public func fetchReceipt(productId: String) {
SwiftyStoreKit.fetchReceipt(forceRefresh: false) { (result) in
switch result {
case .success(let receiptData):
self.successBlock?(productId, receiptData.base64EncodedString(options: []))
self.clearHandlers()
break
case .error(let error):
print("Receipt verification failed: \(error.localizedDescription)")
self.errorBlock?(error.localizedDescription)
self.clearHandlers()
break
}
}
}
private func clearHandlers() {
successBlock = nil
errorBlock = nil
productsRequestCompletionHandler = nil
productsRequest = nil
}
}
extension IAPService: SKProductsRequestDelegate {
public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print("Loaded list of products...")
let skProducts = response.products
skProducts.forEach { (skProduct) in
products.append(skProduct)
}
productsRequestCompletionHandler?()
clearHandlers()
}
public func request(_ request: SKRequest, didFailWithError error: Error) {
print("Failed to load list of products: \(error.localizedDescription)")
productsRequestCompletionHandler?()
clearHandlers()
}
}
extension IAPService: 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...")
deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
}
private func restore(transaction: SKPaymentTransaction) {
guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }
print("restore... \(productIdentifier)")
deliverPurchaseNotificationFor(identifier: productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
}
private func fail(transaction: SKPaymentTransaction) {
print("fail...")
var failureReason: String = ""
if let skError = transaction.error as? SKError {
switch skError.code { // https://developer.apple.com/reference/storekit/skerror.code
case .unknown:
failureReason = "Unknown or unexpected error occurred"
break
case .paymentCancelled:
failureReason = "Payment cancelled by user"
break
case .clientInvalid:
failureReason = "Invalid Client"
break
case .paymentInvalid:
failureReason = "Invalid Payment"
break
case .paymentNotAllowed:
failureReason = "Payment not allowed"
break
case .cloudServiceNetworkConnectionFailed:
failureReason = "Cloud service network connection failed"
break
case .cloudServicePermissionDenied:
failureReason = "Cloud service permission denied"
break
case .storeProductNotAvailable:
failureReason = "Store product not available"
break
case .cloudServiceRevoked:
failureReason = "Cloud service revoked"
break
case .privacyAcknowledgementRequired:
failureReason = "Privacy Acknowledgement Required"
break
case .unauthorizedRequestData:
failureReason = "Unauthorized Request Data"
break
case .invalidOfferIdentifier:
failureReason = "Invalid offer identifier"
break
case .invalidSignature:
failureReason = "Invalid signature"
break
case .missingOfferParams:
failureReason = "Missing offer params"
break
case .invalidOfferPrice:
failureReason = "Invalid offer price"
break
}
failureReason += " code: \(skError.code.rawValue)"
}
else if let isCancelledError = transaction.error?.isCancelledError, isCancelledError == true {
failureReason = "isCancelledError"
}
else {
failureReason = "\(transaction.error.debugDescription)"
}
SKPaymentQueue.default().finishTransaction(transaction)
errorBlock?(failureReason)
self.clearHandlers()
}
private func deliverPurchaseNotificationFor(identifier: String?) {
guard let identifier = identifier else { return }
fetchReceipt(productId: identifier)
}
}