Restore Purchases function - Swift - ios

In my app, the user can make two different purchases.
Here's my paymentQueue function :
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("Received Payment Transaction Response from Apple")
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction {
switch trans.transactionState {
case .Purchased, . Restored:
print("Product Purchased / Restored")
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
// TO DO
if selectedProduct == "product1" {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "product1Purchased")
} else if selectedProduct == "product2" {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "product2Purchased")
}
case .Failed:
print("Purchased Failed")
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
break
default:
break
}
}
}
}
I created a variable named selectedProduct to detect which product the user select. If he click on the button to buy the first product, the variable selectedProduct hold the value "product1".
The problem is when the user click the Restore Purchases button, the app check if the selected product is "product1" or "product2", but selectedProduct has no value if the user click on the Restore Purchases button, so nothing happen.
How can I do please?

Do not use a variable such as selectedProduct to determine which product was purchased or restored. Look inside the SKPaymentTransaction for the needed information.
case .Purchased, . Restored:
print("Product Purchased / Restored")
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
if trans.payment.productIdentifier == "product1" {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "product1Purchased")
} else if trans.payment.productIdentifier == "product2" {
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "product2Purchased")
}
Adjust as needed for your actual product ids.

Related

inApp purchase payment in ios

InApp purchase payment in ios
i need to subscribe a plan using InApp purchase. for payment but i am showing an alert from sandbox environment like "would you like to buy ...." i need to add my card details and show some sample transaction happening. How to redirect to payment and add card details? how the InApp purchase payment can be done in
real time environment.
// MARK: - IAP payment queue
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction:AnyObject in transactions {
if let trans = transaction as? SKPaymentTransaction {
switch trans.transactionState {
case .purchased:
hideLoader()
if let paymentTransaction = transaction as? SKPaymentTransaction {
SKPaymentQueue.default().finishTransaction(paymentTransaction)
}
if productID == PRODUCT_ID {
UserDefaults.standard.set(true, forKey: "isPurchased")
lblPurchaseDone.text = "Pro version PURCHASED!"
lblPurchaseDone.isHidden = false
btnPurchase.isHidden = true
btnRestore.isHidden = true
self.present(Utilities().showAlertContrller(title: "Purchase Success", message: "You've successfully purchased"), animated: true, completion: nil)
}
case .failed:
hideLoader()
if trans.error != nil {
self.present(Utilities().showAlertContrller(title: "Purchase failed!", message: trans.error!.localizedDescription), animated: true, completion: nil)
print(trans.error!)
}
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
case .restored:
print("restored")
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
default: break
}
}
}
}

iOS IAP paymentQueue:updatedTransactions

