In-App Purchase
Integration
Open a terminal and use the following command to install the SDKBOX IAP plugin. Make sure you setup the SDKBOX installer correctly.
$ sdkbox import iap
Important Notice
If you upgraded to Xcode7 you need to perform the following steps to your project for plugin to function correctly.
Disable App Transport Security
Adding the following entry to the plist:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
Disable Bitcode support
You have to turn off Bitcode support. If you don't cocos2d-x will fail to build.
Set your game requires full screen
If your game doesn't support all screen orientations you will need to check Requires full screen
in Xcode. If you do not, your app will fail Apple's submission process.
Whitelist canOpenURL function
This depends on what plugins are in your project. You may need to add the required entry to the info.plist
, underLSApplicationQueriesSchemes
.
JSON Configuration
SDKBOX Installer will automatically inject a sample configuration to your sdkbox_config.json
, that you have to modify it before you can use it for your own app
Here is an example of the IAP configuration, you need to replace <put the product id for ios here>
with the product id from your iTunes Connect or replace <put your googleplay key here>
from your Google Play Console
"ios" :
{
"iap":{
"items":{
"remove_ads":{
"id":"<put the product id for ios here>"
}
}
}
},
"android":
{
"iap":{
"key":"<put your googleplay key here>",
"items":{
"remove_ads":{
"id":"<put the product id for android here>"
}
}
}
}
If you have IAP items that are non-consumable, it is also necessary to supply this for each item in your sdkbox_config.json
. Only Android requires this step. Taking the same json above your config might now look like this example:
"android":
{
"iap":{
"key":"<put your googleplay key here>",
"items":{
"remove_ads":{
"id":"<put the product id for android here>",
"type":"non_consumable"
}
}
}
}
Usage
Initialize IAP
Initialize the plugin where appropriate in your code. We recommend to do this in theAppDelegate::applicationDidFinishLaunching()
or AppController:didFinishLaunchingWithOptions()
. Make sure to include the appropriate headers:
#include "PluginIAP/PluginIAP.h"
AppDelegate::applicationDidFinishLaunching()
{
sdkbox::IAP::init();
}
Retrieve latest Product data
It's always a good idea to retrieve the latest product data from store when your game starts.
To retrieve latest IAP data, simply call sdkbox::IAP::refresh()
.
onProductRequestSuccess
will be trigged if retrieved successfully.
onProductRequestFailure
will be trigged if exception occurs.
Make a purchase
To make a purchase call sdkbox::IAP::purchase(name)
Note: name is the name of the IAP item in your config file under items
tag, not the product id you set in iTunes or GooglePlay Store
onSuccess
will be triggered if purchase is successful.
onFailure
will be triggered if purchase fails.
onCanceled
will be triggered if purchase is canceled by user.
Restore purchase
To restore purchase call sdkbox::IAP::restore()
.
onRestored
will be triggered if restore is successful.
Note: onRestored
could be triggered multiple times
Handling Purchase Events
This allows you to catch the IAP
events so that you can perform operations based upon the response from your players and IAP servers.
- Allow your class to extend
sdkbox::IAPListener
:
#include "PluginIAP/PluginIAP.h"
class MyClass : public sdkbox::IAPListener
{
private:
virtual void onInitialized(bool ok) override;
virtual void onSuccess(sdkbox::Product const& p) override;
virtual void onFailure(sdkbox::Product const& p, const std::string &msg) override;
virtual void onCanceled(sdkbox::Product const& p) override;
virtual void onRestored(sdkbox::Product const& p) override;
virtual void onProductRequestSuccess(std::vector<sdkbox::Product> const &products) override;
virtual void onProductRequestFailure(const std::string &msg) override;
void onRestoreComplete(bool ok, const std::string &msg);
}
- Create a listener that handles callbacks:
sdkbox::IAP::setListener(listener);
API Reference
Methods
static void init ( ) ;
Initialize SDKBox IAP
static void setDebug ( bool debug ) ;
Enable/disable debug logging
static void purchase ( const std::string & name ) ;
Make a purchase request
static void refresh ( ) ;
Refresh the IAP data(title, price, description)
static void restore ( ) ;
Restore purchase
static void setListener ( IAPListener * listener ) ;
Set listener for IAP
static void removeListener ( ) ;
Remove listener for IAP
Listeners
void onInitialized ( bool ok );
Called when IAP initialized
void onSuccess ( const Product & p );
Called when an IAP processed successfully
void onFailure ( const Product & p , const std::string & msg );
Called when an IAP fails
void onCanceled ( const Product & p );
Called when user canceled the IAP
void onRestored ( const Product & p );
Called when server returns the IAP items user already purchased @note this callback will be called multiple times if there are multiple IAP
void onProductRequestSuccess ( const std::vector <Product> & products );
Called the product request is successful, usually developers use product request to update the latest info(title, price) from IAP
void onProductRequestFailure ( const std::string & msg );
Called when the product request fails
void onRestoreComplete ( bool ok , const std::string & msg );
Called when the restore completed
Manual Integration
If the SDKBOX Installer fails to complete successfully, it is possible to integrate SDKBOX manually. If the installer complete successfully, please do not complete anymore of this document. It is not necessary.
These steps are listed last in this document on purpose as they are seldom needed. If you find yourself using these steps, please, after completing, double back and re-read the steps above for other integration items.
Manual Integration For iOS
Drag and drop the following frameworks from the plugins/ios folder of the IAP
bundle into your Xcode project, checkCopy items if needed
when adding frameworks:
sdkbox.framework
PluginIAP.framework
The above frameworks depend upon other frameworks. You also need to add the following system frameworks, if you don't already have them:
Security.framework
StoreKit.framework
AdSupport.framework
SystemConfiguration.framework
Manual Integration For Android
SDKBOX supports three different kinds of Android projects command-line, eclipse and Android Studio.
proj.android
will be used as our<project_root>
for command-line and eclipse projectproj.android-studio
will be used as our<project_root>
for Android Studio project.
Copy Files
Copy jar files
Copy the following jar files from plugin/android/libs
folder of this bundle into your project’s /libs folder.
PluginGooglePlay.jar
PluginIAP.jar
sdkbox.jar
-
If you're using cocos2d-x from source copy the jar files to:
Android command-line:
cocos2d/cocos/platform/android/java/libs
Android Studio:
cocos2d/cocos/platform/android/libcocos2dx/libs
-
If you're using cocos2d-js or lua copy the jar files to:
Android command-line:
frameworks/cocos2d-x/cocos/platform/android/java/libs
Android Studio:
frameworks/cocos2d-x/cocos/platform/android/libcocos2dx/libs
-
If you're using prebuilt cocos2d-x copy the jar files to:
Android command-line:
<project_root>/libs
Copy jni libs
Copy and overwrite all the folders from plugin/android/jni
to your <project_root>/jni/
directory.
Note: sdkbox link with
gnustl
by default, if your project link withc++static
please replace the files in<project_root>/jni/<plugin_name>/libs
with files in<project_root>/jni/<plugin_name>/libs_c++_static
folder
Edit AndroidManifest.xml
Include the following permissions above the application tag:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.vending.BILLING"/>
Edit Android.mk
Edit <project_root>/jni/Android.mk
to:
Add additional dependencies to LOCAL_WHOLE_STATIC_LIBRARIES:
LOCAL_WHOLE_STATIC_LIBRARIES += PluginIAP
LOCAL_WHOLE_STATIC_LIBRARIES += sdkbox
Add a call to:
$(call import-add-path,$(LOCAL_PATH))
before any import-module statements.
Add additional import-module statements at the end:
$(call import-module, ./sdkbox)
$(call import-module, ./pluginiap)
This means that your ordering should look similar to this:
$(call import-add-path,$(LOCAL_PATH))
$(call import-module, ./sdkbox)
$(call import-module, ./pluginiap)
Note: It is important to make sure these statements are above the existing $(call import-module,./prebuilt-mk)
statement, if you are using the pre-built libraries.
Modify Application.mk
(Cocos2d-x v3.0 - v3.2 only)
Edit <project_root>/jni/Application.mk
to make sure APP_STL is defined correctly. If Application.mk
containsAPP_STL := c++_static
, it should be changed to:
APP_STL := gnustl_static
Modify Cocos2dxActivity.java
-
If you're using cocos2d-x from source, assuming you are in the proj.android directory, Cocos2dxActivity.java is located:
../../cocos2d-x/cocos/platform/android/java/src/org/cocos2dx/ lib/Cocos2dxActivity.java
-
If you're using the prebuilt cocos2d-x libraries assuming you are in the
proj.android
directory, Cocos2dxActivity.java is located:./src/org/cocos2dx/lib/Cocos2dxActivity.java
Note: When using Cocos2d-x from source, different versions have Cocos2dxActivity.java in a different location. One way to find the location is to look in proj.android/project.properties. Example:android.library.reference.1=../../cocos2d-x/cocos/platform/android/java
In this case, Cocos2dxActivity.java should be located at:
../../cocos2d-x/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxActivity.java
- Modify Cocos2dxActivity.java to add the following imports:
import android.content.Intent;
import com.sdkbox.plugin.SDKBox;
- Second, modify Cocos2dxActivity.java to edit the
onCreate(final Bundle savedInstanceState)
function to add a call toSDKBox.init(this);
. The placement of this call is important. It must be done after the call toonLoadNativeLibraries();
. Example:
onLoadNativeLibraries();
SDKBox.init(this);
-
Last, we need to insert the proper overrides code. There are a few rules here.
-
If the method listed has not been defined, add it.
-
If the method listed has been defined, add the calls to
SDKBox
in the same existing function.
-
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(!SDKBox.onActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
protected void onStart() {
super.onStart();
SDKBox.onStart();
}
@Override
protected void onStop() {
super.onStop();
SDKBox.onStop();
}
@Override
protected void onResume() {
super.onResume();
SDKBox.onResume();
}
@Override
protected void onPause() {
super.onPause();
SDKBox.onPause();
}
@Override
public void onBackPressed() {
if(!SDKBox.onBackPressed()) {
super.onBackPressed();
}
}
Modify project.properties
An Android Library Reference for Google Play Services is required. The path will be different depending upon your setup. Also, this is an additional download that does not come as part of a standard install. To install use the sdk installer and choose extras->google play services. Here is an example of what this line could look like:
android.library.reference.1=
../android/sdk.latest/extras/google/google_play_services/libproject/
google-play-services_lib
Note: if you already have an android.library.reference.1
you can add another by incrementing the number asandroid.library.reference.2
, etc.
Proguard (optional)
- Edit
project.properties
to specify aProguard
configuration file. Example:
proguard.config=proguard.cfg
- proguard.cfg:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# cocos2d-x
-keep public class org.cocos2dx.** { *; }
-dontwarn org.cocos2dx.**
-keep public class com.chukong.** { *; }
-dontwarn com.chukong.**
# google play service
-keep public class com.google.android.gms.** { public *; }
-dontwarn com.google.android.gms.**
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
#sdkbox
-keep public class com.sdkbox.** { *; }
-dontwarn com.sdkbox.**
Note: Proguard only works with Release builds (i.e cocos run -m release
) debug builds do not invoke Proguard rules.