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

In-App Purchase Model in Unity 3D

Relations with IAP SDK

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).

enter image description here


Description of Library

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

Implement In-App Purchase

The description below is based on unity_sample project and IapSample.cs

Add Library to Unity3D Project

Place AndroidManifest.xml and library in /Assets /Plugins /Android

enter image description here

Initialization

	//-----------------
	// 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
	}

Query Processing

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 Processing

	//------------------------------------------------
	//
	// 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 Processing

	//------------------------------------------------
	//
	// 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);
	}

Termination Processing

iapRequestAdapter.Call ("exit");

Parsing of Passed Results

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.

Response Example (Payment)

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":"......."
		}
}

Error Example (Payment)

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."
}

When Modification IS Needed

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

Project Configuration

β”œβ”€β”€ 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

Description of Main File

File Description Remarks
RequestCallbackAdapter.java This file receives requests from Unity layer and defines callback methods

How to Build Library

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

⚠️ **GitHub.com Fallback** ⚠️