├── app
├── .gitignore
├── release
│ └── app-release.aab
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.webp
│ │ │ │ └── ic_launcher_round.webp
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── themes.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── xml
│ │ │ │ ├── backup_rules.xml
│ │ │ │ └── data_extraction_rules.xml
│ │ │ ├── values-night
│ │ │ │ └── themes.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── activity_consumable.xml
│ │ │ │ ├── activity_subscription.xml
│ │ │ │ └── activity_non_consumable.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── bayram
│ │ │ └── inappexample
│ │ │ ├── MainActivity.java
│ │ │ ├── NonConsumable.java
│ │ │ ├── Subscription.java
│ │ │ └── Consumable.java
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── bayram
│ │ │ └── inappexample
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── bayram
│ │ └── inappexample
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── README.md
├── .idea
├── .gitignore
├── compiler.xml
├── vcs.xml
├── deploymentTargetDropDown.xml
├── gradle.xml
└── misc.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── settings.gradle
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # android-in-app-purchasing
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/app/release/app-release.aab:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/app/release/app-release.aab
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/bayrameker/android-in-app-purchasing/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | IapSample
3 | \"Premium Activited :) \"
4 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Nov 10 12:06:45 PKT 2022
2 | distributionBase=GRADLE_USER_HOME
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
4 | distributionPath=wrapper/dists
5 | zipStorePath=wrapper/dists
6 | zipStoreBase=GRADLE_USER_HOME
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 | local.properties
16 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | gradlePluginPortal()
4 | google()
5 | mavenCentral()
6 | }
7 | }
8 | dependencyResolutionManagement {
9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
10 | repositories {
11 | google()
12 | mavenCentral()
13 | }
14 | }
15 | rootProject.name = "IapSample"
16 | include ':app'
17 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/app/src/test/java/com/bayram/inappexample/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.bayram.inappexample;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/main/res/xml/backup_rules.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
--------------------------------------------------------------------------------
/.idea/deploymentTargetDropDown.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/data_extraction_rules.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
12 |
13 |
19 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/bayram/inappexample/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.bayram.inappexample;
2 |
3 | import android.content.Context;
4 |
5 | import androidx.test.platform.app.InstrumentationRegistry;
6 | import androidx.test.ext.junit.runners.AndroidJUnit4;
7 |
8 | import org.junit.Test;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.junit.Assert.*;
12 |
13 | /**
14 | * Instrumented test, which will execute on an Android device.
15 | *
16 | * @see Testing documentation
17 | */
18 | @RunWith(AndroidJUnit4.class)
19 | public class ExampleInstrumentedTest {
20 | @Test
21 | public void useAppContext() {
22 | // Context of the app under test.
23 | Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24 | assertEquals("com.memojis.iapsample", appContext.getPackageName());
25 | }
26 | }
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
21 |
22 |
27 |
28 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_consumable.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
16 |
17 |
23 |
24 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_subscription.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
16 |
17 |
23 |
24 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_non_consumable.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
16 |
17 |
23 |
24 |
29 |
30 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | }
4 |
5 | android {
6 | compileSdk 32
7 |
8 | defaultConfig {
9 | applicationId "com.bayram.inappexample"
10 | minSdk 24
11 | targetSdk 32
12 | versionCode 12
13 | versionName "12.0"
14 |
15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 | compileOptions {
25 | sourceCompatibility JavaVersion.VERSION_1_8
26 | targetCompatibility JavaVersion.VERSION_1_8
27 | }
28 | }
29 |
30 | dependencies {
31 |
32 | implementation 'com.android.billingclient:billing:5.1.0'
33 | implementation 'androidx.appcompat:appcompat:1.5.1'
34 | implementation 'com.google.android.material:material:1.7.0'
35 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
36 | testImplementation 'junit:junit:4.13.2'
37 | androidTestImplementation 'androidx.test.ext:junit:1.1.3'
38 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
39 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Enables namespacing of each library's R class so that its R class includes only the
19 | # resources declared in the library itself and none from the library's dependencies,
20 | # thereby reducing the size of the R class for that library
21 | android.nonTransitiveRClass=true
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
16 |
19 |
22 |
25 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bayram/inappexample/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.bayram.inappexample;
2 |
3 | import androidx.appcompat.app.AppCompatActivity;
4 |
5 | import android.content.Intent;
6 | import android.os.Bundle;
7 | import android.view.View;
8 | import android.widget.Button;
9 |
10 | public class MainActivity extends AppCompatActivity {
11 |
12 | Button btn_nonconsumeable,btn_consumeable,btn_subscription;
13 | Intent intent = null;
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | setContentView(R.layout.activity_main);
18 | init();
19 | }
20 |
21 | private void init(){
22 | btn_nonconsumeable = this.findViewById(R.id.btn_nonconsumeable);
23 | btn_consumeable = this.findViewById(R.id.btn_consumeable);
24 | btn_subscription = this.findViewById(R.id.btn_subscription);
25 |
26 | btn_nonconsumeable.setOnClickListener(new View.OnClickListener() {
27 | @Override
28 | public void onClick(View view) {
29 | intent = new Intent(MainActivity.this, NonConsumable.class);
30 | startActivity(intent);
31 | }
32 | });
33 |
34 | btn_consumeable.setOnClickListener(new View.OnClickListener() {
35 | @Override
36 | public void onClick(View view) {
37 |
38 | intent = new Intent(MainActivity.this, Consumable.class);
39 | startActivity(intent);
40 | }
41 | });
42 |
43 | btn_subscription.setOnClickListener(new View.OnClickListener() {
44 | @Override
45 | public void onClick(View view) {
46 |
47 | intent = new Intent(MainActivity.this, Subscription.class);
48 | startActivity(intent);
49 | }
50 | });
51 | }
52 | }
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bayram/inappexample/NonConsumable.java:
--------------------------------------------------------------------------------
1 | package com.bayram.inappexample;
2 |
3 | import android.os.Bundle;
4 | import android.util.Log;
5 | import android.view.View;
6 | import android.widget.Button;
7 | import android.widget.TextView;
8 |
9 | import androidx.annotation.NonNull;
10 | import androidx.appcompat.app.AppCompatActivity;
11 |
12 | import com.android.billingclient.api.AcknowledgePurchaseParams;
13 | import com.android.billingclient.api.BillingClient;
14 | import com.android.billingclient.api.BillingClientStateListener;
15 | import com.android.billingclient.api.BillingFlowParams;
16 | import com.android.billingclient.api.BillingResult;
17 | import com.android.billingclient.api.ProductDetails;
18 | import com.android.billingclient.api.ProductDetailsResponseListener;
19 | import com.android.billingclient.api.Purchase;
20 | import com.android.billingclient.api.QueryProductDetailsParams;
21 | import com.android.billingclient.api.QueryPurchasesParams;
22 |
23 | import java.util.ArrayList;
24 | import java.util.List;
25 |
26 | public class NonConsumable extends AppCompatActivity {
27 |
28 | private final String PRODUCT_PREMIUM = "lifetime";
29 | private final String NoAds = "NoAds";
30 | private ArrayList purchaseItemIDs = new ArrayList() {{
31 | add(PRODUCT_PREMIUM);
32 | add(NoAds);
33 | }};
34 |
35 | private String TAG = "iapSample";
36 |
37 | private BillingClient billingClient;
38 |
39 | Button btn_premium, btn_restore;
40 | TextView tv_status;
41 |
42 | @Override
43 | protected void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 | setContentView(R.layout.activity_non_consumable);
46 |
47 | billingClient = BillingClient.newBuilder(this)
48 | .enablePendingPurchases()
49 | .setListener(
50 | (billingResult, list) -> {
51 |
52 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list != null) {
53 | for (Purchase purchase : list) {
54 |
55 | Log.d(TAG, "Response is OK");
56 | handlePurchase(purchase);
57 | }
58 | } else {
59 |
60 | Log.d(TAG, "Response NOT OK");
61 | }
62 | }
63 | ).build();
64 |
65 | //start the connection after initializing the billing client
66 | establishConnection();
67 | init();
68 | }
69 |
70 | void init() {
71 | btn_premium = this.findViewById(R.id.btn_premium);
72 | btn_restore = this.findViewById(R.id.btn_restore);
73 | tv_status = this.findViewById(R.id.tv_status);
74 |
75 | btn_premium.setOnClickListener(new View.OnClickListener() {
76 | @Override
77 | public void onClick(View view) {
78 | GetSingleInAppDetail();
79 | }
80 | });
81 |
82 | btn_restore.setOnClickListener(new View.OnClickListener() {
83 | @Override
84 | public void onClick(View view) {
85 | restorePurchases();
86 | }
87 | });
88 | }
89 |
90 |
91 | void establishConnection() {
92 | billingClient.startConnection(new BillingClientStateListener() {
93 | @Override
94 | public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
95 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
96 |
97 | // The BillingClient is ready. You can query purchases here.
98 |
99 | //Use any of function below to get details upon successful connection
100 |
101 | // GetSingleInAppDetail();
102 | //GetListsInAppDetail();
103 |
104 | Log.d(TAG, "Connection Established");
105 | }
106 | }
107 |
108 | @Override
109 | public void onBillingServiceDisconnected() {
110 | // Try to restart the connection on the next request to
111 | // Google Play by calling the startConnection() method.
112 | Log.d(TAG, "Connection NOT Established");
113 | establishConnection();
114 | }
115 | });
116 | }
117 |
118 | /*
119 | *
120 | * The official examples use an ImmutableList for some reason to build the query,
121 | * but you don't actually need to use that.
122 | * The setProductList method just takes List as its input, it does not require ImmutableList.
123 | *
124 | * */
125 |
126 | /*
127 | * If you have API < 24, you could just make an ArrayList instead.
128 | * */
129 |
130 | void GetSingleInAppDetail() {
131 | ArrayList productList = new ArrayList<>();
132 |
133 | //Set your In App Product ID in setProductId()
134 | productList.add(
135 | QueryProductDetailsParams.Product.newBuilder()
136 | .setProductId(PRODUCT_PREMIUM)
137 | .setProductType(BillingClient.ProductType.INAPP)
138 | .build()
139 | );
140 |
141 | QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder()
142 | .setProductList(productList)
143 | .build();
144 |
145 | billingClient.queryProductDetailsAsync(params, new ProductDetailsResponseListener() {
146 | @Override
147 | public void onProductDetailsResponse(@NonNull BillingResult billingResult, @NonNull List list) {
148 |
149 | //Do Anything that you want with requested product details
150 |
151 | //Calling this function here so that once products are verified we can start the purchase behavior.
152 | //You can save this detail in separate variable or list to call them from any other location
153 | //Create another function if you want to call this in establish connections' success state
154 | LaunchPurchaseFlow(list.get(0));
155 |
156 |
157 | }
158 | });
159 | }
160 |
161 |
162 | void GetListsInAppDetail() {
163 | ArrayList productList = new ArrayList<>();
164 |
165 | //Set your In App Product ID in setProductId()
166 | for (String ids : purchaseItemIDs) {
167 | productList.add(
168 | QueryProductDetailsParams.Product.newBuilder()
169 | .setProductId(ids)
170 | .setProductType(BillingClient.ProductType.INAPP)
171 | .build());
172 | }
173 |
174 | QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder()
175 | .setProductList(productList)
176 | .build();
177 |
178 | billingClient.queryProductDetailsAsync(params, new ProductDetailsResponseListener() {
179 | @Override
180 | public void onProductDetailsResponse(@NonNull BillingResult billingResult, @NonNull List list) {
181 |
182 | for (ProductDetails li : list) {
183 | Log.d(TAG, "IN APP item Price" + li.getOneTimePurchaseOfferDetails().getFormattedPrice());
184 | }
185 | //Do Anything that you want with requested product details
186 | }
187 | });
188 | }
189 |
190 | void LaunchPurchaseFlow(ProductDetails productDetails) {
191 | ArrayList productList = new ArrayList<>();
192 |
193 | productList.add(
194 | BillingFlowParams.ProductDetailsParams.newBuilder()
195 | .setProductDetails(productDetails)
196 | .build());
197 |
198 | BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
199 | .setProductDetailsParamsList(productList)
200 | .build();
201 |
202 | billingClient.launchBillingFlow(this, billingFlowParams);
203 | }
204 |
205 | void handlePurchase(Purchase purchases) {
206 | if (!purchases.isAcknowledged()) {
207 | billingClient.acknowledgePurchase(AcknowledgePurchaseParams
208 | .newBuilder()
209 | .setPurchaseToken(purchases.getPurchaseToken())
210 | .build(), billingResult -> {
211 |
212 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
213 | for (String pur : purchases.getProducts()) {
214 | if (pur.equalsIgnoreCase(PRODUCT_PREMIUM)) {
215 | Log.d("TAG", "Purchase is successful" + pur);
216 | tv_status.setText("Yay! Purchased");
217 | }
218 | }
219 | }
220 | });
221 | }
222 | }
223 |
224 | @Override
225 | protected void onResume() {
226 | super.onResume();
227 |
228 | }
229 |
230 | void restorePurchases() {
231 |
232 | billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener((billingResult, list) -> {
233 | }).build();
234 | final BillingClient finalBillingClient = billingClient;
235 | billingClient.startConnection(new BillingClientStateListener() {
236 | @Override
237 | public void onBillingServiceDisconnected() {
238 | }
239 |
240 | @Override
241 | public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
242 |
243 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
244 | finalBillingClient.queryPurchasesAsync(
245 | QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.INAPP).build(), (billingResult1, list) -> {
246 | if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.OK) {
247 | if (list.size() > 0) {
248 |
249 | Log.d("TAG", "IN APP SUCCESS RESTORE: " + list);
250 | for (int i = 0; i < list.size(); i++) {
251 |
252 | if (list.get(i).getProducts().contains(PRODUCT_PREMIUM)) {
253 | tv_status.setText("Premium Restored");
254 | Log.d("TAG", "Product id " + PRODUCT_PREMIUM + " will restore here");
255 | }
256 |
257 | }
258 | } else {
259 | tv_status.setText("Nothing found to Restored");
260 | Log.d("TAG", "In APP Not Found To Restore");
261 | }
262 | }
263 | });
264 | }
265 | }
266 | });
267 | }
268 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/bayram/inappexample/Subscription.java:
--------------------------------------------------------------------------------
1 | package com.bayram.inappexample;
2 |
3 | import android.os.Bundle;
4 | import android.util.Log;
5 | import android.view.View;
6 | import android.widget.Button;
7 | import android.widget.TextView;
8 |
9 | import androidx.annotation.NonNull;
10 | import androidx.appcompat.app.AppCompatActivity;
11 |
12 | import com.android.billingclient.api.AcknowledgePurchaseParams;
13 | import com.android.billingclient.api.BillingClient;
14 | import com.android.billingclient.api.BillingClientStateListener;
15 | import com.android.billingclient.api.BillingFlowParams;
16 | import com.android.billingclient.api.BillingResult;
17 | import com.android.billingclient.api.ProductDetails;
18 | import com.android.billingclient.api.ProductDetailsResponseListener;
19 | import com.android.billingclient.api.Purchase;
20 | import com.android.billingclient.api.QueryProductDetailsParams;
21 | import com.android.billingclient.api.QueryPurchasesParams;
22 |
23 | import java.util.ArrayList;
24 | import java.util.List;
25 |
26 | public class Subscription extends AppCompatActivity {
27 |
28 | private static String PRODUCT_PREMIUM = "yearly";
29 | private static String PRODUCT_MONTHLY = "monthly";
30 | private BillingClient billingClient;
31 |
32 | Button btn_premium, btn_restore;
33 | TextView tv_status;
34 | private ArrayList purchaseItemIDs = new ArrayList() {{
35 | add(PRODUCT_PREMIUM);
36 | add(PRODUCT_MONTHLY);
37 | }};
38 | private String TAG = "iapSample";
39 | @Override
40 | protected void onCreate(Bundle savedInstanceState) {
41 | super.onCreate(savedInstanceState);
42 | setContentView(R.layout.activity_subscription);
43 | billingClient = BillingClient.newBuilder(this)
44 | .enablePendingPurchases()
45 | .setListener(
46 | (billingResult, list) -> {
47 |
48 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list != null) {
49 | for (Purchase purchase : list) {
50 | verifySubPurchase(purchase);
51 | }
52 | }
53 | }
54 | ).build();
55 |
56 | //start the connection after initializing the billing client
57 | establishConnection();
58 | init();
59 | }
60 |
61 | @Override
62 | protected void onResume() {
63 | super.onResume();
64 | billingClient.queryPurchasesAsync(
65 | QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build(),
66 | (billingResult, list) -> {
67 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
68 | for (Purchase purchase : list) {
69 | if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED && !purchase.isAcknowledged()) {
70 | verifySubPurchase(purchase);
71 | }
72 | }
73 | }
74 | }
75 | );
76 | }
77 |
78 | void init() {
79 | btn_premium = this.findViewById(R.id.btn_premium);
80 | btn_restore = this.findViewById(R.id.btn_restore);
81 | tv_status = this.findViewById(R.id.tv_status);
82 |
83 | btn_premium.setOnClickListener(new View.OnClickListener() {
84 | @Override
85 | public void onClick(View view) {
86 | GetSubPurchases();
87 | }
88 | });
89 |
90 | btn_restore.setOnClickListener(new View.OnClickListener() {
91 | @Override
92 | public void onClick(View view) {
93 | restorePurchases();
94 | }
95 | });
96 | }
97 |
98 | void establishConnection() {
99 | billingClient.startConnection(new BillingClientStateListener() {
100 | @Override
101 | public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
102 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
103 | // The BillingClient is ready. You can query purchases here.
104 | //Use any of function below to get details upon successful connection
105 | Log.d("TAG", "Connection Established");
106 | }
107 | }
108 |
109 | @Override
110 | public void onBillingServiceDisconnected() {
111 | // Try to restart the connection on the next request to
112 | // Google Play by calling the startConnection() method.
113 | Log.d("TAG", "Connection NOT Established");
114 | establishConnection();
115 | }
116 | });
117 | }
118 |
119 | void GetSubPurchases() {
120 | ArrayList productList = new ArrayList<>();
121 |
122 | productList.add(
123 | QueryProductDetailsParams.Product.newBuilder()
124 | .setProductId(PRODUCT_PREMIUM)
125 | .setProductType(BillingClient.ProductType.SUBS)
126 | .build()
127 | );
128 |
129 | QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder()
130 | .setProductList(productList)
131 | .build();
132 |
133 |
134 | billingClient.queryProductDetailsAsync(params, new ProductDetailsResponseListener() {
135 | @Override
136 | public void onProductDetailsResponse(@NonNull BillingResult billingResult, @NonNull List list) {
137 | LaunchSubPurchase(list.get(0));
138 | Log.d("TAG", "Product Price" + list.get(0).getSubscriptionOfferDetails().get(0).getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice());
139 |
140 | }
141 | });
142 | }
143 | //Call this function using PRODUCT_PREMIUM or PRODUCT_MONTHLY as parameters.
144 | void GetListsSubDetail(String SKU) {
145 | ArrayList productList = new ArrayList<>();
146 |
147 | //Set your In App Product ID in setProductId()
148 | for (String ids : purchaseItemIDs) {
149 | productList.add(
150 | QueryProductDetailsParams.Product.newBuilder()
151 | .setProductId(ids)
152 | .setProductType(BillingClient.ProductType.SUBS)
153 | .build());
154 | }
155 |
156 | QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder()
157 | .setProductList(productList)
158 | .build();
159 |
160 | billingClient.queryProductDetailsAsync(params, new ProductDetailsResponseListener() {
161 | @Override
162 | public void onProductDetailsResponse(@NonNull BillingResult billingResult, @NonNull List list) {
163 | // Log.d(TAG, "Total size is: " + list);
164 |
165 | for (ProductDetails li : list) {
166 | if (li.getProductId().equalsIgnoreCase(SKU) && SKU.equalsIgnoreCase(PRODUCT_MONTHLY)) {
167 | LaunchSubPurchase(li);
168 | Log.d(TAG, "Monthly Price is " + li.getSubscriptionOfferDetails().get(0).getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice());
169 | return;
170 | } else if (li.getProductId().equalsIgnoreCase(SKU) && SKU.equalsIgnoreCase(PRODUCT_PREMIUM)) {
171 | LaunchSubPurchase(li);
172 | Log.d(TAG, "Yearly Price is " + li.getSubscriptionOfferDetails().get(0).getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice());
173 | return;
174 | }
175 |
176 | }
177 | //Do Anything that you want with requested product details
178 | }
179 | });
180 | }
181 | void LaunchSubPurchase(ProductDetails productDetails) {
182 | assert productDetails.getSubscriptionOfferDetails() != null;
183 | ArrayList productList = new ArrayList<>();
184 |
185 | productList.add(
186 | BillingFlowParams.ProductDetailsParams.newBuilder()
187 | .setProductDetails(productDetails)
188 | .setOfferToken(productDetails.getSubscriptionOfferDetails().get(0).getOfferToken())
189 | .build()
190 | );
191 |
192 | BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
193 | .setProductDetailsParamsList(productList)
194 | .build();
195 |
196 | billingClient.launchBillingFlow(this, billingFlowParams);
197 | }
198 |
199 | void verifySubPurchase(Purchase purchases) {
200 | if (!purchases.isAcknowledged()) {
201 | billingClient.acknowledgePurchase(AcknowledgePurchaseParams
202 | .newBuilder()
203 | .setPurchaseToken(purchases.getPurchaseToken())
204 | .build(), billingResult -> {
205 |
206 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
207 | for (String pur : purchases.getProducts()) {
208 | if (pur.equalsIgnoreCase(PRODUCT_PREMIUM)) {
209 | Log.d("TAG", "Purchase is successful" + pur);
210 | tv_status.setText("Yay! Purchased");
211 | }
212 | }
213 | }
214 | });
215 | }
216 | }
217 |
218 | void restorePurchases() {
219 |
220 | billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener((billingResult, list) -> {
221 | }).build();
222 | final BillingClient finalBillingClient = billingClient;
223 | billingClient.startConnection(new BillingClientStateListener() {
224 | @Override
225 | public void onBillingServiceDisconnected() {
226 | }
227 |
228 | @Override
229 | public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
230 |
231 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
232 | finalBillingClient.queryPurchasesAsync(
233 | QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build(), (billingResult1, list) -> {
234 | if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.OK) {
235 | if (list.size() > 0) {
236 | for (int i = 0; i < list.size(); i++) {
237 | if (list.get(i).getProducts().contains(PRODUCT_PREMIUM)) {
238 | tv_status.setText("Premium Restored");
239 | Log.d("TAG", "Product id " + PRODUCT_PREMIUM + " will restore here");
240 | }
241 | }
242 | } else {
243 | tv_status.setText("Nothing found to Restored");
244 | }
245 | }
246 | });
247 | }
248 | }
249 | });
250 | }
251 |
252 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/bayram/inappexample/Consumable.java:
--------------------------------------------------------------------------------
1 | package com.bayram.inappexample;
2 |
3 | import androidx.annotation.NonNull;
4 | import androidx.appcompat.app.AppCompatActivity;
5 |
6 | import android.os.Bundle;
7 | import android.util.Log;
8 | import android.view.View;
9 | import android.widget.Button;
10 | import android.widget.TextView;
11 |
12 | import com.android.billingclient.api.AcknowledgePurchaseParams;
13 | import com.android.billingclient.api.BillingClient;
14 | import com.android.billingclient.api.BillingClientStateListener;
15 | import com.android.billingclient.api.BillingFlowParams;
16 | import com.android.billingclient.api.BillingResult;
17 | import com.android.billingclient.api.ConsumeParams;
18 | import com.android.billingclient.api.ConsumeResponseListener;
19 | import com.android.billingclient.api.ProductDetails;
20 | import com.android.billingclient.api.ProductDetailsResponseListener;
21 | import com.android.billingclient.api.Purchase;
22 | import com.android.billingclient.api.QueryProductDetailsParams;
23 | import com.android.billingclient.api.QueryPurchasesParams;
24 |
25 | import java.util.ArrayList;
26 | import java.util.List;
27 |
28 | public class Consumable extends AppCompatActivity {
29 |
30 | private final String PRODUCT_PREMIUM = "lifetime";
31 | private final String ConsumeProductID = "consumabletest";
32 | private final ArrayList purchaseItemIDs = new ArrayList() {{
33 | add(PRODUCT_PREMIUM);
34 | add(ConsumeProductID);
35 | }};
36 |
37 | private final String TAG = "iapSample";
38 |
39 | private BillingClient billingClient;
40 | private int tries=1;
41 | private int maxTries= 3;
42 |
43 | private int connectionAttempts = 0;
44 |
45 | boolean isConnectionEstablished=false;
46 | Button btn_premium, btn_restore;
47 | TextView tv_status;
48 |
49 | @Override
50 | protected void onCreate(Bundle savedInstanceState) {
51 | super.onCreate(savedInstanceState);
52 | setContentView(R.layout.activity_consumable);
53 |
54 | billingClient = BillingClient.newBuilder(this)
55 | .enablePendingPurchases()
56 | .setListener(
57 | (billingResult, list) -> {
58 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list != null) {
59 | for (Purchase purchase : list) {
60 |
61 | // TODO: we will send order id to the backend before handle purchase
62 | String orderId = purchase.getOrderId();
63 | Log.d(TAG, "Response is OK");
64 | handlePurchase(purchase);
65 | }
66 | } else {
67 |
68 | Log.d(TAG, "Response NOT OK");
69 | }
70 | }
71 | ).build();
72 |
73 | //start the connection after initializing the billing client
74 | establishConnection();
75 | init();
76 | }
77 |
78 |
79 |
80 | void init() {
81 | btn_premium = this.findViewById(R.id.btn_premium);
82 | btn_restore = this.findViewById(R.id.btn_restore);
83 | tv_status = this.findViewById(R.id.tv_status);
84 |
85 | btn_premium.setOnClickListener(new View.OnClickListener() {
86 | @Override
87 | public void onClick(View view) {
88 | GetSingleInAppDetail();
89 | }
90 | });
91 |
92 | btn_restore.setOnClickListener(new View.OnClickListener() {
93 | @Override
94 | public void onClick(View view) {
95 | restorePurchases();
96 | }
97 | });
98 | }
99 |
100 |
101 |
102 | private void establishConnection() {
103 | billingClient.startConnection(new BillingClientStateListener() {
104 | @Override
105 | public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
106 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
107 | Log.e("err", String.valueOf(billingResult.getResponseCode()));
108 | Log.e("err","baglamtı başarılı");
109 |
110 | // Bağlantı başarıyla kuruldu
111 | // The BillingClient is ready. You can query purchases here.
112 | //Use any of function below to get details upon successful connection
113 | // GetSingleInAppDetail();
114 | //GetListsInAppDetail();
115 | } else {
116 | errorControl(billingResult.getResponseCode());
117 | }
118 | }
119 |
120 | @Override
121 | public void onBillingServiceDisconnected() {
122 | // Bağlantı kesildiğinde yeniden bağlanmak için yeniden bağlanmayı deneyin
123 | // TODO: 11.04.2023 alttaki yerine retryBillingServiceConnection() eklenecek ve 3 deneme olacak
124 | retryBillingServiceConnection();
125 | }
126 | });
127 | }
128 |
129 | void errorControl(int response){
130 |
131 | if(response == BillingClient.BillingResponseCode.BILLING_UNAVAILABLE ||
132 | response == BillingClient.BillingResponseCode.DEVELOPER_ERROR||
133 | response == BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED ||
134 | response == BillingClient.BillingResponseCode.USER_CANCELED){
135 | // non retriable responses
136 | handleBillingError(response);
137 | }
138 | else{
139 | // Bağlantı başarısız oldu 3 kere tekrar dene
140 | retryBillingServiceConnection();
141 | }
142 |
143 | }
144 |
145 | void retryBillingServiceConnection(){
146 | tries=1;
147 | maxTries= 3;
148 | isConnectionEstablished = false;
149 | do{
150 | try {
151 | billingClient.startConnection(new BillingClientStateListener() {
152 | @Override
153 | public void onBillingServiceDisconnected() {
154 | retryBillingServiceConnection();
155 | }
156 |
157 | @Override
158 | public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
159 | tries++;
160 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
161 | isConnectionEstablished = true;
162 |
163 | }
164 | else if(tries==maxTries){
165 | handleBillingError(billingResult.getResponseCode());
166 | }
167 | }
168 | });
169 | }
170 | catch (Exception e){
171 | tries++;
172 | }}
173 | while (tries <= maxTries && !isConnectionEstablished);
174 |
175 | if(isConnectionEstablished==false){
176 | handleBillingError(-1);
177 | }
178 |
179 | }
180 |
181 |
182 |
183 |
184 | /*
185 | *
186 | * The official examples use an ImmutableList for some reason to build the query,
187 | * but you don't actually need to use that.
188 | * The setProductList method just takes List as its input, it does not require ImmutableList.
189 | *
190 | * */
191 |
192 | /*
193 | * If you have API < 24, you could just make an ArrayList instead.
194 | * */
195 |
196 | void GetSingleInAppDetail() {
197 | ArrayList productList = new ArrayList<>();
198 |
199 | //Set your In App Product ID in setProductId()
200 | productList.add(
201 | QueryProductDetailsParams.Product.newBuilder()
202 | .setProductId(ConsumeProductID)
203 | .setProductType(BillingClient.ProductType.INAPP)
204 | .build()
205 | );
206 |
207 | QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder()
208 | .setProductList(productList)
209 | .build();
210 |
211 | billingClient.queryProductDetailsAsync(params, new ProductDetailsResponseListener() {
212 |
213 | @Override
214 | public void onProductDetailsResponse(@NonNull BillingResult billingResult, @NonNull List list) {
215 |
216 | //Do Anything that you want with requested product details
217 |
218 | //Calling this function here so that once products are verified we can start the purchase behavior.
219 | //You can save this detail in separate variable or list to call them from any other location
220 | //Create another function if you want to call this in establish connections' success state
221 | LaunchPurchaseFlow(list.get(0));
222 |
223 |
224 | }
225 | });
226 | }
227 |
228 | void GetListsInAppDetail() {
229 | ArrayList productList = new ArrayList<>();
230 |
231 | //Set your In App Product ID in setProductId()
232 | for (String ids : purchaseItemIDs) {
233 | productList.add(
234 | QueryProductDetailsParams.Product.newBuilder()
235 | .setProductId(ids)
236 | .setProductType(BillingClient.ProductType.INAPP)
237 | .build());
238 | }
239 |
240 | QueryProductDetailsParams params = QueryProductDetailsParams.newBuilder()
241 | .setProductList(productList)
242 | .build();
243 |
244 | billingClient.queryProductDetailsAsync(params, new ProductDetailsResponseListener() {
245 | @Override
246 | public void onProductDetailsResponse(@NonNull BillingResult billingResult, @NonNull List list) {
247 |
248 | for (ProductDetails li : list) {
249 | Log.d(TAG, "IN APP item Price" + li.getOneTimePurchaseOfferDetails().getFormattedPrice());
250 | }
251 | //Do Anything that you want with requested product details
252 | }
253 | });
254 | }
255 |
256 | //This function will be called in handlepurchase() after success of any consumeable purchase
257 | void ConsumePurchase(Purchase purchase) {
258 |
259 | ConsumeParams params = ConsumeParams.newBuilder()
260 | .setPurchaseToken(purchase.getPurchaseToken())
261 | .build();
262 | billingClient.consumeAsync(params, new ConsumeResponseListener() {
263 | @Override
264 | public void onConsumeResponse(@NonNull BillingResult billingResult, @NonNull String s) {
265 |
266 | Log.d("TAG", "Consuming Successful: "+s);
267 | tv_status.setText("Product Consumed");
268 | }
269 | });
270 | }
271 |
272 | void LaunchPurchaseFlow(ProductDetails productDetails) {
273 | ArrayList productList = new ArrayList<>();
274 |
275 | productList.add(
276 | BillingFlowParams.ProductDetailsParams.newBuilder()
277 | .setProductDetails(productDetails)
278 | .build());
279 |
280 | BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
281 | .setProductDetailsParamsList(productList)
282 | .build();
283 |
284 | billingClient.launchBillingFlow(this, billingFlowParams);
285 | }
286 |
287 | void handlePurchase(Purchase purchases) {
288 |
289 | if (!purchases.isAcknowledged()) {
290 |
291 | billingClient.acknowledgePurchase(AcknowledgePurchaseParams
292 | .newBuilder()
293 | .setPurchaseToken(purchases.getPurchaseToken())
294 | .build(), billingResult -> {
295 |
296 | /* ConsumeResponseListener listener = new ConsumeResponseListener() {
297 | @Override
298 | public void onConsumeResponse(@NonNull BillingResult billingResult, @NonNull String purchaseToken) {
299 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
300 | // Handle the success of the consume operation.
301 | }
302 | }
303 | }; */
304 |
305 | /* ConsumeParams consumeParams =
306 | ConsumeParams.newBuilder()
307 | .setPurchaseToken(purchases.getPurchaseToken())
308 | .build();
309 | *
310 | */
311 |
312 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
313 | for (String pur : purchases.getProducts()) {
314 | if (pur.equalsIgnoreCase(ConsumeProductID)) {
315 | Log.d("TAG", "Purchase is successful");
316 | tv_status.setText("Yay! Purchased");
317 |
318 | //Calling Consume to consume the current purchase
319 | // so user will be able to buy same product again
320 | // billingClient.consumeAsync(consumeParams, listener); // you should have listener method !
321 |
322 |
323 | // TODO: 11.04.2023 bu kısım backend tarafında yapılacak
324 | ConsumePurchase(purchases);
325 | }
326 | }
327 | }
328 | else{
329 | handleBillingError(billingResult.getResponseCode());
330 | }
331 | });
332 | }
333 | }
334 |
335 | @Override
336 | protected void onResume() {
337 | super.onResume();
338 |
339 | }
340 |
341 | void restorePurchases() {
342 |
343 | billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener((billingResult, list) -> {
344 | }).build();
345 | final BillingClient finalBillingClient = billingClient;
346 | billingClient.startConnection(new BillingClientStateListener() {
347 | @Override
348 | public void onBillingServiceDisconnected() {
349 | Log.e("erorr","geldi");
350 | }
351 |
352 | @Override
353 | public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
354 |
355 | if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
356 | finalBillingClient.queryPurchasesAsync(
357 | QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.INAPP).build(), (billingResult1, list) -> {
358 | if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.OK) {
359 | if (list.size() > 0) {
360 |
361 | Log.d("TAG", "IN APP SUCCESS RESTORE: " + list);
362 | for (int i = 0; i < list.size(); i++) {
363 |
364 | if (list.get(i).getProducts().contains(PRODUCT_PREMIUM)) {
365 | tv_status.setText("Premium Restored");
366 | Log.d("TAG", "Product id "+PRODUCT_PREMIUM+" will restore here");
367 | }
368 |
369 | }
370 | } else {
371 | tv_status.setText("Nothing found to Restored");
372 | Log.d("TAG", "In APP Not Found To Restore");
373 | }
374 | }
375 | });
376 | }
377 | else{
378 | handleBillingError(billingResult.getResponseCode());
379 | }
380 |
381 | }
382 | });
383 | }
384 |
385 |
386 | private void handleBillingError(int responseCode) {
387 | String errorMessage = "";
388 | switch (responseCode) {
389 | case BillingClient.BillingResponseCode.BILLING_UNAVAILABLE:
390 | errorMessage = "Billing service is currently unavailable. Please try again later.";
391 | break;
392 | case BillingClient.BillingResponseCode.DEVELOPER_ERROR:
393 | errorMessage = "An error occurred while processing the request. Please try again later.";
394 | break;
395 | case BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED:
396 | errorMessage = "This feature is not supported on your device.";
397 | break;
398 | case BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED:
399 | errorMessage = "You already own this item.";
400 | break;
401 | case BillingClient.BillingResponseCode.ITEM_NOT_OWNED:
402 | errorMessage = "You do not own this item.";
403 | break;
404 | case BillingClient.BillingResponseCode.ITEM_UNAVAILABLE:
405 | errorMessage = "This item is not available for purchase.";
406 | break;
407 | case BillingClient.BillingResponseCode.SERVICE_DISCONNECTED:
408 | errorMessage = "Billing service has been disconnected. Please try again later.";
409 | break;
410 | case BillingClient.BillingResponseCode.SERVICE_TIMEOUT:
411 | errorMessage = "Billing service timed out. Please try again later.";
412 | break;
413 | case BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE:
414 | errorMessage = "Billing service is currently unavailable. Please try again later.";
415 | break;
416 | case BillingClient.BillingResponseCode.USER_CANCELED:
417 | errorMessage = "The purchase has been canceled.";
418 | break;
419 | default:
420 | errorMessage = "An unknown error occurred.";
421 | break;
422 | }
423 | Log.e("BillingError", errorMessage);
424 | }
425 |
426 | }
--------------------------------------------------------------------------------