[JAVA] How to easily implement in-app purchase using itemstore <Implementation: Android>

itemstore is a service that allows you to easily implement in-app purchases on Android and iOS apps. Troublesome in-app purchase processing can be realized with a simple source code, and management such as display / non-display of sales items can be easily performed from the item store screen.

The general flow of use is as follows.

[STEP1] Settings on the developer site [STEP2] Store settings [STEP3] Implementation

This time, I would like to summarize "[STEP3] Implementation: Android Edition".

Overview

Operating environment

Android 4.0 (API level 14) or later

Development environment

This tutorial assumes development in Android Studio.

SDK settings

Copy the downloaded SDK jar file to the libs folder.

If you cannot see the libs folder, change the display of Project in the Tool Window by selecting "Project" in the pull-down menu.

After deploying the SDK, perform a clean build by selecting "Sync Project with Gradle Files" on the toolbar or [Build]-> [Clean Project] from the menu.

Required support for API version 23 or later

If the corresponding API version is 23 or later, add the following under android of build.gradle in the app folder.

Required support for API version 23 or later


android {
  〜
  useLibrary 'org.apache.http.legacy'
  〜
}

Release build settings

proguard settings

If you want to release build with proguard enabled, add the following to proguard-rules.pro in the app folder.

proguard settings


-dontwarn org.apache.http.**
-keep class net.app_c.sdk.** { *; }

Interface implementation

To implement the itemstore billing function / push notification function, implement the OnAppCStartedListener interface in the class that is the main activity.

Example)

Implemented OnAppCStartedListener interface


public class MainActivity extends Activity implements AppC.OnAppCStartedListener {

    private AppC appc;

・ ・ ・

itemstore billing function

Sample code

itemstore billing function


public class MainActivity extends Activity implements AppC.OnAppCStartedListener {

    private AppC appc;

    〜

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        〜

        //itemstore initialization
        appc = new AppC(this).on(AppC.API.ITEM_STORE).setMediaKey(_MEDIA_KEY).start();

        〜

    }

    @Override
    public void onAppCStarted(boolean flg) {
        //Called after the itemstore initialization process is complete
        //If you want to call the itemstore related method immediately after starting the app,
        //Please do various processing after this callback is called.
    }

    〜

    @Override
    public void finish() {
        //itemstore exit
        appc.finish();
        
        super.finish();
    }

    〜

}

//itemstore method

//itemstore view call
appc.ItemStore.openItemStore();

//Get the group name of the billing item
String groupName = appc.ItemStore.getItem("Group ID").getName();

//Get the number of billing items you have
int itemCount = appc.ItemStore.getItemCount("Group ID");

//Increase or decrease the number of billing items
//If you want to reduce the number of possessions, enter a negative value in the second argument
appc.ItemStore.addItemCount("Group ID", 1);

//Set the specified value for the number of billing items you have
appc.ItemStore.setItemCount("Group ID", 10);

//Get inquiry key
String inquiryKey = appc.getInquiryKey();

Push notification function

Sample code

Push notification function


public class MainActivity extends Activity implements AppC.OnAppCStartedListener {

    private AppC appc;

    〜

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        〜

        //itemstore initialization
        appc = new AppC(this).on(AppC.API.PUSH).setMediaKey(_MEDIA_KEY).start();

        /*
By default, the Activity that starts when you tap the push notification is the Activity that initializes the appC SDK.
If you want to start any activity when you tap the push notification, specify the activity class in the second argument of on.
The icon displayed by push notification is ic by default_The launcher is specified
If you want to change the icon image displayed by push notification, specify any icon in the third argument of on.
        appc = new AppC(this).on(AppC.API.PUSH, MainActivity.class, R.drawable.ic_launcher)
                              .setMediaKey(_MEDIA_KEY)
                              .start();
        */
        〜

    }

    @Override
    public void onAppCStarted(boolean flg) {
        //Called after the itemstore initialization process is complete
        //If you want to call the itemstore related method immediately after starting the app,
        //Please do various processing after this callback is called.

        //You can get the delivery text (custom parameter) set in push delivery when you start it with push notification.
        //Example) Log.d("appc", "onAppCStarted:" + appc.Push.getParam());
    }

    @Override
    protected void onNewIntent(final Intent intent) {
        super.onNewIntent(intent);
        //You can get custom parameters when the app is in a task state and there is a push notification
        
        //You can get the delivery text (custom parameter) set in push delivery when you start it with push notification.
        //Example) Log.d("appc", "onNewIntent:" + appc.Push.getParam());
    }

    @Override
    public void finish() {
        //itemstore exit
        appc.finish();
        
        super.finish();
    }

    〜
}

//Push notification method

//Get delivery text (custom parameters)
String customParameter = appc.Push.getParam();

//Get inquiry key
String inquiryKey = appc.getInquiryKey();

AndroidManifest.xml settings

Please use it as needed. If there are other necessary items, please add them as appropriate.

AndroidManifest.xml settings


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="[package name]">

    <!-- require AppC -->
    <uses-permission android:name="android.permission.INTERNET" />
    
    <!-- require Push Start -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.VIBRATE" >
    </uses-permission>
    <uses-permission android:name="[package name].permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission
        android:name="[package name].permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <!-- require Push End -->
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- require Push Start -->
        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="[package name]" />
            </intent-filter>
        </receiver>

        <service android:name="net.app_c.sdk.AppCPushService" />
        <!-- require Push End -->

    </application>
</manifest>

Recommended Posts

How to easily implement in-app purchase using itemstore <Implementation: Android>
[Swift5] How to implement animation using "lottie-ios"
How to implement image posting using rails
How to implement the breadcrumb function using gretel
[Rails] How to easily implement numbers with pull-down
Rails learning How to implement search function using ActiveModel
[2020 version] How to send an email using Android Studio Javamail
How to make an app using Tensorflow with Android Studio
How to implement one-line display of TextView in Android development
[Rails] How to implement scraping
[Java] How to implement multithreading
How to authorize using graphql-ruby
[Swift] How to implement the Twitter login function using Firebase UI ①
How to implement UI automated test using image comparison in Selenium
[Swift] How to implement the Twitter login function using Firebase UI ②
How to "hollow" View on Android
[Java] Try to implement using generics
[Rails] How to implement star rating
[Android] How to make Dialog Fragment
How to build CloudStack using Docker
[Android] How to turn the Notification panel on and off using StatusBarManager
How to implement a slideshow using slick in Rails (one by one & multiple by one)
[Android] How to pass images and receive callbacks when sharing using ShareCompat
How to sort a List using Comparator
How to implement search functionality in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
[Rails] How to upload images using Carrierwave
[Android] How to deal with dark themes
How to detect microphone conflicts on Android
[Java] How to calculate age using LocalDate
[Swift] How to implement the countdown function
How to implement coding conventions in Java
Android Easily give a "press" to a button
How to implement TextInputLayout with validation function
[Swift5] How to implement standby screen using'PKHUD'
How to implement ranking functionality in Rails
How to make asynchronous pagenations using Kaminari
How to implement asynchronous processing in Outsystems
[Rails] How to handle data using enum
How to insert icons using Font awesome
How to use ExpandableListView in Android Studio
How to implement image posting function using Active Storage in Ruby on Rails
[Java] How to easily get the longest character string of ArrayList using stream