Unity IAP Error: Unable to buy item (response: 7: Item Already Owned ) DuplicateTransaction

Hi,

I use Unity 2018.3 and Unity In-App Purchase in my game. I encountered this error: products that I tried to buy before but failed in the middle of the process seem like bought and I can’t buy those products now because it says they are already owned.

Here is how it happens: I try to buy a consumable product. While it is being bought I close the game before the process was completed. Then the product stays as consumed because consuming the product happens at the end of the purchasing process by Unity IAP. Which means, I initialize the buying process but I don’t finalize it so, Google Play sees it as bought and not consumed. When I try to buy the same item again, this time it gives me an error that I can’t buy it again because the item is already owned.

This is the log file:

03-03 15:39:09.735: I/Unity(8475): Purchasing product asychronously: 'specialpack1'
03-03 15:39:09.735: I/Unity(8475):  
03-03 15:39:09.735: I/Unity(8475): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
03-03 15:39:09.736: I/UnityIAP(8475): isUnityVrEnabled = false
03-03 15:39:09.737: I/UnityIAP(8475): onPurchaseProduct: specialpack1
03-03 15:39:09.737: I/UnityIAP(8475): ITEM TYPE:inapp
03-03 15:39:09.753: I/Unity(8475): purchase({0}): specialpack1
03-03 15:39:09.753: I/Unity(8475):  
03-03 15:39:09.753: I/Unity(8475): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
03-03 15:39:09.799: I/UnityIAP(8475): Creating purchase activity
03-03 15:39:09.799: I/UnityIAP(8475): oldSkuMetadata is null
03-03 15:39:09.800: I/UnityIAP(8475): invoking callback
03-03 15:39:09.800: I/UnityIAP(8475): Constructing buy intent for specialpack1, item type: inapp
03-03 15:39:09.854: I/UnityIAP(8475): Launching buy intent for specialpack1. Request code: 999

-- Note: App was closed here before the buying process was completed

03-03 15:40:29.066: I/Unity(8722): Purchasing product asychronously: 'specialpack1'
03-03 15:40:29.066: I/Unity(8722):  
03-03 15:40:29.066: I/Unity(8722): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
03-03 15:40:29.083: I/UnityIAP(8722): isUnityVrEnabled = false
03-03 15:40:29.087: I/UnityIAP(8722): onPurchaseProduct: specialpack1
03-03 15:40:29.089: I/UnityIAP(8722): ITEM TYPE:inapp
03-03 15:40:29.113: I/Unity(8722): purchase({0}): specialpack1
03-03 15:40:29.113: I/Unity(8722):  
03-03 15:40:29.113: I/Unity(8722): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
03-03 15:40:29.162: I/UnityIAP(8722): Creating purchase activity
03-03 15:40:29.162: I/UnityIAP(8722): oldSkuMetadata is null
03-03 15:40:29.163: I/UnityIAP(8722): invoking callback
03-03 15:40:29.163: I/UnityIAP(8722): Constructing buy intent for specialpack1, item type: inapp
03-03 15:40:29.200: I/UnityIAP(8722): onIabPurchaseFinished: false
03-03 15:40:29.200: I/UnityIAP(8722): Unable to buy item (response: 7:Item Already Owned)
03-03 15:40:29.200: I/UnityIAP(8722): Purchase response code:7
03-03 15:40:29.279: I/UnityIAP(8722): onActivityResult
03-03 15:40:29.347: I/Unity(8722): onPurchaseFailedEvent({0}): specialpack1
03-03 15:40:29.347: I/Unity(8722):  
03-03 15:40:29.347: I/Unity(8722): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
03-03 15:40:29.363: I/Unity(8722): OnPurchaseFailed: FAIL. Product: 'specialpack1', PurchaseFailureReason: DuplicateTransaction
03-03 15:40:29.363: I/Unity(8722):  
03-03 15:40:29.363: I/Unity(8722): (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)

I have solved it. It turns out there was an error in the code that prevented Unity IAP from consuming all of the products at the beginning. Therefore PurchaseProcessingResult.Complete wasn’t being returned.

public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args) 
{
    // (code with error was here)
    return PurchaseProcessingResult.Complete;
}

For resolving the bug, all you need is to edit the OnInitialized function as follows:

    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        // Purchasing has succeeded initializing. Collect our Purchasing references.
        // Overall Purchasing system, configured with products for this application.
        m_StoreController = controller;
        // Store specific subsystem, for accessing device-specific store features.
        m_StoreExtensionProvider = extensions;

        foreach (var product in m_StoreController.products.all)
        {
            m_StoreController.ConfirmPendingPurchase(product);
        }
    }

You can get more details here:
http://codesaying.com/unity-iap-stuck/