Created
June 17, 2019 14:10
-
-
Save ren6/f52d2ea4e0bf4f50c9d833d01d7ed955 to your computer and use it in GitHub Desktop.
Auto-renewable subscriptions transaction observer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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