refactor: Improve log in in-app purchase manager
This commit is contained in:
@@ -83,11 +83,9 @@ extension IAPHelper {
|
||||
}
|
||||
|
||||
public func buyProduct(_ product: SKProduct) {
|
||||
|
||||
print("Buying \(product.productIdentifier)...")
|
||||
print("[IAPHelper] Buying product \(product.productIdentifier)...")
|
||||
let payment = SKPayment(product: product)
|
||||
SKPaymentQueue.default().add(payment)
|
||||
|
||||
}
|
||||
|
||||
public func isProductPurchased(_ productIdentifier: ProductIdentifier) -> Bool {
|
||||
@@ -108,23 +106,17 @@ extension IAPHelper {
|
||||
extension IAPHelper: SKProductsRequestDelegate {
|
||||
|
||||
public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
|
||||
print("[IAPHelper] Loaded list of products...")
|
||||
|
||||
let products = response.products
|
||||
products.forEach { (p) in
|
||||
print("[IAPHelper] Found product: \(p.productIdentifier) \(p.localizedTitle) \(p.price.floatValue)")
|
||||
}
|
||||
|
||||
print("[IAPHelper] Products loaded (count: \(response.products.count))")
|
||||
|
||||
// this protocol method is not guaranteed to be dispatched on main thread
|
||||
DispatchQueue.main.async {
|
||||
self.productsRequestCompletionHandler?(true, products)
|
||||
self.productsRequestCompletionHandler?(true, response.products)
|
||||
self.clearRequestAndHandler()
|
||||
}
|
||||
}
|
||||
|
||||
public func request(_ request: SKRequest, didFailWithError error: Error) {
|
||||
print("[IAPHelper] Failed to load list of products.")
|
||||
print("[IAPHelper] Error: \(error.localizedDescription)")
|
||||
print("[IAPHelper] Failed to load list of products (error: \(error.localizedDescription))")
|
||||
|
||||
// this protocol method is not guaranteed to be dispatched on main thread
|
||||
DispatchQueue.main.async {
|
||||
@@ -144,17 +136,16 @@ extension IAPHelper: SKProductsRequestDelegate {
|
||||
extension IAPHelper: SKPaymentTransactionObserver {
|
||||
|
||||
public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
|
||||
print("[IAPHelper] Updated transactions (count: \(transactions.count))")
|
||||
|
||||
for transaction in transactions {
|
||||
switch (transaction.transactionState) {
|
||||
case .purchased:
|
||||
complete(transaction: transaction)
|
||||
break
|
||||
handlePurchased(transaction: transaction)
|
||||
case .failed:
|
||||
fail(transaction: transaction)
|
||||
break
|
||||
handleFailed(transaction: transaction)
|
||||
case .restored:
|
||||
restore(transaction: transaction)
|
||||
break
|
||||
handleRestored(transaction: transaction)
|
||||
case .deferred:
|
||||
break
|
||||
case .purchasing:
|
||||
@@ -165,31 +156,31 @@ extension IAPHelper: SKPaymentTransactionObserver {
|
||||
}
|
||||
}
|
||||
|
||||
private func complete(transaction: SKPaymentTransaction) {
|
||||
|
||||
print("complete...")
|
||||
private func handlePurchased(transaction: SKPaymentTransaction) {
|
||||
print("[IAPHelper] Handle purchased transaction (product: \(transaction.payment.productIdentifier))")
|
||||
deliverPurchaseNotificationFor(identifier: transaction.payment.productIdentifier)
|
||||
SKPaymentQueue.default().finishTransaction(transaction)
|
||||
|
||||
}
|
||||
|
||||
private func restore(transaction: SKPaymentTransaction) {
|
||||
guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }
|
||||
private func handleRestored(transaction: SKPaymentTransaction) {
|
||||
guard let productIdentifier = transaction.original?.payment.productIdentifier else {
|
||||
print("[IAPHelper] Handle restored transaction falied, cannot retrieve productIdentifier")
|
||||
return
|
||||
}
|
||||
|
||||
print("restore... \(productIdentifier)")
|
||||
print("[IAPHelper] Handle restored transaction (product: \(productIdentifier))")
|
||||
deliverPurchaseNotificationFor(identifier: productIdentifier)
|
||||
SKPaymentQueue.default().finishTransaction(transaction)
|
||||
}
|
||||
|
||||
private func fail(transaction: SKPaymentTransaction) {
|
||||
|
||||
print("fail...")
|
||||
private func handleFailed(transaction: SKPaymentTransaction) {
|
||||
print("[IAPHelper] Handle failed transaction (product: \(transaction.payment.productIdentifier))")
|
||||
if let transactionError = transaction.error as NSError?,
|
||||
let localizedDescription = transaction.error?.localizedDescription,
|
||||
transactionError.code != SKError.paymentCancelled.rawValue {
|
||||
print("Transaction Error: \(localizedDescription)")
|
||||
|
||||
print("[IAPHelper] Handle failed transaction error: \(localizedDescription)")
|
||||
}
|
||||
|
||||
NotificationCenter.default.post(name: .IAPHelperPurchaseNotificationFail, object: nil)
|
||||
SKPaymentQueue.default().finishTransaction(transaction)
|
||||
}
|
||||
@@ -201,6 +192,4 @@ extension IAPHelper: SKPaymentTransactionObserver {
|
||||
UserDefaults.standard.set(true, forKey: identifier)
|
||||
NotificationCenter.default.post(name: .IAPHelperPurchaseNotification, object: identifier)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user