The SKPaymentTransactionObserver.paymentQueue:updatedTransactions returns a array of transactions. When I make a payment, how do I know which transaction is the payment I made? Does it always return one transaction when I make a payment?
Meanwhile, this observer function is also called when restoring transactions. So, what is the best practice to handle the updatedTransactions?
BTW, my subscription product is a auto-renew subscription.
iterate through the loop of the transactions and check for each transaction.
public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch (transaction.transactionState) {
case .purchased:
completeTransaction(transaction: transaction)
break
case .failed:
failedTransaction(transaction: transaction)
break
case .restored:
restoreTransaction(transaction: transaction)
break
case .deferred:
// TODO show user that is waiting for approval
break
case .purchasing:
break
}
}
}
private func completeTransaction(transaction: SKPaymentTransaction) {
print("completeTransaction...")
deliverPurchaseForIdentifier(identifier: transaction.payment.productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
}
private func restoreTransaction(transaction: SKPaymentTransaction) {
guard let productIdentifier = transaction.original?.payment.productIdentifier else { return }
print("restoreTransaction... \(productIdentifier)")
deliverPurchaseForIdentifier(identifier: productIdentifier)
SKPaymentQueue.default().finishTransaction(transaction)
}
private func failedTransaction(transaction: SKPaymentTransaction) {
if let error = transaction.error as NSError? {
if error.domain == SKErrorDomain {
// handle all possible errors
switch (error.code) {
case SKError.unknown.rawValue:
print("Unknown error")
BaseViewController.currentViewController?.Alert(title: MFErrors.purchaseFaild.messgae.title, msg: MFErrors.purchaseFaild.messgae.body)
case SKError.clientInvalid.rawValue:
print("client is not allowed to issue the request")
BaseViewController.currentViewController?.Alert(title: MFErrors.accountNotAllowedToMakePurchase.messgae.title, msg: MFErrors.accountNotAllowedToMakePurchase.messgae.body)
case SKError.paymentCancelled.rawValue:
print("user cancelled the request")
case SKError.paymentInvalid.rawValue:
print("purchase identifier was invalid")
case SKError.paymentNotAllowed.rawValue:
print("this device is not allowed to make the payment")
BaseViewController.currentViewController?.Alert(title: MFErrors.purchaseFaild.messgae.title, msg: MFErrors.purchaseFaild.messgae.body)
default:
break;
}
}
ProgressViewManager.shared.hide()
}
SKPaymentQueue.default().finishTransaction(transaction)
}
private func deliverPurchaseForIdentifier(identifier: String?) {
guard let identifier = identifier else { return }
//Check if this transactions is a subscription
//SubscriptionsProductIdentifiers is an array of subscriptions product ids you sent to the app store to get SKProducts
//If subscription
if SubscriptionsProductIdentifiers.contains(identifier) {
}else{
//If non-consumable, consumables etc...
}
}
here's complete Store Manager in my previous answer:
How to handle shouldAddStorePayment for In-App Purchases in iOS 11?

Hidden Adjacent CollectionViewCells are still active?

I have a collectionView with paging enabled in the storyboard. Each collection view cell is the full width of the screen
return CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)
I am trying to set up IAP and have encountered an Issue. Each cell has a button that checks for a product_ID associated with the row when tapped.
print("About to fetch the product...")
// Can make payments
if (SKPaymentQueue.canMakePayments())
{
let productID:NSSet! = NSSet(object: self.product_ID)
let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
productsRequest.delegate = self
productsRequest.start()
print("Fetching Products")
}else{
print("Can't make purchases")
UIAlertView(title: "Purchases Disabled",
message: "Purchases are disabled on your device! Enable Purchases in the Restriction Settings on your device.",
delegate: nil, cancelButtonTitle: "OK").show()
}
payment queue
func paymentQueue(_ queue: SKPaymentQueue,
updatedTransactions transactions: [SKPaymentTransaction])
{
print("Received Payment Transaction Response from Apple")
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {
case .purchased:
print("Product Purchased")
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
// Handle the purchase
print(product_ID)
UserDefaults.standard.set(true, forKey: product_ID)
setTitle = "Apply Theme"
case .failed:
print("Purchased Failed")
setTitle = "4.99"
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
case .restored:
print("Already Purchased")
//SKPaymentQueue.default().restoreCompletedTransactions()
// Handle the purchase
UserDefaults.standard.set(true, forKey: product_ID)
setTitle = "Apply Theme"
default:
break
}
purchaseButton.setTitle(setTitle, for: .normal)
}
}
}
`
The issue I have is that when I press the button, it doesn't return 1 productID, it returns 3. It processes the payment for the cell I select and every adjacent cell.. It seems that even when the other cells are off screen they are still active. They don't seem to be removed by the system. Is there a solution to this? I've never run into an issue like this before.

IAPs are being restoring when they have not yet been purchased yet

When the user calls restorePurchases(), the non-consumable com.premium is restored even thought they do not own it.
Here are the functions that are responsible for the restoring purchases and purchasing IAPs. This is only an issue with non-consumable IAPs.
There is no issue with purchasing. And if the user attempts to purchase an IAP that they already have, it is simply restored. Thank you for looking in to this.
func restorePurchases() {
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()
}
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
print("transactions restored")
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction
let prodID = t.payment.productIdentifier as String
print("starting restoring")
switch prodID {
case "com.premium":
print("restoring ads")
removeAds()
case "com.cash":
print("we dont restore coins")
case "com.level":
print("we dont restore levels")
default:
print("can't restore")
}
}
Here is my payment queue also.
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("add paymnet")
for transaction:AnyObject in transactions {
let trans = transaction as! SKPaymentTransaction
print(trans.error)
switch trans.transactionState {
case .purchased:
print("buying, ok unlock iap here")
print(p.productIdentifier)
let prodID = p.productIdentifier as String
switch prodID {
case "com.premium":
print("buying ads")
removeAds()
case "com.cash":
print("buying coins")
addCoins()
case "com.level":
print("buying levels")
addLevels()
default:
print("can't buy")
}
queue.finishTransaction(trans)
break;
case .failed:
print("buy error")
queue.finishTransaction(trans)
break;
default:
print("default")
break;
}
}
}
You should not update any purchase status in paymentQueueRestoreCompletedTransactionsFinished. This function just lets you know that the restoration process has completed. You could use this to update your UI or display an alert or something.
The restoration process delivers the transactions to be restored to the updatedTransactions function where you handle the .restored state in the same way that you handle the .purchased state.
Essentially "restore" just replays the purchase transaction process for non-consumable and auto-renewing subscription purchase types.

In-app purchase ios sending more transactions than needed

Im using in-app purchase in SpriteKit. The first transaction does fine, but when i do the second one my
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])
called 2 times, than 3,4,5
so i do one request but there it adds 100 coins instead of 50...
i gues the problem in that function:
func buyProduct() {
print("buy +" + p.productIdentifier)
var payment = SKMutablePayment(product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().addPayment(payment)
}
i also have :
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
var myProduct = response.products
for product in myProduct {
list.append(product)
print("product added")
print(product.productIdentifier)
print(product.localizedTitle)
}
print("list is \(list)")
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print(transactions.count)
for transaction in transactions {
print(transaction.error)
switch transaction.transactionState {
case .Purchased:
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
print("buyed")
print(p.productIdentifier)
let prodID = p.productIdentifier
switch prodID {
case "com.addCoins" :
print("increaing coinsCount")
coinsCount = coinsCount + 50
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setInteger(coinsCount, forKey: "coins")
userDefaults.synchronize() // don't forget this!!!!
coinsLabel.text = String(coinsCount)
default: print("IAD not setuped")
}
print("premium added")
queue.finishTransaction(transaction)
break
case .Failed:
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
print("buy error")
queue.finishTransaction(transaction)
break
default: break
}
}
}
im calling purchase with:
for product in list {
let loadingNotification = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
loadingNotification.mode = MBProgressHUDMode.Indeterminate
loadingNotification.labelText = "Loading"
let prodID = product.productIdentifier
if prodID == "com.addCoins" {
p = product
buyProduct()
break
}
}
At some point you have to tell Apple that a purchase has been carried out and you delivered the goods. Until then, the purchases stay in the transaction queue forever.
Its bad practice to add
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
everytime you buy something. You should add the transaction observer at app launch and remove it when your app is closed, as per apples recommended guidelines.
Also you dont need to call synchronise for your userDefaults anymore since iOS 8, yet I still see a lot of people doing it.
In regards to you problem the code seems to be ok. The only thing I noticed so far is why in the buy function are you adding a SKMutablePayment?
Try changing SKMutablePayment to SKPayment and see if it makes a difference.
func buyProduct() {
print("buy +" + p.productIdentifier)
var payment = SKPayment(product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self) // should not be here
SKPaymentQueue.defaultQueue().addPayment(payment)
}

Resources