- Home /
Restoring Transactions ios, what do I add when the restoration process succeeded?
I've been trying to get my non consumables to restore on my ios device but I'm getting no results with my restore button. I want to know if there is anything else I'm supposed to add in this code?
// Inside class inheriting from Unity IAP IStoreListener interface
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
extensions.GetExtension
().RestoreTransactions (result => {
if (result) {
// This does not mean anything was restored,
// merely that the restoration process succeeded.
} else {
// Restoration failed.
}
});
}
I can't find any examples on what needs to go in, if anything at all. If someone could provide a bit of clarity or even an example code that has a working restore function then I would be really grateful.
Answer by Getsumi3 · Oct 29, 2019 at 12:42 PM
Hi
I'm bad at explanation but I'll try to help.
The restore method is almost the same as yours.
In my game I have a bool variable that is called on Awake/Start that tells to the game that item is unlocked.
Ok. So basicly if "result" returns true than CallYourMethodThatWillUnlockItems()
Here is the BuyLevels() and RestorePurchases() code that worked for my game.
private void Awake()
{
isUnlocked = PlayerPrefs.HasKey("isUnlocked") ? (isUnlocked = (PlayerPrefs.GetString("isUnlocked") == "true") ? true : false) : false;
PlayerPrefs.SetString("isUnlocked", (isUnlocked ? "true": "false"));
}
public void BuyLevels()
{
// If Purchasing has been initialized ...
if (IsInitialized())
{
// ... look up the Product reference with the general product identifier and the Purchasing
// system's products collection.
Product product = m_StoreController.products.WithID(kProductIDLevels);
// If the look up found a product for this device's store and that product is ready to be sold ...
if (product != null && product.availableToPurchase)
{
Console.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
// ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
// asynchronously.
m_StoreController.InitiatePurchase(product);
isUnlocked = true;
PlayerPrefs.SetString("isUnlocked", (isUnlocked ? "true" : "false"));
}
// Otherwise ...
else
{
// ... report the product look-up failure situation
Console.LogWarning("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
isUnlocked = false;
PlayerPrefs.SetString("isUnlocked", (isUnlocked ? "true" : "false"));
}
}
// Otherwise ...
else
{
// ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or
// retrying initiailization.
Console.LogError("BuyProductID FAIL. Not initialized.");
isUnlocked = false;
PlayerPrefs.SetString("isUnlocked", (isUnlocked ? "true" : "false"));
}
}
// Restore purchases previously made by this customer. Some platforms automatically restore purchases, like Google.
// Apple currently requires explicit purchase restoration for IAP, conditionally displaying a password prompt.
public void RestorePurchases()
{
// If Purchasing has not yet been set up ...
if (!IsInitialized())
{
// ... report the situation and stop restoring. Consider either waiting longer, or retrying initialization.
Console.LogError("RestorePurchases FAIL. Not initialized.");
return;
}
// If we are running on an Apple device ...
if (Application.platform == RuntimePlatform.IPhonePlayer ||
Application.platform == RuntimePlatform.OSXPlayer)
{
// ... begin restoring purchases
Console.Log("RestorePurchases started ...");
// Fetch the Apple store-specific subsystem.
var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
// Begin the asynchronous process of restoring purchases. Expect a confirmation response in
// the Action<bool> below, and ProcessPurchase if there are previously purchased products to restore.
apple.RestoreTransactions((result) =>
{
// The first phase of restoration. If no more responses are received on ProcessPurchase then
// no purchases are available to be restored.
Console.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
if (result == true)
{
Console.Log("Purchase restored");
//purchase restored. Unlock all bought items
isUnlocked = true;
PlayerPrefs.SetString("isUnlocked", (isUnlocked ? "true": "false"));
}
else
{
Console.LogWarning("No purchases available to restore");
}
});
}
// Otherwise ...
else
{
// We are not running on an Apple device. No work is necessary to restore purchases.
Console.LogError("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
}
}
I'll try this out with my code tonight after work but just so I understand, the code used to unlock the levels in your BuyLevels() method just needs to be copy pasted into the "result == true" if statement that's within the RestorePurchases() method correct?
Edit: O$$anonymous$$ so I think I'm missing something. I get that now I'm supposed to put in some kind of code to unlock the passed levels purchased but how do I do that if I can only use the results bool? I have multiple levels that are sorted by numbered string variables, how to I restore them if in the PurchaseProcessingResult() I did it like this.
if (String.Equals(args.purchasedProduct.definition.id, "puzzle_pack_" + packNumber.ToString(), StringComparison.Ordinal))
{
Debug.Log(string.Format("ProcessPurchase: PASS. Product: '{0}'", args.purchasedProduct.definition.id));
Data$$anonymous$$anager.Instance.UnlockPuzzle("puzzle_pack_" + packNumber.ToString());
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612214035im_/https://answers.unity.com/themes/thub/images/avi.jpg)