Skip to content
Roman Karpievich edited this page Apr 26, 2019 · 13 revisions

Public API reference

Content

Usage

finishing stands for whether transaction will be finished automatically or not.

Get products list

BillingHelper.sharedInstance.products(["com.applicaster.iap.purchase1"]) { (result) in
    switch result {
    case .success(let response):
        let products = response.products
        let invalidIDs = response.invalidIDs
    case .failure(let error):
        print(error.localizedDescription)
    }
}

Purchase item

BillingHelper.sharedInstance.purchase(product, amount: 1, finishing: true, completion: { (result) in
    switch result {
    case .success(let purchase):
        self.unlockContent(for: purchase)
    case .failure(let error):
        print(error.localizedDescription)
    }
})

Restore purchases

BillingHelper.sharedInstance.restore(finishing: true) { (result) in
    switch result {
    case .success(let transactions):
        let purchasedItemIDs = transactions.map({ $0.payment.productIdentifier })
    case .failure(let error):
        print(error.localizedDescription)
    }
}

Download content bound to purchase

It is possible to set associated downloadable content to in-app purchase. This content can be retrieved through SkPaymentTransaction object which you get during purchasing, restoration and finishing uncompleted transactions.

let transaction = purchase.transaction!
let downloads = transaction.downloads

BillingHelper.sharedInstance.start(downloads)
//BillingHelper.sharedInstance.cancel(downloads)
//BillingHelper.sharedInstance.pause(downloads)
//BillingHelper.sharedInstance.resume(downloads)

BillingHelper.sharedInstance.downloadsCompletion = { downloads in
    // process downloaded content
}

Finish transactions

It is possible that transaction won’t be finished (for example due to network connection issues). It is recommended to set your transaction completion handler at application(:didFinishLaunchingWithOptions:) method.

BillingHelper.sharedInstance.unfinishedTransactionCompletion = { transaction in
    switch transaction.transactionState {
    case .purchased, .restored:
        // unlock content
        BillingHelper.sharedInstance.finishTransaction(transaction)
    case .deferred,.failed, .purchasing:
        break
    }
}

Get local receipt

let receiptData: Data? = BillingHelper.sharedInstance.localReceiptData()

Refresh receipt

BillingHelper.sharedInstance.refreshReceipt { (result) in
    switch result {
    case .success:
        break
    case .failure(let error):
        print(error.localizedDescription)
    }
}

Clone this wiki locally