/
BillingClientLifecycle.java
386 lines (356 loc) · 16.5 KB
/
BillingClientLifecycle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright 2020 Google LLC. All rights reserved.
*
* 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.
*/
package com.sample.android.classytaxijava.billing;
import android.app.Activity;
import android.app.Application;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.OnLifecycleEvent;
import com.android.billingclient.api.AcknowledgePurchaseParams;
import com.android.billingclient.api.AcknowledgePurchaseResponseListener;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesResponseListener;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.SkuDetailsResponseListener;
import com.sample.android.classytaxijava.Constants;
import com.sample.android.classytaxijava.ui.SingleLiveEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BillingClientLifecycle implements LifecycleObserver, PurchasesUpdatedListener,
BillingClientStateListener, SkuDetailsResponseListener, PurchasesResponseListener {
private static final String TAG = "BillingLifecycle";
private static final List<String> LIST_OF_SKUS = Collections.unmodifiableList(
new ArrayList<String>() {{
add(Constants.BASIC_SKU);
add(Constants.PREMIUM_SKU);
}});
/**
* The purchase event is observable. Only one observer will be notified.
*/
public SingleLiveEvent<List<Purchase>> purchaseUpdateEvent = new SingleLiveEvent<>();
/**
* Purchases are observable. This list will be updated when the Billing Library
* detects new or existing purchases. All observers will be notified.
*/
public MutableLiveData<List<Purchase>> purchases = new MutableLiveData<>();
/**
* SkuDetails for all known SKUs.
*/
public MutableLiveData<Map<String, SkuDetails>> skusWithSkuDetails = new MutableLiveData<>();
private static volatile BillingClientLifecycle INSTANCE;
private Application app;
private BillingClient billingClient;
private BillingClientLifecycle(Application app) {
this.app = app;
}
public static BillingClientLifecycle getInstance(Application app) {
if (INSTANCE == null) {
synchronized (BillingClientLifecycle.class) {
if (INSTANCE == null) {
INSTANCE = new BillingClientLifecycle(app);
}
}
}
return INSTANCE;
}
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public void create() {
Log.d(TAG, "ON_CREATE");
// Create a new BillingClient in onCreate().
// Since the BillingClient can only be used once, we need to create a new instance
// after ending the previous connection to the Google Play Store in onDestroy().
billingClient = BillingClient.newBuilder(app)
.setListener(this)
.enablePendingPurchases() // Not used for subscriptions.
.build();
if (!billingClient.isReady()) {
Log.d(TAG, "BillingClient: Start connection...");
billingClient.startConnection(this);
}
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void destroy() {
Log.d(TAG, "ON_DESTROY");
if (billingClient.isReady()) {
Log.d(TAG, "BillingClient can only be used once -- closing connection");
// BillingClient can only be used once.
// After calling endConnection(), we must create a new BillingClient.
billingClient.endConnection();
}
}
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
int responseCode = billingResult.getResponseCode();
String debugMessage = billingResult.getDebugMessage();
Log.d(TAG, "onBillingSetupFinished: " + responseCode + " " + debugMessage);
if (responseCode == BillingClient.BillingResponseCode.OK) {
// The billing client is ready. You can query purchases here.
querySkuDetails();
queryPurchases();
}
}
@Override
public void onBillingServiceDisconnected() {
Log.d(TAG, "onBillingServiceDisconnected");
// TODO: Try connecting again with exponential backoff.
}
/**
* Receives the result from {@link #querySkuDetails()}}.
* <p>
* Store the SkuDetails and post them in the {@link #skusWithSkuDetails}. This allows other
* parts of the app to use the {@link SkuDetails} to show SKU information and make purchases.
*/
@Override
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
if (billingResult == null) {
Log.wtf(TAG, "onSkuDetailsResponse: null BillingResult");
return;
}
int responseCode = billingResult.getResponseCode();
String debugMessage = billingResult.getDebugMessage();
switch (responseCode) {
case BillingClient.BillingResponseCode.OK:
Log.i(TAG, "onSkuDetailsResponse: " + responseCode + " " + debugMessage);
final int expectedSkuDetailsCount = LIST_OF_SKUS.size();
if (skuDetailsList == null) {
skusWithSkuDetails.postValue(Collections.<String, SkuDetails>emptyMap());
Log.e(TAG, "onSkuDetailsResponse: " +
"Expected " + expectedSkuDetailsCount + ", " +
"Found null SkuDetails. " +
"Check to see if the SKUs you requested are correctly published " +
"in the Google Play Console.");
} else {
Map<String, SkuDetails> newSkusDetailList = new HashMap<String, SkuDetails>();
for (SkuDetails skuDetails : skuDetailsList) {
newSkusDetailList.put(skuDetails.getSku(), skuDetails);
}
skusWithSkuDetails.postValue(newSkusDetailList);
int skuDetailsCount = newSkusDetailList.size();
if (skuDetailsCount == expectedSkuDetailsCount) {
Log.i(TAG, "onSkuDetailsResponse: Found " + skuDetailsCount + " SkuDetails");
} else {
Log.e(TAG, "onSkuDetailsResponse: " +
"Expected " + expectedSkuDetailsCount + ", " +
"Found " + skuDetailsCount + " SkuDetails. " +
"Check to see if the SKUs you requested are correctly published " +
"in the Google Play Console.");
}
}
break;
case BillingClient.BillingResponseCode.SERVICE_DISCONNECTED:
case BillingClient.BillingResponseCode.SERVICE_UNAVAILABLE:
case BillingClient.BillingResponseCode.BILLING_UNAVAILABLE:
case BillingClient.BillingResponseCode.ITEM_UNAVAILABLE:
case BillingClient.BillingResponseCode.DEVELOPER_ERROR:
case BillingClient.BillingResponseCode.ERROR:
Log.e(TAG, "onSkuDetailsResponse: " + responseCode + " " + debugMessage);
break;
case BillingClient.BillingResponseCode.USER_CANCELED:
Log.i(TAG, "onSkuDetailsResponse: " + responseCode + " " + debugMessage);
break;
// These response codes are not expected.
case BillingClient.BillingResponseCode.FEATURE_NOT_SUPPORTED:
case BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED:
case BillingClient.BillingResponseCode.ITEM_NOT_OWNED:
default:
Log.wtf(TAG, "onSkuDetailsResponse: " + responseCode + " " + debugMessage);
}
}
/**
* Query Google Play Billing for existing purchases.
* <p>
* New purchases will be provided to the PurchasesUpdatedListener.
* You still need to check the Google Play Billing API to know when purchase tokens are removed.
*/
public void queryPurchases() {
if (!billingClient.isReady()) {
Log.e(TAG, "queryPurchases: BillingClient is not ready");
}
Log.d(TAG, "queryPurchases: SUBS");
billingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS, this);
}
/**
* Callback from the billing library when queryPurchasesAsync is called.
*/
@Override
public void onQueryPurchasesResponse(@NonNull BillingResult billingResult,
@NonNull List<Purchase> list) {
processPurchases(list);
}
/**
* Called by the Billing Library when new purchases are detected.
*/
public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
if (billingResult == null) {
Log.wtf(TAG, "onPurchasesUpdated: null BillingResult");
return;
}
int responseCode = billingResult.getResponseCode();
String debugMessage = billingResult.getDebugMessage();
Log.d(TAG, String.format("onPurchasesUpdated: %s %s",responseCode, debugMessage));
switch (responseCode) {
case BillingClient.BillingResponseCode.OK:
if (purchases == null) {
Log.d(TAG, "onPurchasesUpdated: null purchase list");
processPurchases(null);
} else {
processPurchases(purchases);
}
break;
case BillingClient.BillingResponseCode.USER_CANCELED:
Log.i(TAG, "onPurchasesUpdated: User canceled the purchase");
break;
case BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED:
Log.i(TAG, "onPurchasesUpdated: The user already owns this item");
break;
case BillingClient.BillingResponseCode.DEVELOPER_ERROR:
Log.e(TAG, "onPurchasesUpdated: Developer error means that Google Play " +
"does not recognize the configuration. If you are just getting started, " +
"make sure you have configured the application correctly in the " +
"Google Play Console. The SKU product ID must match and the APK you " +
"are using must be signed with release keys."
);
break;
}
}
/**
* Send purchase SingleLiveEvent and update purchases LiveData.
* <p>
* The SingleLiveEvent will trigger network call to verify the subscriptions on the sever.
* The LiveData will allow Google Play settings UI to update based on the latest purchase data.
*/
private void processPurchases(List<Purchase> purchasesList) {
if (purchasesList != null) {
Log.d(TAG, "processPurchases: " + purchasesList.size() + " purchase(s)");
} else {
Log.d(TAG, "processPurchases: with no purchases");
}
if (isUnchangedPurchaseList(purchasesList)) {
Log.d(TAG, "processPurchases: Purchase list has not changed");
return;
}
purchaseUpdateEvent.postValue(purchasesList);
purchases.postValue(purchasesList);
if (purchasesList != null) {
logAcknowledgementStatus(purchasesList);
}
}
/**
* Log the number of purchases that are acknowledge and not acknowledged.
* <p>
* https://developer.android.com/google/play/billing/billing_library_releases_notes#2_0_acknowledge
* <p>
* When the purchase is first received, it will not be acknowledge.
* This application sends the purchase token to the server for registration. After the
* purchase token is registered to an account, the Android app acknowledges the purchase token.
* The next time the purchase list is updated, it will contain acknowledged purchases.
*/
private void logAcknowledgementStatus(List<Purchase> purchasesList) {
int ack_yes = 0;
int ack_no = 0;
for (Purchase purchase : purchasesList) {
if (purchase.isAcknowledged()) {
ack_yes++;
} else {
ack_no++;
}
}
Log.d(TAG, "logAcknowledgementStatus: acknowledged=" + ack_yes +
" unacknowledged=" + ack_no);
}
/**
* Check whether the purchases have changed before posting changes.
*/
private boolean isUnchangedPurchaseList(List<Purchase> purchasesList) {
// TODO: Optimize to avoid updates with identical data.
return false;
}
/**
* In order to make purchases, you need the {@link SkuDetails} for the item or subscription.
* This is an asynchronous call that will receive a result in {@link #onSkuDetailsResponse}.
*/
public void querySkuDetails() {
Log.d(TAG, "querySkuDetails");
SkuDetailsParams params = SkuDetailsParams.newBuilder()
.setType(BillingClient.SkuType.SUBS)
.setSkusList(LIST_OF_SKUS)
.build();
Log.i(TAG, "querySkuDetailsAsync");
billingClient.querySkuDetailsAsync(params, this);
}
/**
* Launching the billing flow.
* <p>
* Launching the UI to make a purchase requires a reference to the Activity.
*/
public int launchBillingFlow(Activity activity, BillingFlowParams params) {
if (!billingClient.isReady()) {
Log.e(TAG, "launchBillingFlow: BillingClient is not ready");
}
BillingResult billingResult = billingClient.launchBillingFlow(activity, params);
int responseCode = billingResult.getResponseCode();
String debugMessage = billingResult.getDebugMessage();
Log.d(TAG, "launchBillingFlow: BillingResponse " + responseCode + " " + debugMessage);
return responseCode;
}
/**
* Acknowledge a purchase.
* <p>
* https://developer.android.com/google/play/billing/billing_library_releases_notes#2_0_acknowledge
* <p>
* Apps should acknowledge the purchase after confirming that the purchase token
* has been associated with a user. This app only acknowledges purchases after
* successfully receiving the subscription data back from the server.
* <p>
* Developers can choose to acknowledge purchases from a server using the
* Google Play Developer API. The server has direct access to the user database,
* so using the Google Play Developer API for acknowledgement might be more reliable.
* TODO(134506821): Acknowledge purchases on the server.
* <p>
* If the purchase token is not acknowledged within 3 days,
* then Google Play will automatically refund and revoke the purchase.
* This behavior helps ensure that users are not charged for subscriptions unless the
* user has successfully received access to the content.
* This eliminates a category of issues where users complain to developers
* that they paid for something that the app is not giving to them.
*/
public void acknowledgePurchase(String purchaseToken) {
Log.d(TAG, "acknowledgePurchase");
AcknowledgePurchaseParams params = AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchaseToken)
.build();
billingClient.acknowledgePurchase(params, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
int responseCode = billingResult.getResponseCode();
String debugMessage = billingResult.getDebugMessage();
Log.d(TAG, "acknowledgePurchase: " + responseCode + " " + debugMessage);
}
});
}
}