• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2025 Replies
  • 331 Replies
    1928 Replies

Simple in-app purchases

User avatar
CodenStuff
Site Admin
Posts: 4361

Simple in-app purchases

Wed Dec 07, 2016 5:00 pm

The following is a basic example of how to deal with in-app (or now called add-on) purchases for your app.

The first thing you will need in order to test in-app purchases is to add a file to your project called "WindowsStoreProxy.xml" and enter the following code in to the file:

Code: Select all

<?xml version="1.0" encoding="utf-16" ?>
<CurrentApp>
    <ListingInformation>
        <App>
            <AppId>00000000-0000-0000-0000-000000000000</AppId>
            <LinkUri>
    http://apps.microsoft.com/webpdp/app/00000000-0000-0000-0000-000000000000
           </LinkUri>
            <CurrentMarket>en-US</CurrentMarket>
            <AgeRating>3</AgeRating>
            <MarketData xml:lang="en-US">
                <Name>AppName</Name>
                <Description>AppDescription</Description>
                <Price>1.00</Price>
                <CurrencySymbol>$</CurrencySymbol>
                <CurrencyCode>USD</CurrencyCode>
            </MarketData>
        </App>
        <Product ProductId="TestFeature" ProductType="Durable">
            <MarketData xml:lang="en-US">
                <Name>TestFeature</Name>
                <Price>1.00</Price>
                <CurrencySymbol>$</CurrencySymbol>
                <CurrencyCode>USD</CurrencyCode>
            </MarketData>
        </Product>      
    </ListingInformation>
    <LicenseInformation>
        <App>
            <IsActive>true</IsActive>
            <IsTrial>false</IsTrial>
        </App>
        <Product ProductId="TestFeature">
            <IsActive>false</IsActive>
        </Product>    
    </LicenseInformation>
</CurrentApp>
Now lets go through some of that code so you can identify what does what. The following piece of code identifies the in-app purchase item that you wish to sell:

Code: Select all

        <Product ProductId="TestFeature" ProductType="Durable">
            <MarketData xml:lang="en-US">
                <Name>TestFeature</Name>
                <Price>1.00</Price>
                <CurrencySymbol>$</CurrencySymbol>
                <CurrencyCode>USD</CurrencyCode>
            </MarketData>
        </Product>
Here is a product simply called "TestFeature" and it is a "Durable" purchase meaning it never expires, perfect for one off purchases for example enabled extra content/features in your app. It also has a price and the currency we're charging in.

Code: Select all

        <Product ProductId="TestFeature">
            <IsActive>false</IsActive>
        </Product>
Here we identify if the in-app product has been purchased or not. true is it has already been purchased, false if not.

You can easily extent the WindowsStoreProxy file and add other in-app products using the codes above.

Now lets test them out in our app. Start a new UWP project and add a button control to the mainpage.xml and use the following for its click event:

Code: Select all

'Comment these 2 lines out before publish to the store
        '******************************************************
        Dim file = Await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("WindowsStoreProxy.xml")
        Await CurrentAppSimulator.ReloadSimulatorAsync(file)
        '******************************************************

        Dim licenseInformation As LicenseInformation = CurrentAppSimulator.LicenseInformation
        If Not licenseInformation.ProductLicenses("TestFeature").IsActive Then
            Try
                Await CurrentAppSimulator.RequestProductPurchaseAsync("TestFeature")
                If licenseInformation.ProductLicenses("TestFeature").IsActive Then
                    'Purchased it
                Else
                    'Not purchased it
                End If
            Catch generatedExceptionName As Exception

            End Try
        Else
            'No items for sale
        End If
The first part of that code simply loads the WindowsStoreProxy.xml file for testing purposes. The rest of the code is the purchase mechanism that allows us to buy our in-app products...virtually anyway. The product we wish to purchase has been identified as "TestFeature". When you want to upload your app to the store you would obviously change "TestFeature" to the name of your created add-on which you'd have set up in the store.

If all went well our code should be working and you would be prompted with the test store dialogue when you press the button in your app.

Okay so that works but how do we check if the user has already purchased our add-on or not when the app is launched.

Code: Select all

'Comment these 2 lines out before publish to the store
        '******************************************************
        Dim file = Await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("WindowsStoreProxy.xml")
        Await CurrentAppSimulator.ReloadSimulatorAsync(file)
        '******************************************************
        Dim licenseInformation As LicenseInformation = CurrentAppSimulator.LicenseInformation
        Dim productLicense = licenseInformation.ProductLicenses("TestFeature")
        If productLicense.IsActive Then
            'Has been purchased - do something here
        Else
            'Has NOT been purchased - choose to do something here
        End If
The above code quite simply checks if the product has been purchased and a good place to put that code is in your mainpage.xml loaded event. Now in order to test this lets change the following code in our WindowsStoreProxy.xml file:

Code: Select all

        <Product ProductId="TestFeature">
            <IsActive>true</IsActive>
        </Product>
We have now changed the active property to true which tells us that the product has already been purchased. Run your app and you'll noticed you no longer get a dialogue when pushing your button.

And that is basically all there is to it. Change the code to match your own requirements.

Very important
You MUST change all instances of "CurrentAppSimulator" to "CurrentApp" before uploading to the store or your app will not work and be rejected.

Download an example project below to see it all in action.
CNSSimpleInAppPurchase.zip
I hope this helps you :)
You do not have the required permissions to view the files attached to this post.
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.

User avatar
Scottie1972
Codenstuff Elite
Posts: 953

Re: Simple in-app purchases

Wed Dec 28, 2016 2:54 am

As usual Cody, awesome tutorial.
Image

Post Reply

Return to “Visual Basic”