PURCHASE
Purchase flow - iOS
Purchasing flow is a multistage process, which involves payment collection, purchase validation and consumption. Purchases the player already made are recovered separately, through restoration.
Starting purchasing flow
We can use previously acquired SKProduct info to create payment and initiate purchase:
- Objective-C
- Swift
SKProduct *product = ...; // previously fetched product
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
let product = ... // previously fetched product
let payment = SKPayment(product: product)
SKPaymentQueue.default().add(payment)
Validating purchases (recommended)
Although validation is NOT a mandatory step, yet it's highly recommended to minimize the chance of fraud and any other malevolent intents.
For instructions on how to validate your purchases, see Purchase validation.
Consuming purchased goods
The very final step in the chain of purchasing flow stages is to award the purchased goods to the user and mark these goods as "awarded". The process also known as purchase consumption.
Use the code below to do just that:
- Objective-C
- Swift
Purchase result will become known in paymentQueue:updatedTransactions: method:
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for(SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStateFailed:
// Check if the user cancelled the purchase (don't treat as an error)
if (transaction.error.code == SKErrorPaymentCancelled) {
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
// Always acknowledge transaction and finish it
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStatePurchased:
// here you can save the purchase and award it to the user
// Always acknowledge transaction and finish it after it was saved
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
default: break;
}
}
}
Purchase result will become known in paymentQueue(_:updatedTransactions:) method:
func paymentQueue(_: SKPaymentQueue, updatedTransactions: [SKPaymentTransaction]) {
for transaction in updatedTransactions {
switch transaction.transactionState {
case .failed:
// Check if the user cancelled the purchase (don't treat as an error)
if (transaction.error as? SKXError)?.errorCode == .paymentCancelled {
SKPaymentQueue.default().finishTransaction(transaction)
break
}
// Always acknowledge transaction and finish it
SKPaymentQueue.default().finishTransaction(transaction)
case .purchased:
// here you can save the purchase and award it to the user
// Always acknowledge transaction and finish it after it was saved
SKPaymentQueue.default().finishTransaction(transaction)
default:
break
}
}
}
It's highly recommended to perform at least some form of validation before consuming the purchased goods.
Always call finishTransaction(_:) after you've awarded the purchased goods. If local purchase restoration is enabled (the default), an unfinished transaction is re-delivered to paymentQueue(_:updatedTransactions:) on the next launch until it is finished — see Purchase Restoration Settings.
Restored purchases
Restoration hands you purchases the player already made, after a reinstall, on a new device, or when the app was closed before the purchase finished. It runs on its own, and there's no call to make. Where it reads them from is set in Purchase Restoration Settings — the player's Xsolla Inventory by default, or the Xsolla Events API.
In a webhook-based integration neither is used: Xsolla sends the purchase to your backend, and your backend decides what a returning player gets. Turn restoration off in the app with settings.useLocalPurchaseRestoration = false and use the transaction callbacks only to update your UI, as you would during server-side validation.
Restored purchases arrive in paymentQueue(_:updatedTransactions:) as .purchased, not .restored, and you finish them the same way as a new purchase. To tell the two apart, check original: it's set on a restored purchase and nil on one the player just made.
What a restored transaction carries depends on the backend:
| Property | Xsolla Inventory (default) | Events API |
|---|---|---|
transactionIdentifier | nil | Xsolla order ID |
invoiceIdentifier | nil | Xsolla invoice ID |
transactionDate | When restoration delivered it | When restoration delivered it |
original.transactionDate | nil | When the player paid |
payment.quantity | Units the player holds | Quantity from the original order |
As in StoreKit, transactionDate on a restored purchase is when the SDK handed it to you, and original.transactionDate is when the player paid. The Xsolla Inventory keeps no record of past purchases, so a restore from it gives you no date and no order IDs — only the fact that the player owns the item.
A player who reinstalls gets back everything they still own, so check your own records before you award a restored purchase. Otherwise a player can be given the same item twice.
Restoring from the Xsolla Inventory returns consumable Virtual Items only. Non-consumable items, virtual currency balances and bundles don't come back this way — get those from your own records or from the Xsolla Inventory.
Restoration is skipped while any purchase is still unfinished, and it doesn't tell you it was skipped. Finish every transaction you receive, or later restores quietly do nothing.