IAP Game Unity Developer Guide - ONE-store/inapp-sdk-eng GitHub Wiki
< NOTE >
This documents are for ONE store IAP SDK v16(API v4). If you are looking for the latest version of ONE store IAP SDK v17(API v5), please refer to the url below:
URL for the latest documents and downloads of ONE store IAP SDK v17(API v5) : https://dev.onestore.co.kr/devpoc/reference/view/IAP_v17
This instruction guide is to help you apply the IAP SDK for an Android App in Unity3D environment.
You can download default libraries (unity_adaptor) and test Apps related to Unity at Download.
All the Unity-related libraries and the test Apps provide source and project, and the developers are required to modify and use them according to their use.
- unity_adaptor : Library project to apply the IAP to Unity
- unity_sample :Unity application project implemented with unity_adaptor
You can use the library provided for In-App purchase (iap_plugin) without modification. Additional library (unity_adaptor) is generated to allow you to use this in the Unity3D layer. You can directly use this in the script file (IapSample.cs).
File Provided | Description | Remarks |
---|---|---|
libs/iap_plugin_[version]_[build date].jar | This is the IAP library | |
libs/unity_adaptor_[version]_[build date].jar | This is the Unity3D Adaptor library | |
src/IapSample.cs | This is an example file implemented with request and callback | |
src/AndroidManifest.xml | This is a file declared with meta-data and Default Acticity | |
src/Response.cs | This is used for parsing the JSON-format result | |
src/Error.cs | This is used for parsing the JSON-format result | |
src/Response.cs | This is used for parsing the JSON-format result |
The description below is based on unity_sample project and IapSample.cs
Place AndroidManifest.xml and library in /Assets /Plugins /Android
//-----------------
// Initialize
//-----------------
unityPlayerClass = new AndroidJavaClass ("com.unity3d.player.UnityPlayer");
currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject> ("currentActivity");
if (currentActivity != null) {
// Initialize RequestAdapter
// ---------------------------------
// Clean up function parameters
// ---------------------------------
// (1) Class name to receive callback
// (2) Activity Context
// (3) debuggable
iapRequestAdapter = new AndroidJavaObject("com.onestore.iap.unity.RequestAdapter", "IapSample", currentActivity, false); //Release
//iapRequestAdapter = new AndroidJavaObject("com.onestore.iap.unity.RequestAdapter", "IapSample", currentActivity, true); //Debug
}
Requests are made using iapRequestAdapter, and callbacks are received in common.
//------------------------------------------------
//
// Command - Request
//
//------------------------------------------------
public void RequestPurchaseHistory()
{
// ---------------------------------
// Clean up function parameters
// ---------------------------------
// (0) Method name : Check purchase history
// ---------------------------------
// (1) Expose UI if needed
// (2) appId
// (3) productIds
// ----------------------------------
string[] productIds = {"0910024112"};
iapRequestAdapter.Call ("requestPurchaseHistory", false, "OA00679020", productIds);
//iapRequestAdapter.Call ("requestPurchaseHistory", true, "OA00679020", productIds); // Background only without UI exposure
}
public void RequestProductInfo()
{
// ---------------------------------
// Clean up function parameters
// ---------------------------------
// (0) Method name : Check product information
// ---------------------------------
// (1) Expose UI if needed
// (2) appId
// ----------------------------------
iapRequestAdapter.Call ("requestProductInfo", false, "OA00679020");
//iapRequestAdapter.Call ("requestProductInfo", true, "OA00679020"); // Background only without UI exposure
}
public void RequestCheckPurchasability()
{
// ---------------------------------
// Clean up function parameters
// ---------------------------------
// (0) Method name : Check purchase availability
// ---------------------------------
// (1) Expose UI if needed
// (2) appId
// (3) productIds
// ----------------------------------
string[] productIds = {"0910024112"};
iapRequestAdapter.Call ("requestCheckPurchasability", false, "OA00679020", productIds);
//iapRequestAdapter.Call ("requestCheckPurchasability", true, "OA00679020", productIds); // Background only without UI exposure
}
public void RequestSubtractPoints()
{
// ---------------------------------
// Clean up function parameters
// ---------------------------------
// (0) Method name : Request to change product properties
// ---------------------------------
// (1) Expose UI if needed
// (2) action(the deduction of the item)
// (3) appId
// (4) productIds
// ----------------------------------
string[] productIds = {"0910024112"};
iapRequestAdapter.Call ("requestChangeProductProperties", false, "subtract_points", "OA00679020", productIds);
//iapRequestAdapter.Call ("requestChangeProductProperties", true, "subtract_points", "OA00679020", productIds); // Background only without UI exposure
}
public void RequestCancelSubscription()
{
// ---------------------------------
// Clean up function parameters
// ---------------------------------
// (0) Method name : Request to change product properties
// ---------------------------------
// (1) Expose UI if needed
// (2) action(cancellation of auto-renewal)
// (3) appId
// (4) productIds
// ----------------------------------
string[] productIds = {"0910042744"};
iapRequestAdapter.Call ("requestChangeProductProperties", false, "cancel_subscription", "OA00697454", productIds);
//iapRequestAdapter.Call ("requestChangeProductProperties", true, "cancel_subscription", "OA00697454", productIds); // // Background only without UI exposure
}
//------------------------------------------------
//
// Command - Callback
//
//------------------------------------------------
public void CommandResponse(string response)
{
Debug.Log ("[UNITY] CommandResponse >>> " + response);
// Parsing Json string to "Reponse" class
Response data = JsonUtility.FromJson<Response> (response);
}
public void CommandError(string message)
{
Debug.Log ("[UNITY] CommandError >>> " + message);
// Parsing Json string to "Error" class
Error data = JsonUtility.FromJson<Error> (message);
}
//------------------------------------------------
//
// Payment - Request
//
//------------------------------------------------
public void RequestPaymenet()
{
// ---------------------------------
// Clean up function parameters
// ---------------------------------
// (0) Method name : Request purchase
// ---------------------------------
// (1) appId
// (2) productId
// (3) proudtName
// (4) tId
// (5) bpInfo
// ----------------------------------
iapRequestAdapter.Call ("requestPayment", "OA00679020", "0910024112", "UNITYκ²°μ ", "TID_0123", "BPINFO_0123");
}
//------------------------------------------------
//
// Payment - Callback
//
//------------------------------------------------
public void PaymentResponse(string response)
{
Debug.Log ("[UNITY] PaymentResponse >>> " + response);
// Parsing Json string to "Reponse" class
Response data = JsonUtility.FromJson<Response> (response);
// Try ReceiptVerification
iapRequestAdapter.Call ("verifyReceipt", "OA00679020", data.result.txid, data.result.receipt);
}
public void PaymentError(string message)
{
Debug.Log ("[UNITY] PaymentError >>> " + message);
// Parsing Json string to "Error" class
Error data = JsonUtility.FromJson<Error> (message);
}
//------------------------------------------------
//
// e-Receipt verification - Request
//
//------------------------------------------------
public void VerifyReceipt()
{
// ---------------------------------
// Clean up function parameters
// ---------------------------------
// (0) Method name : Request purchase
// ---------------------------------
// (1) appId
// (2) txId
// (3) signData
// ----------------------------------
iapRequestAdapter.Call ("verifyReceipt", appId, txId, signData);
}
//------------------------------------------------
//
// e-Receipt verification - Callback
//
//------------------------------------------------
public void ReceiptVerificationResponse(string result)
{
Debug.Log ("[UNITY] ReceiptVerificationResponse >>> " + result);
// Parsing Json string to "VerifyReceipt" class
VerifyReceipt data = JsonUtility.FromJson<VerifyReceipt> (result);
}
public void ReceiptVerificationError(string message)
{
Debug.Log ("[UNITY] ReceiptVerificationError >>> " + message);
// Parsing Json string to "Error" class
Error data = JsonUtility.FromJson<Error> (message);
}
iapRequestAdapter.Call ("exit");
All the results passed to Unity layer are delivered in JSON-format string.
In addition, the resulting data classes can be provided as a file(C#) to assist with the resulting parsing.
- Response.cs : Data Class to load after parsing (query, payment) results
- VerifyReceipt.cs : Data class to load electronic receipt verification results
- Error.cs : Data class that will contain the results of error
In **unity_sample**use the JsonUtility provided by the Unity version 5.0 or higher for parsing.
If the Unity version is lower than 5.0, you can parse the results using the existing JSON Parser.
In case of SUCCESS, the result is the same as the outcome passed from the IAP SDK, and you can check it at IAP Sample Specification
{
"api_version":"4",
"identifier":"1477984099815",
"method":"purchase",
"result":
{
"code":"0000",
"message":"request was successful.",
"count":1,
"txid":"TSTOREXXXX_..",
"receipt":"......."
}
}
In case of ERROR, the result is selectively passed, using the following values as a key.
- requestId : request identifier
- errorCode : error code for request
- errorMessage : result message for error
Please note that some values are not passed in some cases
{
"requestId":"",
"errorCode":"-1001",
"errorMessage":"Service has not allowed."
}
In some cases, the developerβs environment requires you to modify unity_adaptor library.
In that case, you can generate unity_adaptor library by modifying Project
βββ build.gradle
βββ gradle
β βββ wrapper
β βββ gradle-wrapper.jar
βββ gradlew
βββ gradlew.bat
βββ library
β βββ build.gradle
β βββ libs
β β βββ classes.jar
β β βββ iap_plugin_[version]_[build date].jar
β βββ proguard-rules.pro
β βββ src
β βββ main
β βββ AndroidManifest.xml
β βββ java
β β βββ com
β β βββ onestore
β β βββ iap
β β βββ unity
β β βββ AbsCallbackAdapter.java
β β βββ MainActivity.java
β β βββ MessageMaker.java
β β βββ ReceiptVerificationCallbackAdapter.java
β β βββ RequestAdapter.java
β β βββ RequestCallbackAdapter.java
β βββ res
β βββ values
β βββ strings.xml
βββ settings.gradle
File | Description | Remarks |
---|---|---|
RequestCallbackAdapter.java | This file receives requests from Unity layer and defines callback methods |
You can perform gradle build, using the following command in the project:
./gradlew buildUnityAdaptorJar
The result is located in the path below:
unity_adaptor/library/build/outputs/aar/unity_adaptor_[version]_[build date].jar