My app has in-app purchase built in (I followed this tutorial), the purchase functionality works. However, when I redeem the promo code in App Store for one of the in-app purchase products, my app doesn't response to it. Even the App Store says the product has been successfully redeemed, my app doesn't response to it.
Has anyone who has in-app purchase tested if your app can process the promo code? Would you mind share the solution?
I started with this:
override func viewDidLoad() {
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive(notification:)),
name: NSNotification.Name.UIApplicationDidBecomeActive,
object: nil
)
}
func applicationDidBecomeActive(notification: NSNotification){
let store = IAPHealper()
//what needs to be put here?
}
extension IAPHelper: SKPaymentTransactionObserver {
public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch (transaction.transactionState) {
case .purchased:
completeTransaction(transaction)
break
case .failed:
failedTransaction(transaction)
break
case .restored:
restoreTransaction(transaction)
break
case .deferred:
break
case .purchasing:
break
}
}
}
fileprivate func completeTransaction(_ transaction: SKPaymentTransaction) {
print("completeTransaction...")
deliverPurchaseNotificatioForIdentifier(transaction.payment.productIdentifier)
defaultQueue.finishTransaction(transaction)
purchaseCompletionHandler?(true, transaction)
}
fileprivate func restoreTransaction(_ transaction: SKPaymentTransaction) {
guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }
print("restoreTransaction... \(productIdentifier)")
deliverPurchaseNotificatioForIdentifier(productIdentifier)
defaultQueue.finishTransaction(transaction)
}
fileprivate func failedTransaction(_ transaction: SKPaymentTransaction) {
print("failedTransaction...")
if transaction.error!._code != SKError.paymentCancelled.rawValue {
print("Transaction Error: \(String(describing: transaction.error?.localizedDescription))")
purchaseCompletionHandler?(false, transaction)
}
defaultQueue.finishTransaction(transaction)
}
fileprivate func deliverPurchaseNotificatioForIdentifier(_ identifier: String?) {
guard let identifier = identifier else { return }
purchasedProductIdentifiers.insert(identifier)
//NSNotificationCenter.defaultCenter().postNotificationName(IAPHelper.IAPHelperPurchaseNotification, object: identifier)
}
public func paymentQueue(_ queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]){
print("Removed from queue.")
print(transactions)
}
}
Thanks.