Restore Purchases
Restoring purchases is a process that you should allow for user's who have either changed devices or reinstalled your application and hence need to gain access to their purchases again. It should not be an automatic process but one done through user interaction.
This process must be available to your users and must initiated by user interaction in order to pass App Store review.
To satisfy this requirement, you must provide a button somewhere in your application to initiate this restore process. Generally somewhere like your application settings is an appropriate place to have a restore purchases functionality.
For more information on the concepts you can read the Apple documentation here.
For retrieving purchases without user interaction see the Get Purchases functionality.
- AIR
- Unity
The restoring purchases process starts by calling the restorePurchases()
function and concludes with either a success (InAppBillingEvent.RESTORE_PURCHASES_SUCCESS
) or failure (InAppBillingEvent.RESTORE_PURCHASES_FAILED
) event.
After calling restorePurchase()
you will a receive PurchaseEvent.PURCHASES_UPDATED
dispatched
which will contain Purchase
objects in the restored state having the original purchase variable
representing the original purchase transaction.
// The service must be setup and a list of products retrieved.
// Make sure you have added your purchases handler
InAppBilling.service.addEventListener( PurchaseEvent.PURCHASES_UPDATED, purchases_updatedHandler );
InAppBilling.service.addEventListener( InAppBillingEvent.RESTORE_PURCHASES_SUCCESS, restorePurchases_successHandler );
InAppBilling.service.addEventListener( InAppBillingEvent.RESTORE_PURCHASES_FAILED, restorePurchases_failedHandler );
// Start the restore process
InAppBilling.service.restorePurchases();
//
//
//
function purchases_updatedHandler( event:PurchaseEvent ):void
{
for each (var purchase:Purchase in event.data)
{
switch (purchase.transactionState)
{
// Other states...
case Purchase.STATE_RESTORED:
// The originalPurchase property will contain all the
// information on the original transaction allowing
// you to restore the user's product
addPurchaseToInventory( purchase.originalPurchase );
InAppBilling.service.finishPurchase( purchase );
// Again you could hold on to this purchase and check it against
// your application server before calling finishPurchase
break;
}
}
}
//
// RESTORE PURCHASE EVENTS
//
function restorePurchases_successHandler( event:InAppBillingEvent ):void
{
// Called when all purchases have been restored
// This event contains no data
}
function restorePurchases_failedHandler( event:InAppBillingEvent ):void
{
// Called if there was an error restoring purchases
}
The restoring purchases process starts by calling the RestorePurchases()
function and concludes with either a success (InAppBilling.Instance.Events.OnRestorePurchasesSuccess
) or failure (InAppBilling.Instance.Events.OnRestorePurchasesFailed
) event.
After calling RestorePurchases()
you will a receive the InAppBilling.Instance.Events.OnPurchaseUpdated
event dispatched which will contain Purchase
objects in the restored state having the original purchase
variable representing the original purchase transaction.
// The service must be setup and a list of products retrieved.
// Make sure you have added your purchases handler
InAppBilling.Instance.Events.OnPurchaseUpdated += OnPurchaseUpdated;
InAppBilling.Instance.Events.OnRestorePurchasesSuccess += OnRestorePurchasesSuccess;
InAppBilling.Instance.Events.OnRestorePurchasesFailed += OnRestorePurchasesFailed;
// Start the restore process
InAppBilling.Instance.RestorePurchases();
private void OnPurchaseUpdated(PurchaseEvent e)
{
foreach (Purchase purchase in e.purchases)
{
switch (purchase.transactionState)
{
// Other states...
case Purchase.STATE_RESTORED:
// The originalPurchase property will contain all the
// information on the original transaction allowing
// you to restore the user's product
AddPurchaseToInventory( purchase.originalPurchase );
InAppBilling.Instance.FinishPurchase( purchase );
// Again you could hold on to this purchase and check it against
// your application server before calling finishPurchase
break;
}
}
}
private void OnRestorePurchasesSuccess(PurchaseEvent e)
{
// Called when all purchases have been restored
// This event contains no data
}
private void OnRestorePurchasesFailed(PurchaseEvent e)
{
// Called if there was an error restoring purchases
Debug.Log("Restore purchases failed: " + e.errorCode + " " + e.message);
}