Skip to content

Commit

Permalink
billing
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalsonic committed Feb 21, 2023
1 parent 53362d9 commit fc75891
Show file tree
Hide file tree
Showing 18 changed files with 317 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2023 Hypersoft Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
5 changes: 5 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Apache CustomBadge
Copyright 2023 The Apache Software Foundation

This product includes software developed at
The Apache Software Foundation (http://www.apache.org/).
104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
[![](https://jitpack.io/v/orbitalsonic/SonicInApp.svg)](https://jitpack.io/#orbitalsonic/SonicInApp)
# SonicInApp

SonicInApp is a [Google Play Billing](https://developer.android.com/google/play/billing/integrate) library that demonstrates InApp purchase in your Android application

## Getting Started

### Step 1

Add maven repository in project level build.gradle or in latest project setting.gradle file
```
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
```

### Step 2

Add SonicInApp dependencies in App level build.gradle.
```
dependencies {
implementation 'com.github.hypersoftdev:inappbilling:1.0.1'
}
```

### Step 3

Declare BillingManger Variable, "this" parameter Must be a reference of an Activity

also declare your original productId

```
private val billingManager by lazy { BillingManager(this) }
private val productId:String = "Paste your original Product ID"
```

#### Billing Initializaiton

Get debugging ids for testing using "getDebugProductIDList()" method

```
if (BuildConfig.DEBUG) {
billingManager.startConnection(billingManager.getDebugProductIDList()){ isConnectionEstablished, message ->
Log.d("BillingManager", message)
}
} else {
billingManager.startConnection(listOf(productId)) { isConnectionEstablished, message ->
Log.d("BillingManager", message)
}
}
```
#### Billing State Observer

observe the states of establishing connections

```
State.billingState.observe(this) {
Log.d("BillingManager", "initObserver: $it")
}
```
#### Purchasing InApp

```
billingManager.makePurchase { isSuccess, message ->
Log.d("BillingManager", message)
}
```

#### Note
Here is the list of debuging product ids, can be test every state of billing

```
"android.test.purchased"
"android.test.item_unavailable"
"android.test.refunded"
"android.test.canceled"
```

can be found here

```
billingManager.getDebugProductIDList()
billingManager.getDebugProductIDsList()
```

# LICENSE

Copyright 2023 Hypersoft Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

14 changes: 9 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {

defaultConfig {
applicationId "com.hypersoft.inappbilling"
minSdk 24
minSdk 23
targetSdk 33
versionCode 1
versionName "1.0"
Expand All @@ -35,10 +35,14 @@ android {
dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'

// SonicInApp Library
implementation project(path: ':billing')
// implementation 'com.github.hypersoftdev:inappbilling:1.0.3'
}
51 changes: 51 additions & 0 deletions app/src/main/java/com/hypersoft/inappbilling/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,61 @@ package com.hypersoft.inappbilling

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import com.hypersoft.billing.status.State
import com.hypersoft.billing.BillingManager

class MainActivity : AppCompatActivity() {
private val billingManager by lazy { BillingManager(this) }
private lateinit var tvTitle: TextView

// mostly people use package name in their
private val productId:String = "Paste your original Product ID"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

initBilling()
initObserver()

tvTitle = findViewById(R.id.tv_title)
findViewById<Button>(R.id.btn_purchase).setOnClickListener {
onPurchaseClick()
}
}

private fun initObserver() {
State.billingState.observe(this) {
Log.d("BillingManager", "initObserver: $it")
tvTitle.text = it.toString()
}
}

private fun initBilling() {
if (BuildConfig.DEBUG) {
billingManager.startConnection(billingManager.getDebugProductIDList()){ isConnectionEstablished, message ->
showMessage(message)
}
} else {
billingManager.startConnection(listOf(productId)) { isConnectionEstablished, message ->
showMessage(message)
}
}
}


private fun onPurchaseClick() {
billingManager.makePurchase { isSuccess, message ->
showMessage(message)
}
}

private fun showMessage(message:String){
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

}
15 changes: 15 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,27 @@
tools:context=".MainActivity">

<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:padding="10dp"
android:layout_marginHorizontal="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn_purchase"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Purchase"
android:layout_marginHorizontal="20dp"
android:layout_marginBottom="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions billing/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
40 changes: 40 additions & 0 deletions billing/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}

android {
namespace 'com.hypersoft.billing'
compileSdk 33

defaultConfig {
minSdk 23
targetSdk 33
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'

// Google Play Billing
implementation "com.android.billingclient:billing-ktx:5.1.0"

// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1"
}
Empty file added billing/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions billing/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.hypersoft.billing

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.hypersoft.billing.test", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions billing/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
4 changes: 4 additions & 0 deletions billing/src/main/java/com/hypersoft/billing/Dummy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.hypersoft.billing

class Dummy {
}
17 changes: 17 additions & 0 deletions billing/src/test/java/com/hypersoft/billing/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.hypersoft.billing

import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.4.0' apply false
id 'com.android.library' version '7.4.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.21' apply false
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
}
Loading

0 comments on commit fc75891

Please sign in to comment.