Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct usage example #30

Closed
rankorn opened this issue Apr 7, 2014 · 4 comments
Closed

Correct usage example #30

rankorn opened this issue Apr 7, 2014 · 4 comments

Comments

@rankorn
Copy link

rankorn commented Apr 7, 2014

Since there is no documentation for this ANE, I will post how I made this work for Android and hopefully it will help others.

Application.xml:
The documentation says to add a service to the XML, but I have found this is wrong. You need to add an activity:

<activity android:name="com.freshplanet.inapppurchase.activities.BillingActivity"/>

Next thing you need to do in your code is to initialize the service:

_iap = InAppPurchase.getInstance();
_iap.init(GOOGLE_PLAY_LICENSE_KEY, true);

The GOOGLE_PLAY_LICENSE_KEY can be found in the Google play developer console under the Services & APIs section. NOTE: the initialization process is async, and for some reason freshplanet did not implement an event on initialization end. The only solution I have found is to make sure you wait enough time for the initialization to complete (bad bad practice.. but no good solution was found except to fork and add the event).

To make a purchase:

_iap.addEventListener(InAppPurchaseEvent.PURCHASE_SUCCESSFULL, onSuccess);
_iap.addEventListener(InAppPurchaseEvent.PURCHASE_ERROR, onError);
_iap.makePurchase(productId);

Everything is async so you have to add event listeners to receive success or error responses.
The InAppPurchaseEvent class holds a data property that is a JSON string with the information received from Google play. The content of the JSON is defined in the Google play in app purchases documentation.
For example, after you make a purchase you can extract the data like this:

var inAppData:Object = JSON.parse(e.data);
var receipt:Object = inAppData["receipt"];
var productId:String = inAppData["productId"];

Consuming products:

_iap.removePurchaseFromQueue(productId, JSON.stringify(receipt));

Where receipt is a JSON string that you get from Google play. This receipt is received right after you purchase or you can get it by querying the user's previous transactions.

More things to notice that will prevent problems

  • Unlike some posts found and some docs from Google, when you develop using Air there is no problem running in debug mode on your device and connecting to the debugger remotely from a connected device.
  • You can see debug and error messages from the ANE using LogCat. It's very easy to use and you can just run it from the command line when you have a connected device. There are some free LogCat tools with GUIs. Many times when there is an exception, you get nothing in Air but by checking the log you can see what's wrong.
  • The user that is used to test in app purchases on the Android device cannot be the user that is the administrator of the app. This is Google's limitation ( :-(( they say this is because you cannot buy from yourself but come on Google... you solved harder problems!)
  • You can test with the static predefined products that Google have available but you still must consume them to test again! I did not find any way to consume from the admin console, so I suggest you implement consuming before your first test run. But really, the easiest way is just to define some test products and test users.

That's it, hope it helps, hope it's correct, comment if not.

@rankorn rankorn closed this as completed Apr 7, 2014
@rankorn rankorn reopened this Apr 7, 2014
@rankorn rankorn mentioned this issue Apr 7, 2014
@esdebon
Copy link

esdebon commented Apr 14, 2014

Thank you @rankorn

@ChrisGamePill
Copy link

Can anyone please hint me on what needs to be done in case there's no InAppPurchaseEvent.PURCHASE_SUCCESSFULL sent back from the store? Have tried everything available on these discussions to no avail. From the catLog it all seems to be starting or being called to start and no error or warning or debug hints on any problems... I'm out of tries please help!

@rankorn
Copy link
Author

rankorn commented May 2, 2014

@ChrisGamePill as I said in another post, I personally moved to another ANE implementation:
https://github.com/pozirk/AndroidInAppPurchase
as I read that freshplanet's is now outdated.

Pozrik's ANE is Android only, but it's API is more similar to the native in-app API. If you do move I can help you with an example that works.

For iOS, I use Adobe's native extension.

@yadurajiv
Copy link

I'm listening to all these events

InAppPurchase.getInstance().init(GOOGLE_LICENSE_KEY, GOOGLE_FLAG_DEBUG);
InAppPurchase.getInstance().addEventListener(InAppPurchaseEvent.PRODUCT_INFO_ERROR, store_callback);
InAppPurchase.getInstance().addEventListener(InAppPurchaseEvent.PRODUCT_INFO_RECEIVED, store_callback);
InAppPurchase.getInstance().addEventListener(InAppPurchaseEvent.PURCHASE_DISABLED, store_callback);
InAppPurchase.getInstance().addEventListener(InAppPurchaseEvent.PURCHASE_ENABLED, store_callback);
InAppPurchase.getInstance().addEventListener(InAppPurchaseEvent.PURCHASE_ERROR, store_callback);
InAppPurchase.getInstance().addEventListener(InAppPurchaseEvent.PURCHASE_SUCCESSFULL, store_callback);
InAppPurchase.getInstance().addEventListener(InAppPurchaseEvent.RESTORE_INFO_RECEIVED, store_callback);
InAppPurchase.getInstance().addEventListener(InAppPurchaseEvent.SUBSCRIPTION_DISABLED, store_callback);
InAppPurchase.getInstance().addEventListener(InAppPurchaseEvent.SUBSCRIPTION_ENABLED, store_callback);

and later in my store_callback function, the purchase event can be checked.
function store_callback(pe:InAppPurchaseEvent):void

switch(true) {
    case (pe.type == InAppPurchaseEvent.PURCHASE_SUCCESSFULL):
        // get data
        var inAppData:Object = JSON.parse(pe.data);
        var receipt:Object = inAppData["receipt"];
        var productId:String = inAppData["productId"];

        // consume product if it is a consumable
        InAppPurchase.getInstance().removePurchaseFromQueue(productId, JSON.stringify(receipt));
        // and update whatever else in game/server etc

    case (pe.type == InAppPurchaseEvent.PURCHASE_ERROR):
        // failed! user cancel etc
        break;
}

I hope this is of some use. The other ANE looks interesting too!

@AdamFP AdamFP closed this as completed Apr 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants