Tutorial Google Play Store Purchase

276 views
Skip to first unread message

Christoph Stehl

unread,
Jan 25, 2015, 12:51:12 AM1/25/15
to androi...@googlegroups.com
With this code you can call the PlayStore InApp Window:

playStore = app.CreatePlayStore();

playStore.Purchase( "prodID", "token", "callback");

Dave Smart

unread,
Jan 25, 2015, 6:05:57 AM1/25/15
to androi...@googlegroups.com
Hi Guys,

Sorry this is not documented properly yet, but here are some code snippets that might help those trying to use In App Purchasing:-

You will need to read the Google play IAP licensing info and setup IAP products in your dev console to use this.. although you can use Google's test ids to practice with at first.

Here is how to prepare for IAP:-

var playStore=null, purchases=[];

function OnStart()
{
   DrawLayout();
   :
   //Create Google Play object.
   playStore = app.CreatePlayStore();
   :
}


Here is how to buy an item:-

//Purchase an IAP item.
function PurchaseItem()
{
        //Start the purchase process.
        var prodId = "myproductcode";
playStore.Purchase( prodId, "xbxrandomxbx", OnPurchased );
}

//Handle completed purchase.
function OnPurchased( prodId )
{
    //Update purchase items array.
    purchases[prodId] = true;
    
    alert( "OnPurchased" + prodId );
}


Here is how to check for purchased items:-

//Check items are licensed.
function CheckLicenses()
{
    //Check licenses after delay (allows app to finish starting up).
    setTimeout( "playStore.GetPurchases(OnLicenses)", 1000 );
}

//Save licenses to an array.
function OnLicenses( items )
{
    for( var i=0; i<items.length; i++ )
        purchases[items[i].productId] = (items[i].purchaseState==0);
}


Here is how to get prices of items:-

//Get prices from Google Play.
//(Takes a comma separated list of prod ids)
function GetPrices( prodIDs )
{
    //Get product info from Google Play.
    //(Only paid items will be listed in OnStoreInfo)
    playStore.GetBillingInfo( prodIDs, OnStoreInfo );
}

//Show Play Store prices.
function OnStoreInfo( items )
{
    //Show prices.
    for( var i=0; i<items.length; i++ )
    {
        var prodId = items[i].productId;
        var price = items[i].price;
        alert( prodId + " = " + price );
    }
}

Samueljh1 Youtube

unread,
Jan 26, 2015, 2:20:42 AM1/26/15
to androi...@googlegroups.com
I did this but it says I need to login when I already have... How do I fix this? Is it something to to do with the params prodid and token? Where do I get these? Thanks.

Dave Smart

unread,
Jan 28, 2015, 5:09:14 AM1/28/15
to androi...@googlegroups.com
Hi Samuel,

You need to use a valid IAP product id... you can create your own using your Google Play developer console or you can use the Google testing ids (search on Google for it).  The token is not required, so you can use anything for that.  You need to read the section on 'in-app-purchasing' in the developer console carefully.

I'm pretty sure you can't do test purchases when logged into your developer account. So you might need to borrow someones phone or use a second phone to do test purchases.

Regards
David

Samueljh1 Youtube

unread,
Feb 1, 2015, 9:52:27 AM2/1/15
to androi...@googlegroups.com
I can't find any testing ones! :( p.s where exactly in the dev console do I make the key?

Dave Smart

unread,
Feb 1, 2015, 1:10:13 PM2/1/15
to androi...@googlegroups.com
You can find where you create new IAP product id's in the "In-App Products" section, right below "Pricing and Distribution" menu item.

Just click on the "Add new Product" button and make up new id for it like "my_extra_stuff" or something.

There is also a "Learn more about in-app billing and in-app products." link to click on there.

Regards
David

Samueljh1 Youtube

unread,
Feb 2, 2015, 2:05:24 AM2/2/15
to androi...@googlegroups.com
Thanks anyway, I managed to work it out! :)
Message has been deleted

Steve Garman

unread,
Dec 22, 2015, 6:11:04 PM12/22/15
to DroidScript
Gabriele,
all of that is explained in Dave Smart's three posts in this thread except for the "how does it know" question

The answer to that is
Yes it knows by the package name.

John Constatine

unread,
Jan 5, 2017, 11:34:08 AM1/5/17
to DroidScript
Does anyone here have a clearer example from the DS In-App Purchasing example?
I could not understand how to sell internal items, the DS does when you buy a pplugin, I would like to implement a free-premium application
To be clear is I have an application and I want to have two functions called by two buttons, but these functions are released to anyone who buys the item, any help?

Chris

unread,
Jan 5, 2017, 12:10:56 PM1/5/17
to DroidScript
AS I STATED IN YOUR OTHER POST, this sample was moved to a premium sample AND has been discussed in depth in that forum.

Chris

Rico

unread,
Apr 5, 2019, 10:29:51 PM4/5/19
to DroidScript
I want to make an application using IAP for 10,000 items (a matrix of 10,000 rows), or else you can buy the information contained on a matrix row. May?

Netpower8

unread,
Apr 6, 2019, 1:55:07 AM4/6/19
to DroidScript
There is a limit on IAP items you can sell. Anybody can correct me if i'm wrong. I think its 20 item is the limit.
Reply all
Reply to author
Forward
0 new messages