Skip to content

Commit 54d06fc

Browse files
committed
Add purchase and consume sample
1 parent 69a9e07 commit 54d06fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1317
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
.DS_Store

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle.kts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
plugins {
2+
alias(libs.plugins.android.application)
3+
alias(libs.plugins.jetbrains.kotlin.android)
4+
}
5+
6+
android {
7+
namespace = "com.aghajari.inappbillingbazaar"
8+
compileSdk = 35
9+
10+
defaultConfig {
11+
applicationId = "com.aghajari.inappbillingbazaar"
12+
minSdk = 21
13+
targetSdk = 35
14+
versionCode = 1
15+
versionName = "1.0"
16+
17+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
18+
vectorDrawables {
19+
useSupportLibrary = true
20+
}
21+
}
22+
23+
buildTypes {
24+
release {
25+
isMinifyEnabled = false
26+
proguardFiles(
27+
getDefaultProguardFile("proguard-android-optimize.txt"),
28+
"proguard-rules.pro"
29+
)
30+
}
31+
}
32+
compileOptions {
33+
sourceCompatibility = JavaVersion.VERSION_1_8
34+
targetCompatibility = JavaVersion.VERSION_1_8
35+
}
36+
kotlinOptions {
37+
jvmTarget = "1.8"
38+
}
39+
buildFeatures {
40+
compose = true
41+
}
42+
composeOptions {
43+
kotlinCompilerExtensionVersion = "1.5.1"
44+
}
45+
packaging {
46+
resources {
47+
excludes += "/META-INF/{AL2.0,LGPL2.1}"
48+
}
49+
}
50+
}
51+
52+
dependencies {
53+
54+
implementation(libs.androidx.core.ktx)
55+
implementation(libs.androidx.lifecycle.runtime.ktx)
56+
implementation(libs.androidx.activity.compose)
57+
implementation(platform(libs.androidx.compose.bom))
58+
implementation(libs.androidx.ui)
59+
implementation(libs.androidx.ui.graphics)
60+
implementation(libs.androidx.ui.tooling.preview)
61+
implementation(libs.androidx.material3)
62+
testImplementation(libs.junit)
63+
androidTestImplementation(libs.androidx.junit)
64+
androidTestImplementation(libs.androidx.espresso.core)
65+
androidTestImplementation(platform(libs.androidx.compose.bom))
66+
androidTestImplementation(libs.androidx.ui.test.junit4)
67+
debugImplementation(libs.androidx.ui.tooling)
68+
debugImplementation(libs.androidx.ui.test.manifest)
69+
70+
implementation(libs.poolakey)
71+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.aghajari.inappbillingbazaar
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.aghajari.inappbillingbazaar", appContext.packageName)
23+
}
24+
}

