Skip to content

Instantly share code, notes, and snippets.

@ren6
Created June 17, 2019 14:10
Show Gist options
  • Save ren6/f52d2ea4e0bf4f50c9d833d01d7ed955 to your computer and use it in GitHub Desktop.
Save ren6/f52d2ea4e0bf4f50c9d833d01d7ed955 to your computer and use it in GitHub Desktop.
Auto-renewable subscriptions transaction observer
extension IAPManager: SKPaymentTransactionObserver {
public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch (transaction.transactionState) {
case .purchased:
SKPaymentQueue.default().finishTransaction(transaction)
notifyIsPurchased(transaction: transaction)
break
case .failed:
SKPaymentQueue.default().finishTransaction(transaction)
print("purchase error : \(transaction.error?.localizedDescription ?? "")")
self.failureBlock?(transaction.error)
cleanUp()
break
case .restored:
SKPaymentQueue.default().finishTransaction(transaction)
notifyIsPurchased(transaction: transaction)
break
case .deferred, .purchasing:
break
default:
break
}
}
}
private func notifyIsPurchased(transaction: SKPaymentTransaction) {
refreshSubscriptionsStatus(callback: {
self.successBlock?()
self.cleanUp()
}) { (error) in
// couldn't verify receipt
self.failureBlock?(error)
self.cleanUp()
}
}
func cleanUp(){
self.successBlock = nil
self.failureBlock = nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment