Backup and Restore Data

Backup and restore data is a feature that allows you to backup and restore purchased data manually.

By default the Library use Key/Value Backup (Android Backup Serviceopen in new window) to backup and restore data.

Backup Purchased Data

To backup encrypted purchased data as a JSON string or JSONObject.

Since purchased data is encrypted to device specifically, it only work for a single device

PurchasedItems purchasedItems = new PurchasedItems(/*context*/ this);
String encryptedString = purchasedItems.getAllEncryptedPayedItemAsJSONString();
JSONObject encryptedJSon = purchasedItems.getAllEncryptedPayedItemAsJSON();
val purchasedItems = PurchasedItems(/*context*/ this)
val encryptedString:String = purchasedItems.allEncryptedPayedItemAsJSONString
val encryptedJSon:JSONObject = purchasedItems.allEncryptedPayedItemAsJSON

Restore Purchased Data

To restore encrypted purchased data from a JSON string or JSONObject.

try {
   PurchasedItems purchasedItems = new PurchasedItems(/*context*/ this);
   String encryptedString = "--encrypted json string--";
   purchasedItems.restorePurchase(encryptedString);
   // using encrypted JSON object
   JSONObject encryptedJSON = // encrypted JSONObject
   purchasedItems.restorePurchase(encryptedJSON);
 } catch(JSONException e){
    e.printStackTrace();
}
try {
   val purchasedItems = PurchasedItems(/*context*/ this)
   val encryptedString = "--encrypted json string--";
   purchasedItems.restorePurchase(encryptedString);
   // using encrypted JSON object
   JSONObject encryptedJSON = // encrypted JSONObject
   purchasedItems.restorePurchase(encryptedJSON);
 } catch (e : JSONException){
    Log.e("Chapa", "Error: ${e.message}")
}