Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So now we'll look at the code to implementing Amazon In-App Purchases; I'm going to scroll down to my android manifest and you'll see that I have some code here starting with the receiver block. You can copy and paste this code from the Amazon in-app purchase SDK tutorial if you want and that's exactly what I did here. So we're just going to need the receiver block and then let's go to Skins.cpp. As I mentioned before, I just checked to see if that user default is true. If so, I set the skin and rerun MainMenu, if not I run buyItem.
So to unlock the item all we need to do is set that user default value true. Let's look at buyItem. I'll go over to jniIAP.cpp. I added this file to the folder where all of the jni files are and all it does is it runs a static method buyItem passing in the string to in moleitx.java. Remember you have to past in the fully qualified class name here, so that's fairly simple. Here is the header file in case you'd like to see it, and moleitx.java let's scroll down to buyItem.
In buyItem I used PurchasingManager. initiatePurchaseRequests passing in the item. PurchasingManager is an Amazon class. So let's scroll up and see how to handle the Amazon purchases. I created a class called my observer inside of my moleitx file and that extends BasePurchasingObserver and this is like a listener that listens for purchasing events. So in there I have a private moleitx called mainActivity. In the constructor I pass in that super and then I set mainActivity as the activity passed into the constructor.
And if you want to you can override on itemDataResponse; this is when you get item data so you can display any extra information to the user should you wish. And then the method that is executed when a purchase is responded to is called onPurchaseResponse, and that gives you the information about whether the purchase was successful or unsuccessful or they already have it. So here I have a string call response and I'm getting the string from the purchaseResponse.getPurchaseRequestStatus method, so that gives me the request status.
I got it as a string then I check to see if it's successful or the person already has this purchase. If so I'll run mainActivity unlockSkin. If not, I Log to see what the problem is. So it's pretty straightforward and of course you can find more information about this purchase response class inside of the documentation for the Amazon API. So let's scroll down a little bit and look at some other code that I added. The unlockSkin method runs native unlockSkin, which is a native method, which we'll look at in just the second.
You'll notice that on start I create the observer, passing in this and then I tell the PurchasingManager to register the observer and in onResume I run PurchasingManager.initiateGetUserIdRequest. So let's look at native unlocks skin, that's defined in jniUnlock.cpp. For this file there is no header file. Here I am defining the Java_com_ toddparkins_moleitx_moleitx_nativeUnlocksSkin with just the basic parameters.
And then I just run this CCUserDefault method and set that value true for the skin and that's how we unlock it. So when you test this out on your device, you should see that when you tap on the aliens, a popup window should show up, ask you to purchase the skin. If you do and the purchase is successful, if you hit ALIEN again, then it will change to the Alien Skin and that's all there is to it. So there is kind of a lot of code to implementing the Amazon In-App Purchases, but as you've seen the code is fairly simple.
Once you have all this code setup, you can test the app on your device and confirm the In-App Purchases working by clicking on the ALIENS button from the skins menu and seeing that dialog popup. And when you get a successful purchase, you can close the dialog and hit ALIEN again and the skin will change.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 102088 Viewers
61 Video lessons · 88805 Viewers
71 Video lessons · 72620 Viewers
56 Video lessons · 104256 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.