When the user is ready to purchase an item, your application asks the App Store to collect payment. When your application asks for payment, the App Store creates a persistent transaction and continues to process the payment, even if the user quits and relaunches your application. The App Store synchronizes the list of pending transactions with your application and delivers updates to your application when the status of any of these transactions changes.
To collect payment, your application creates a payment object and queues it on the payment queue, as shown in Figure 3-1.
When the payment is added to the payment queue, a persistent transaction is created to hold it. After the payment is processed, the transaction is updated with information about the payment collection. Your application implements an observer that receives messages when transactions are updated. The observer should provide purchased items to the user and then remove the transaction from the payment queue.
Collecting payment starts with a payment object. The payment object includes a product identifier and optionally includes the quantity of that product to be purchased. You can queue the same payment object more than once; each time a payment object is queued results in a separate request for payment.
Users can disable the ability to make purchases in the Settings application. Before attempting to queue a purchase, your application should first confirm that payment can be processed. You do this by calling the payment queue’s canMakePayments
method.
The payment queue is used to communicate with the App Store. When payments are added to the queue, Store Kit transmits the request to the App Store. Store Kit presents dialogs to ask the user to authorize payment. The completed transaction is returned to your application’s observer.
A transaction is created for every payment added to the queue. Each transaction has properties that allow your application to determine the status of the transaction. When payment is collected, the transaction includes additional details about the successful transaction.
Although your application can ask the payment queue for a list of pending transactions, it is more common for an application to wait until the payment queue notifies the payment queue’s observer with a list of updated transactions.
Your application implements the SKPaymentTransactionObserver
protocol on an object and adds it as an observer to the payment queue. The observer’s primary responsibility is to examine completed transactions, deliver items that were successfully purchased, and remove those transactions from the payment queue.
Your application should associate an observer with the payment queue when it launches, rather than wait until the user attempts to purchase an item. Transactions are not lost when an application terminates. The next time the application launches, Store Kit resumes processing transactions. Adding the observer during your application’s initialization ensures that all transactions are returned to your application.
Once a transaction has been processed and removed from the queue, your application normally never sees it again. If your application supports nonconsumable or subscription products, it must provide the ability for users to restore these purchases on additional devices.
Store Kit provides built-in functionality to restore transactions for nonconsumable products. To restore transactions, your application calls the payment queue’s restoreCompletedTransactions
method. For each nonconsumable purchase that was previously completed, the App Store generates a new restore transaction. The restore transaction includes a copy of the original transaction. Your application processes restore transactions by retrieving the original transaction and using it to unlock the purchased content. Once Store Kit restores all the previous transactions, it notifies the payment queue observers by calling their paymentQueueRestoreCompletedTransactionsFinished:
method.
If the user attempts to purchase a nonconsumable item they have already purchased, your application receives a regular transaction for that item, not a restore transaction. However, the user is not charged again for that product. Your application should treat these transactions identically to those of the original transaction.
Subscriptions and consumable products are not automatically restored by Store Kit. To restore these products, you must record the transactions on your own server when they are purchased and provide your own mechanism to restore those transactions to the user’s devices.
Last updated: 2010-06-14