In-App Purchase

Create In-App purchase product

Before you refer this, please visit itunesconnect and create in-app purchase product under your app. You will get the product identifier which we are going to use in this code snippet. 

If you already have done then ignore this.

 

Import Statements


    import StoreKit

    


Protocols


    SKProductsRequestDelegate, SKPaymentTransactionObserver



Get product info & price


    func getInAppPrice(for identifier: String) {

        if SKPaymentQueue.canMakePayments() {

            let productsRequest = SKProductsRequest(productIdentifiers: [identifier])

            productsRequest.delegate = self

            productsRequest.start()

        }else {

            print("User cannot make payments due to parental controls")

        }

    }



    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {

        if response.products.count > 0 {

            let validProduct: SKProduct = response.products[0]

            //price = validProduct.localizedPrice

            purchase(product: validProduct)

        }else {

           // "No valid in-app purchases available"

        }

    }



Localised Price extension, will return price in local format


    extension SKProduct {

        var localizedPrice: String {

            let formatter = NumberFormatter()

            formatter.numberStyle = .currency

            formatter.locale = priceLocale

            return formatter.string(from: price)!

        }

    }    



Initiate Purchase


    func purchase(product: SKProduct) {

        let skPayment = SKPayment(product: product)

        SKPaymentQueue.default().add(self)

        SKPaymentQueue.default().add(skPayment)

    }



Purchase / Restore Delegate Methods


    func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {

           // "Error.localised"

    }


    

    func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {

        if queue.transactions.count == 0 {

            // "No purchases found to restore"

        }else {

            for transaction in queue.transactions {

                if transaction.transactionState == .restored {

                    SKPaymentQueue.default().finishTransaction(transaction)

                    break

                }

            }

        }

    }


    

    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {


        for transaction in queue.transactions {

            switch transaction.transactionState {

            case .purchasing:

                print("purchasing")

            case .purchased:

                SKPaymentQueue.default().finishTransaction(transaction)

                print("Purchase successfully")

            case .restored:

                SKPaymentQueue.default().finishTransaction(transaction)

                print("Purchase restored successfully")

            case .failed:

                SKPaymentQueue.default().finishTransaction(transaction)

                print("Purchase could not be completed")

            case .deferred:

                SKPaymentQueue.default().finishTransaction(transaction)

                print("Purchase could not be completed")

            default: break

                

            }

        }

        

    }



Restore Purchase


    func restore() {

        SKPaymentQueue.default().add(self)

        SKPaymentQueue.default().restoreCompletedTransactions()

    }







Comments

Popular posts from this blog

PUSH Notification

NotificationCenter