app/src/main/AndroidManifest.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:dataExtractionRules="@xml/data_extraction_rules"
8+
android:fullBackupContent="@xml/backup_rules"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/Theme.Inappbillingbazaar"
14+
tools:targetApi="31">
15+
<activity
16+
android:name=".MainActivityJava"
17+
android:exported="true"
18+
android:label="@string/app_name"
19+
android:theme="@style/Theme.Inappbillingbazaar">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
</application>
27+
28+
</manifest>
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.aghajari.inappbillingbazaar;
2+
3+
import android.os.Bundle;
4+
import android.widget.Toast;
5+
6+
import androidx.annotation.Nullable;
7+
8+
import com.aghajari.inappbillingbazaar.poolakey.Configurations;
9+
import com.aghajari.inappbillingbazaar.ui.content.BasePoolakeyActivity;
10+
11+
import ir.cafebazaar.poolakey.Connection;
12+
import ir.cafebazaar.poolakey.ConnectionState;
13+
import ir.cafebazaar.poolakey.Payment;
14+
import ir.cafebazaar.poolakey.config.PaymentConfiguration;
15+
import ir.cafebazaar.poolakey.config.SecurityCheck;
16+
import ir.cafebazaar.poolakey.request.PurchaseRequest;
17+
import kotlin.Unit;
18+
19+
public class MainActivityJava extends BasePoolakeyActivity {
20+
21+
private Payment payment;
22+
private Connection paymentConnection;
23+
24+
@Override
25+
protected void onCreate(@Nullable Bundle savedInstanceState) {
26+
super.onCreate(savedInstanceState); // UI handled on BasePoolakeyActivity
27+
28+
SecurityCheck securityCheck = new SecurityCheck.Enable(Configurations.RSA_KEY);
29+
PaymentConfiguration paymentConfig = new PaymentConfiguration(securityCheck);
30+
payment = new Payment(this, paymentConfig);
31+
32+
paymentConnection = payment.connect(connectionCallback -> {
33+
connectionCallback.connectionSucceed(() -> {
34+
updateStatus("Connected");
35+
return Unit.INSTANCE;
36+
});
37+
connectionCallback.connectionFailed(throwable -> {
38+
updateStatus("Failed: " + throwable.getMessage());
39+
return Unit.INSTANCE;
40+
});
41+
connectionCallback.disconnected(() -> {
42+
updateStatus("Disconnected");
43+
return Unit.INSTANCE;
44+
});
45+
return Unit.INSTANCE;
46+
});
47+
}
48+
49+
@Override
50+
public void onClickSKU10KIRR() {
51+
purchase(
52+
new PurchaseRequest(
53+
Configurations.PRODUCT_ID_1,
54+
"SKU10",
55+
null
56+
)
57+
);
58+
}
59+
60+
@Override
61+
public void onClickSKU20KIRR() {
62+
purchase(
63+
new PurchaseRequest(
64+
Configurations.PRODUCT_ID_2,
65+
"SKU20",
66+
null
67+
)
68+
);
69+
}
70+
71+
private void purchase(PurchaseRequest request) {
72+
if (paymentConnection.getState() != ConnectionState.Connected.INSTANCE) {
73+
Toast.makeText(this, "Not Connected!", Toast.LENGTH_SHORT).show();
74+
return;
75+
}
76+
77+
payment.purchaseProduct(getActivityResultRegistry(), request, purchaseCallback -> {
78+
purchaseCallback.purchaseFlowBegan(() -> {
79+
updateStatus("Purchase Began");
80+
return Unit.INSTANCE;
81+
});
82+
purchaseCallback.failedToBeginFlow(throwable -> {
83+
updateStatus("Failed to Begin: " + throwable.getMessage());
84+
return Unit.INSTANCE;
85+
});
86+
purchaseCallback.purchaseSucceed(purchaseEntity -> {
87+
updateStatus("Purchase Succeed: " + purchaseEntity.getPayload());
88+
// You can validate the purchase here using a server-side request
89+
// if it's one time purchase, give user the item here,
90+
// if it's not one time, you must consume it
91+
consume(purchaseEntity.getPurchaseToken(), purchaseEntity.getPayload());
92+
return Unit.INSTANCE;
93+
});
94+
purchaseCallback.purchaseCanceled(() -> {
95+
updateStatus("Purchase Canceled");
96+
return Unit.INSTANCE;
97+
});
98+
purchaseCallback.purchaseFailed(throwable -> {
99+
updateStatus("Purchase Failed: " + throwable.getMessage());
100+
return Unit.INSTANCE;
101+
});
102+
103+
return Unit.INSTANCE;
104+
});
105+
}
106+
107+
private void consume(String purchaseToken, String payload) {
108+
payment.consumeProduct(purchaseToken, consumeCallback -> {
109+
consumeCallback.consumeSucceed(() -> {
110+
updateStatus(payload + " Consume Succeed");
111+
// Give user the purchased item here
112+
return Unit.INSTANCE;
113+
});
114+
consumeCallback.consumeFailed(throwable -> {
115+
updateStatus(payload + " Consume Failed: " + throwable.getMessage());
116+
return Unit.INSTANCE;
117+
});
118+
return Unit.INSTANCE;
119+
});
120+
}
121+
122+
@Override
123+
public void onDestroy() {
124+
paymentConnection.disconnect();
125+
super.onDestroy();
126+
}
127+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.aghajari.inappbillingbazaar
2+
3+
import android.os.Bundle
4+
import android.widget.Toast
5+
import com.aghajari.inappbillingbazaar.poolakey.Configurations
6+
import com.aghajari.inappbillingbazaar.ui.content.BasePoolakeyActivity
7+
import ir.cafebazaar.poolakey.Connection
8+
import ir.cafebazaar.poolakey.ConnectionState
9+
import ir.cafebazaar.poolakey.Payment
10+
import ir.cafebazaar.poolakey.config.PaymentConfiguration
11+
import ir.cafebazaar.poolakey.config.SecurityCheck
12+
import ir.cafebazaar.poolakey.request.PurchaseRequest
13+
14+
class MainActivityKotlin : BasePoolakeyActivity() {
15+
16+
private lateinit var payment: Payment
17+
private lateinit var paymentConnection: Connection
18+
19+
override fun onCreate(savedInstanceState: Bundle?) {
20+
super.onCreate(savedInstanceState) // UI handled on BasePoolakeyActivity
21+
22+
val securityCheck = SecurityCheck.Enable(rsaPublicKey = Configurations.RSA_KEY)
23+
val paymentConfig = PaymentConfiguration(localSecurityCheck = securityCheck)
24+
payment = Payment(context = this, config = paymentConfig)
25+
26+
paymentConnection = payment.connect {
27+
connectionSucceed {
28+
updateStatus(status = "Connected")
29+
}
30+
connectionFailed { throwable ->
31+
updateStatus(status = "Failed: ${throwable.message}")
32+
}
33+
disconnected {
34+
updateStatus(status = "Disconnected")
35+
}
36+
}
37+
}
38+
39+
override fun onClickSKU10KIRR() {
40+
purchase(
41+
request = PurchaseRequest(
42+
productId = Configurations.PRODUCT_ID_1,
43+
payload = "SKU10",
44+
),
45+
)
46+
}
47+
48+
override fun onClickSKU20KIRR() {
49+
purchase(
50+
request = PurchaseRequest(
51+
productId = Configurations.PRODUCT_ID_2,
52+
payload = "SKU20",
53+
),
54+
)
55+
}
56+
57+
private fun purchase(request: PurchaseRequest) {
58+
if (paymentConnection.getState() != ConnectionState.Connected) {
59+
Toast.makeText(this, "Not Connected!", Toast.LENGTH_SHORT).show()
60+
return
61+
}
62+
63+
payment.purchaseProduct(
64+
registry = activityResultRegistry,
65+
request = request,
66+
) {
67+
purchaseFlowBegan {
68+
updateStatus(status = "Purchase Began")
69+
}
70+
failedToBeginFlow { throwable ->
71+
updateStatus(status = "Failed to Begin: ${throwable.message}")
72+
}
73+
purchaseSucceed { purchaseEntity ->
74+
updateStatus(status = "Purchase Succeed: ${purchaseEntity.payload}")
75+
// You can validate the purchase here using a server-side request
76+
// if it's one time purchase, give user the item here,
77+
// if it's not one time, you must consume it
78+
consume(purchaseEntity.purchaseToken, purchaseEntity.payload)
79+
}
80+
purchaseCanceled {
81+
updateStatus(status = "Purchase Canceled")
82+
}
83+
purchaseFailed { throwable ->
84+
updateStatus(status = "Purchase Failed: ${throwable.message}")
85+
}
86+
}
87+
}
88+
89+
private fun consume(purchaseToken: String, payload: String) {
90+
payment.consumeProduct(purchaseToken = purchaseToken) {
91+
consumeSucceed {
92+
updateStatus(status = "$payload Consume Succeed")
93+
// Give user the purchased item here
94+
}
95+
consumeFailed { throwable ->
96+
updateStatus(status = "$payload Consume Failed: " + throwable.message)
97+
}
98+
}
99+
}
100+
101+
override fun onDestroy() {
102+
paymentConnection.disconnect()
103+
super.onDestroy()
104+
}
105+
}

0 commit comments

Comments
 (0)