# Restore Previous Purchases

## Goal

Restore non-consumables and subscriptions (for example after reinstall or on a new device).

## Actions Required

| Action                                          | Purpose                                            |
| ----------------------------------------------- | -------------------------------------------------- |
| BillingServicesInitializeStore                  | Load product catalog                               |
| BillingServicesRestorePurchases                 | Trigger restore process                            |
| BillingServicesOnRestorePurchasesComplete       | Listen for restore completion                      |
| BillingServicesGetRestorePurchasesSuccessResult | Get restored count                                 |
| BillingServicesGetRestoredTransactionInfo       | Read restored transaction details                  |
| BillingServicesFinishTransactions               | Finish restored transactions                       |
| BillingServicesGetRestorePurchasesError         | Read cached restore error after failure (optional) |

## Variables Needed

* restoredCount (Int)
* transactionIndex (Int)
* productId (String)
* receiptVerificationState (Enum: BillingReceiptVerificationState)

## Implementation Steps

### 1. InitializeStore

Run `BillingServicesInitializeStore` and wait for `successEvent`.

### 2. RegisterRestoreListener (persistent)

Run `BillingServicesOnRestorePurchasesComplete` in a state that stays active (for example in your settings scene while the restore flow runs).

* This action is what fires the completion events and caches data for the extractor actions.

### 3. TriggerRestore (user taps “Restore Purchases”)

Run `BillingServicesRestorePurchases` (`forceRefresh` optional).

### 4. Wait for completion

Your `BillingServicesOnRestorePurchasesComplete` listener will fire:

* `successEvent` → `GetRestoreResult`
* `failureEvent` → (Optional) `BillingServicesGetRestorePurchasesError`

### 5. GetRestoreResult

Run `BillingServicesGetRestorePurchasesSuccessResult` → `restoredCount`.

### 6. Loop restored items

Loop `transactionIndex = 0..restoredCount-1`:

* `BillingServicesGetRestoredTransactionInfo(transactionIndex)` → `productId`, `transactionState`, `receiptVerificationState`
* Grant content only when:
  * `transactionState == Purchased`
  * `receiptVerificationState == Success`
* If verification failed, do not restore the entitlement

### 7. FinishRestore

Run `BillingServicesFinishTransactions` (either finish all cached/pending, or only the restored IDs you collected).

## Notes

* Consumables are not restorable (by design).
* Prefer showing restore from a Settings screen rather than auto-restoring every launch.
* Restored purchases should still pass local verification. A restored transaction that is not `ReceiptVerificationState.Success` should not grant content.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://assetstore.essentialkit.voxelbusters.com/features/billing-services/playmaker/use-cases/use-case-3-restore-purchases.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
