Home > Enterprise >  In-App Purchase not working in TestFlight
In-App Purchase not working in TestFlight

Time:06-30

My app was rejected by Apple, as they couldn't get my IAP to work. I'd only been testing through Xcode, and everything seemed to be working fine (I had no idea to test through TestFlight). I've since been testing through TestFlight, and the IAP doesn't work on this.

Can anyone offer any advice on where I'm going wrong? I have added "In-App Purchase" to the Signing & Capabilities. I have tested in TestFlight with both StoreKit Configuration set to "None" and also "Configuration.storekit".

My IAP on App Store Connect has the status “Ready to Submit”.

import StoreKit

class Menu: SKScene, SKProductsRequestDelegate, SKPaymentTransactionObserver {
    
    enum Product: String, CaseIterable {
        case premium = "[my IAP identifier]"
    }
    
    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        if let oProduct = response.products.first {
            print("Product is available")
            self.purchase(aproduct: oProduct)
        } else {
            print("Product is not available")
        }
    }
    
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch transaction.transactionState {
            case .purchased:
                SKPaymentQueue.default().finishTransaction(transaction)
                print("Purchased")
                purchasedPremium = true
                
                userDefaults.set(true, forKey: "purchasedPremium")
                
                var transition:SKTransition = SKTransition.fade(withDuration: 1)
                var scene:SKScene = Menu(size: self.size)
                self.view?.presentScene(scene, transition: transition)
                
            case .failed:
                SKPaymentQueue.default().finishTransaction(transaction)
                print("Failed")
            case .restored:
                print("Restored")
            case .deferred:
                print("Deferred")
            default:
                break
            }
        }
    }
    
    func purchase(aproduct: SKProduct) {
        let payment = SKPayment(product: aproduct)
        SKPaymentQueue.default().add(self)
        SKPaymentQueue.default().add(payment)
    }

I trigger the purchase procedure by running this method:

func buyPremium() {
        if SKPaymentQueue.canMakePayments() {
            let set : Set<String> = [Product.premium.rawValue]
            let productRequest = SKProductsRequest(productIdentifiers: set)
            productRequest.delegate = self
            productRequest.start()
        }
    }

CodePudding user response:

The flow you have implemented for your product request and purchase is not correct.

You should not request the product when the user initiates the purchase and you should not initiate the purchase when the SKProduct is retrieved.

You should adopt something like the following:

  • Instantiate a "purchase manager" object in didFinishLaunching
  • The "purchase manager" should:
    • Set itself as the payment queue observer.
    • Check for canMakePayments
    • If true then request your product
    • If you successfully retrieve your product, store the SKProduct and indicate to your app views that IAP is available
  • Your views can then show/enable the purchase UI (button etc)
    • When the user taps the purchase button you get your "purchase manager" to submit the SKPayment
    • Once the purchase is complete, persist the purchase state and refresh your views to unlock the purchased content

It is important that you establish your queue observer immediately on launch as there may be a pending purchase in the queue for your app to process.

Checking for product availability and canMakePayments first improves the user experience by ensuring they are not shown a purchase option that they cannot use

CodePudding user response:

  1. Set up in App Store Connect, configure the in-app purchase item's product name, description, price, and availability.
  2. Add and use Store Kit into your project
  3. You should create a sandbox Apple ID or test account, then you can test in-app purchases with sandbox. https://developer.apple.com/documentation/storekit/in-app_purchase/testing_in-app_purchases_with_sandbox

Please refer this link for more details https://developer.apple.com/in-app-purchase/

  • Related