Skip to content

Commit 07b5c70

Browse files
authored
Merge pull request #53 from mert-karadeniz/master
Upgraded to Google Billing v6.0
2 parents 2c11117 + 6402be5 commit 07b5c70

File tree

7 files changed

+260
-66
lines changed

7 files changed

+260
-66
lines changed

app/src/main/java/games/moisoni/google_inapp_billing/JavaSampleActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void onProductsFetched(@NonNull List<ProductInfo> productDetails) {
9292

9393
for (ProductInfo productInfo : productDetails) {
9494
product = productInfo.getProduct();
95-
price = productInfo.getOneTimePurchaseOfferPrice();
95+
price = productInfo.getOneTimePurchaseOfferFormattedPrice();
9696

9797
if (product.equalsIgnoreCase("consumable_id_1")) {
9898
//TODO - do something

app/src/main/java/games/moisoni/google_inapp_billing/KotlinSampleActivity.kt

Lines changed: 74 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ class KotlinSampleActivity : AppCompatActivity() {
7272
subscriptionIds.add("subscription_id_3")
7373

7474
billingConnector = BillingConnector(
75-
this,
76-
"license_key") //"license_key" - public developer key from Play Console
77-
.setConsumableIds(consumableIds) //to set consumable ids - call only for consumable products
78-
.setNonConsumableIds(nonConsumableIds) //to set non-consumable ids - call only for non-consumable products
79-
.setSubscriptionIds(subscriptionIds) //to set subscription ids - call only for subscription products
80-
.autoAcknowledge() //legacy option - better call this. Alternatively purchases can be acknowledge via public method "acknowledgePurchase(PurchaseInfo purchaseInfo)"
81-
.autoConsume() //legacy option - better call this. Alternatively purchases can be consumed via public method consumePurchase(PurchaseInfo purchaseInfo)"
82-
.enableLogging() //to enable logging for debugging throughout the library - this can be skipped
83-
.connect() //to connect billing client with Play Console
75+
this,
76+
"license_key") //"license_key" - public developer key from Play Console
77+
.setConsumableIds(consumableIds) //to set consumable ids - call only for consumable products
78+
.setNonConsumableIds(nonConsumableIds) //to set non-consumable ids - call only for non-consumable products
79+
.setSubscriptionIds(subscriptionIds) //to set subscription ids - call only for subscription products
80+
.autoAcknowledge() //legacy option - better call this. Alternatively purchases can be acknowledge via public method "acknowledgePurchase(PurchaseInfo purchaseInfo)"
81+
.autoConsume() //legacy option - better call this. Alternatively purchases can be consumed via public method consumePurchase(PurchaseInfo purchaseInfo)"
82+
.enableLogging() //to enable logging for debugging throughout the library - this can be skipped
83+
.connect() //to connect billing client with Play Console
8484

8585
billingConnector.setBillingEventListener(object : BillingEventListener {
8686
override fun onProductsFetched(productDetails: MutableList<ProductInfo>) {
@@ -89,23 +89,23 @@ class KotlinSampleActivity : AppCompatActivity() {
8989

9090
for (productInfo in productDetails) {
9191
product = productInfo.product
92-
price = productInfo.oneTimePurchaseOfferPrice
92+
price = productInfo.oneTimePurchaseOfferFormattedPrice
9393

9494
if (product.equals("consumable_id_1", ignoreCase = true)) {
9595
//TODO - do something
9696
Log.d("BillingConnector", "Product fetched: $product")
9797
Toast.makeText(
98-
this@KotlinSampleActivity,
99-
"Product fetched: $product",
100-
Toast.LENGTH_SHORT
98+
this@KotlinSampleActivity,
99+
"Product fetched: $product",
100+
Toast.LENGTH_SHORT
101101
).show()
102102

103103
//TODO - do something
104104
Log.d("BillingConnector", "Product price: $price")
105105
Toast.makeText(
106-
this@KotlinSampleActivity,
107-
"Product price: $price",
108-
Toast.LENGTH_SHORT
106+
this@KotlinSampleActivity,
107+
"Product price: $price",
108+
Toast.LENGTH_SHORT
109109
).show()
110110
}
111111

@@ -116,8 +116,8 @@ class KotlinSampleActivity : AppCompatActivity() {
116116
}
117117

118118
override fun onPurchasedProductsFetched(
119-
productType: ProductType,
120-
purchases: MutableList<PurchaseInfo>
119+
productType: ProductType,
120+
purchases: MutableList<PurchaseInfo>
121121
) {
122122
/*
123123
* This will be called even when no purchased products are returned by the API
@@ -127,14 +127,17 @@ class KotlinSampleActivity : AppCompatActivity() {
127127
ProductType.INAPP -> {
128128
//TODO - non-consumable/consumable products
129129
}
130+
130131
ProductType.SUBS -> {
131132
//TODO - subscription products
132133
}
134+
133135
ProductType.COMBINED -> {
134136
//this will be triggered on activity start
135137
//the other two (INAPP and SUBS) will be triggered when the user actually buys a product
136138
//TODO - restore purchases
137139
}
140+
138141
else -> {
139142
Log.d("BillingConnector", "None of the above ProductType match")
140143
}
@@ -146,9 +149,9 @@ class KotlinSampleActivity : AppCompatActivity() {
146149
//TODO - do something
147150
Log.d("BillingConnector", "Purchased product fetched: $it")
148151
Toast.makeText(
149-
this@KotlinSampleActivity,
150-
"Purchased product fetched: $it",
151-
Toast.LENGTH_SHORT
152+
this@KotlinSampleActivity,
153+
"Purchased product fetched: $it",
154+
Toast.LENGTH_SHORT
152155
).show()
153156
}
154157

@@ -169,17 +172,17 @@ class KotlinSampleActivity : AppCompatActivity() {
169172
//TODO - do something
170173
Log.d("BillingConnector", "Product purchased: $product")
171174
Toast.makeText(
172-
this@KotlinSampleActivity,
173-
"Product purchased: $product",
174-
Toast.LENGTH_SHORT
175+
this@KotlinSampleActivity,
176+
"Product purchased: $product",
177+
Toast.LENGTH_SHORT
175178
).show()
176179

177180
//TODO - do something
178181
Log.d("BillingConnector", "Purchase token: $purchaseToken")
179182
Toast.makeText(
180-
this@KotlinSampleActivity,
181-
"Purchase token: $purchaseToken",
182-
Toast.LENGTH_SHORT
183+
this@KotlinSampleActivity,
184+
"Purchase token: $purchaseToken",
185+
Toast.LENGTH_SHORT
183186
).show()
184187
}
185188

@@ -207,9 +210,9 @@ class KotlinSampleActivity : AppCompatActivity() {
207210
//TODO - do something
208211
Log.d("BillingConnector", "Acknowledged: ${purchase.product}")
209212
Toast.makeText(
210-
this@KotlinSampleActivity,
211-
"Acknowledged: ${purchase.product}",
212-
Toast.LENGTH_SHORT
213+
this@KotlinSampleActivity,
214+
"Acknowledged: ${purchase.product}",
215+
Toast.LENGTH_SHORT
213216
).show()
214217
}
215218

@@ -231,9 +234,9 @@ class KotlinSampleActivity : AppCompatActivity() {
231234
//TODO - do something
232235
Log.d("BillingConnector", "Consumed: ${purchase.product}")
233236
Toast.makeText(
234-
this@KotlinSampleActivity,
235-
"Consumed: ${purchase.product}",
236-
Toast.LENGTH_SHORT
237+
this@KotlinSampleActivity,
238+
"Consumed: ${purchase.product}",
239+
Toast.LENGTH_SHORT
237240
).show()
238241
}
239242

@@ -242,22 +245,26 @@ class KotlinSampleActivity : AppCompatActivity() {
242245
}
243246

244247
override fun onBillingError(
245-
billingConnector: BillingConnector,
246-
response: BillingResponse
248+
billingConnector: BillingConnector,
249+
response: BillingResponse
247250
) {
248251
when (response.errorType) {
249252
ErrorType.CLIENT_NOT_READY -> {
250253
//TODO - client is not ready yet
251254
}
255+
252256
ErrorType.CLIENT_DISCONNECTED -> {
253257
//TODO - client has disconnected
254258
}
259+
255260
ErrorType.PRODUCT_NOT_EXIST -> {
256261
//TODO - product does not exist
257262
}
263+
258264
ErrorType.CONSUME_ERROR -> {
259265
//TODO - error during consumption
260266
}
267+
261268
ErrorType.CONSUME_WARNING -> {
262269
/*
263270
* This will be triggered when a consumable purchase has a PENDING state
@@ -270,9 +277,11 @@ class KotlinSampleActivity : AppCompatActivity() {
270277
* */
271278
//TODO - warning during consumption
272279
}
280+
273281
ErrorType.ACKNOWLEDGE_ERROR -> {
274282
//TODO - error during acknowledgment
275283
}
284+
276285
ErrorType.ACKNOWLEDGE_WARNING -> {
277286
/*
278287
* This will be triggered when a purchase can not be acknowledged because the state is PENDING
@@ -285,51 +294,62 @@ class KotlinSampleActivity : AppCompatActivity() {
285294
* */
286295
//TODO - warning during acknowledgment
287296
}
297+
288298
ErrorType.FETCH_PURCHASED_PRODUCTS_ERROR -> {
289299
//TODO - error occurred while querying purchased products
290300
}
301+
291302
ErrorType.BILLING_ERROR -> {
292303
//TODO - error occurred during initialization / querying product details
293304
}
305+
294306
ErrorType.USER_CANCELED -> {
295307
//TODO - user pressed back or canceled a dialog
296308
}
309+
297310
ErrorType.SERVICE_UNAVAILABLE -> {
298311
//TODO - network connection is down
299312
}
313+
300314
ErrorType.BILLING_UNAVAILABLE -> {
301315
//TODO - billing API version is not supported for the type requested
302316
}
317+
303318
ErrorType.ITEM_UNAVAILABLE -> {
304319
//TODO - requested product is not available for purchase
305320
}
321+
306322
ErrorType.DEVELOPER_ERROR -> {
307323
//TODO - invalid arguments provided to the API
308324
}
325+
309326
ErrorType.ERROR -> {
310327
//TODO - fatal error during the API action
311328
}
329+
312330
ErrorType.ITEM_ALREADY_OWNED -> {
313331
//TODO - item is already owned
314332
}
333+
315334
ErrorType.ITEM_NOT_OWNED -> {
316335
//TODO - failure to consume since item is not owned
317336
}
337+
318338
else -> {
319339
Log.d("BillingConnector", "None of the above ErrorType match")
320340
}
321341
}
322342

323343
Log.d(
324-
"BillingConnector", "Error type: ${response.errorType}" +
325-
" Response code: ${response.responseCode}" + " Message: ${response.debugMessage}"
344+
"BillingConnector", "Error type: ${response.errorType}" +
345+
" Response code: ${response.responseCode}" + " Message: ${response.debugMessage}"
326346
)
327347

328348
Toast.makeText(
329-
this@KotlinSampleActivity,
330-
"Error type: ${response.errorType}" + " Response code: ${response.responseCode}"
331-
+ " Message: ${response.debugMessage}",
332-
Toast.LENGTH_SHORT
349+
this@KotlinSampleActivity,
350+
"Error type: ${response.errorType}" + " Response code: ${response.responseCode}"
351+
+ " Message: ${response.debugMessage}",
352+
Toast.LENGTH_SHORT
333353
).show()
334354
}
335355
})
@@ -417,14 +437,17 @@ class KotlinSampleActivity : AppCompatActivity() {
417437
//TODO - do something
418438
Log.d("BillingConnector", "Device subscription support: SUPPORTED")
419439
}
440+
420441
SupportState.NOT_SUPPORTED -> {
421442
//TODO - do something
422443
Log.d("BillingConnector", "Device subscription support: NOT_SUPPORTED")
423444
}
445+
424446
SupportState.DISCONNECTED -> {
425447
//TODO - do something
426448
Log.d("BillingConnector", "Device subscription support: client DISCONNECTED")
427449
}
450+
428451
else -> {
429452
Log.d("BillingConnector", "None of the above SupportState match")
430453
}
@@ -441,27 +464,31 @@ class KotlinSampleActivity : AppCompatActivity() {
441464
//TODO - do something
442465
Log.d("BillingConnector", "The product: ${productInfo.product} is purchased")
443466
}
467+
444468
PurchasedResult.NO -> {
445469
//TODO - do something
446470
Log.d(
447-
"BillingConnector",
448-
"The product: ${productInfo.product} is not purchased"
471+
"BillingConnector",
472+
"The product: ${productInfo.product} is not purchased"
449473
)
450474
}
475+
451476
PurchasedResult.CLIENT_NOT_READY -> {
452477
//TODO - do something
453478
Log.d(
454-
"BillingConnector",
455-
"Cannot check: ${productInfo.product} because client is not ready"
479+
"BillingConnector",
480+
"Cannot check: ${productInfo.product} because client is not ready"
456481
)
457482
}
483+
458484
PurchasedResult.PURCHASED_PRODUCTS_NOT_FETCHED_YET -> {
459485
//TODO - do something
460486
Log.d(
461-
"BillingConnector",
462-
"Cannot check: ${productInfo.product} because purchased products are not fetched yet"
487+
"BillingConnector",
488+
"Cannot check: ${productInfo.product} because purchased products are not fetched yet"
463489
)
464490
}
491+
465492
else -> {
466493
Log.d("BillingConnector", "None of the above PurchasedResult match")
467494
}

google-iab/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,22 @@ afterEvaluate {
5252
from components.release
5353
groupId = 'com.github.moisoni97'
5454
artifactId = 'google-inapp-billing'
55-
version = '1.1.1'
55+
version = '1.1.2'
5656
}
5757
}
5858
}
5959
}
6060

6161
dependencies {
6262
testImplementation 'junit:junit:4.13.2'
63-
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
64-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
63+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
64+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
6565

6666
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
6767

6868
implementation 'androidx.appcompat:appcompat:1.5.1'
6969
implementation 'com.google.android.material:material:1.6.1'
7070
implementation 'com.google.guava:guava:31.1-android'
7171

72-
implementation 'com.android.billingclient:billing:5.0.0'
72+
implementation 'com.android.billingclient:billing:6.0.0'
7373
}

google-iab/src/main/java/games/moisoni/google_iab/BillingConnector.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import static com.android.billingclient.api.BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED;
77
import static com.android.billingclient.api.BillingClient.BillingResponseCode.ITEM_NOT_OWNED;
88
import static com.android.billingclient.api.BillingClient.BillingResponseCode.ITEM_UNAVAILABLE;
9+
import static com.android.billingclient.api.BillingClient.BillingResponseCode.NETWORK_ERROR;
910
import static com.android.billingclient.api.BillingClient.BillingResponseCode.OK;
1011
import static com.android.billingclient.api.BillingClient.BillingResponseCode.SERVICE_DISCONNECTED;
11-
import static com.android.billingclient.api.BillingClient.BillingResponseCode.SERVICE_TIMEOUT;
1212
import static com.android.billingclient.api.BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE;
1313
import static com.android.billingclient.api.BillingClient.BillingResponseCode.USER_CANCELED;
1414
import static com.android.billingclient.api.BillingClient.FeatureType.SUBSCRIPTIONS;
@@ -146,9 +146,11 @@ private void init(Context context) {
146146
new BillingResponse(ErrorType.ITEM_NOT_OWNED, billingResult)));
147147
break;
148148
case SERVICE_DISCONNECTED:
149-
case SERVICE_TIMEOUT:
150149
Log("Initialization error: service disconnected/timeout. Trying to reconnect...");
151150
break;
151+
case NETWORK_ERROR:
152+
Log("Initialization error: service network error. Trying to reconnect...");
153+
break;
152154
default:
153155
Log("Initialization error: " + new BillingResponse(ErrorType.BILLING_ERROR, billingResult));
154156
break;

0 commit comments

Comments
 (0)