Murat
Murat

Reputation: 445

In-App Purchase - application let me purchase again after navigate

i am triyng to add in-app purchase option for my application. i can purchase product correctly but when i purchase and navigate to other page and back to purchase page i can still purchase same product again. and when i purchase another product, my older purchased product become unpurchased.

here is my codes:

    Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

    LicenseChangedEventHandler licenseChangeHandler = null;


    public SatinAlma()
    {
        this.InitializeComponent();

    }


    private async Task LoadInAppPurchaseProxyFileAsync()
    {

        StorageFolder proxyDataFolder = await Package.Current.InstalledLocation.GetFolderAsync("Data");
        StorageFile proxyFile = await proxyDataFolder.GetFileAsync("in-app-purchase.xml");
        licenseChangeHandler = new LicenseChangedEventHandler(InAppPurchaseRefreshScenario);
        CurrentAppSimulator.LicenseInformation.LicenseChanged += licenseChangeHandler;
        await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);

        try
        {
            ListingInformation listing = await CurrentAppSimulator.LoadListingInformationAsync();

            var PurchasePzl9 = listing.ProductListings["puzzle9"];
            var PurchasePzl16 = listing.ProductListings["puzzle16"];
            var PurchasePzl25 = listing.ProductListings["puzzle25"];
            var PurchaseYardimseverlik = listing.ProductListings["yardimseverlik"];
            var PurchaseTumpaket = listing.ProductListings["tumpaket"];

        }
        catch (Exception)
        {
            OAL_toast.showToast("LoadListingInformationAsync API call failed\n");
        }
    }

    ///////**i insert and delete this part nothing changed
    //protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
    //{
    //    if (licenseChangeHandler != null)
    //    {
    //        CurrentAppSimulator.LicenseInformation.LicenseChanged -= licenseChangeHandler;
    //    }
    //    base.OnNavigatingFrom(e);
    //}
    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        await LoadInAppPurchaseProxyFileAsync();
    }

    private void InAppPurchaseRefreshScenario()
    {

    }

    private async void purchaseProduct()
    {
        LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
        if (!licenseInformation.ProductLicenses[stringPurchaseProduct].IsActive)
        {
            try
            {
                await CurrentAppSimulator.RequestProductPurchaseAsync(stringPurchaseProduct);
                if (licenseInformation.ProductLicenses[stringPurchaseProduct].IsActive)
                {
                    OAL_toast.showToast(stringPurchaseProduct + " purchased.");
                    this.Frame.Navigate(typeof(MainPage));
                }
                else
                {
                    //OptimeAnimationLib.MsgBox(stringPurchaseProduct + " was not purchased.");
                    OAL_toast.showToast(stringPurchaseProduct + " was not purchased.");

                }
            }
            catch (Exception)
            {
                OAL_toast.showToast("Unable to buy " + stringPurchaseProduct);

            }
        }
        else
        {
            OAL_toast.showToast("you already own " + stringPurchaseProduct);

        }
    }

    private void IMG_puzzle9_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
        var productLicense = licenseInformation.ProductLicenses["puzzle9"];
        if (productLicense.IsActive)
        {
            OAL_toast.showToast("you already own Puzzle 9.");
        }
        else
        {
            stringPurchaseProduct = "puzzle9";
            purchaseProduct();
        }

    }

thanks, sorry for my english

Upvotes: 0

Views: 218

Answers (1)

It looks like you're reloading your Store simulator XML file every time you navigate, which will reset any Store activity you've done previously. Change your code so that you load the XML only when the app starts, and then you should see the Store state maintain itself across navigations. Note also that when you restart the app you'll be reloading the XML and thus reset the state.

Upvotes: 1

Related Questions