-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathBillingClientWrapper.java
More file actions
273 lines (244 loc) · 12.8 KB
/
BillingClientWrapper.java
File metadata and controls
273 lines (244 loc) · 12.8 KB
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
/*
* Copyright 2023 Google LLC
*
* 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
*
* https://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.example.pbl;
import android.app.Activity;
import android.content.Context;
import com.android.billingclient.api.AcknowledgePurchaseParams;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClient.BillingResponseCode;
import com.android.billingclient.api.BillingClient.ProductType;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.BillingFlowParams.ProductDetailsParams;
import com.android.billingclient.api.BillingFlowParams.SubscriptionUpdateParams;
import com.android.billingclient.api.BillingResult;
import com.android.billingclient.api.ConsumeParams;
import com.android.billingclient.api.ConsumeResponseListener;
import com.android.billingclient.api.GetBillingConfigParams;
import com.android.billingclient.api.InAppMessageParams;
import com.android.billingclient.api.InAppMessageResult;
import com.android.billingclient.api.PendingPurchasesParams;
import com.android.billingclient.api.ProductDetails;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.QueryProductDetailsParams;
import com.google.common.collect.ImmutableList;
import java.util.List;
/**
* A wrapper for the Google Play Billing Library that handles all the billing logic.
*/
class BillingClientWrapper {
private final Context context;
private final Activity activity;
private final BillingClient billingClient;
private List<ProductDetails> productDetailsList;
private ProductDetails productDetails;
public BillingClientWrapper(Context context, Activity activity) {
this.context = context;
this.activity = activity;
// [START android_playbilling_initialize_java]
// 1. Initialize the BillingClient.
PurchasesUpdatedListener purchasesUpdatedListener =
(billingResult, purchases) -> {
// To be implemented in a later section.
if (billingResult.getResponseCode() == BillingResponseCode.OK && purchases != null) {
for (Purchase purchase : (List<Purchase>) purchases) {
// Process the purchase.
}
} else if (billingResult.getResponseCode() == BillingResponseCode.USER_CANCELED) {
// Handle an error caused by a user canceling the purchase flow.
} else {
// Handle any other error codes.
}
};
this.billingClient =
BillingClient.newBuilder(context)
.setListener(purchasesUpdatedListener)
// [START android_playbilling_enableautoreconnect_java]
.enablePendingPurchases(PendingPurchasesParams.newBuilder()
.enableOneTimeProducts()
.build())
// [END android_playbilling_enableautoreconnect_java]
.build();
// [END android_playbilling_initialize_java]
startConnection();
}
public void startConnection() {
// [START android_playbilling_startconnection_java]
billingClient.startConnection(
new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingResponseCode.OK) {
// The BillingClient is ready. You can query purchases here.
// It's a good practice to query products after the connection is established.
queryProductDetails();
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
// This is automatically handled by the library when you call a method that requires a connection.
}
});
// [END android_playbilling_startconnection_java]
}
public void queryProductDetails() {
// [START android_playbilling_queryproductdetails_java]
QueryProductDetailsParams queryProductDetailsParams =
QueryProductDetailsParams.newBuilder()
.setProductList(
ImmutableList.of(
QueryProductDetailsParams.Product.newBuilder()
.setProductId("product_id_example")
.setProductType(ProductType.SUBS)
.build()))
.build();
billingClient.queryProductDetailsAsync(
queryProductDetailsParams,
(billingResult, fetchedProductDetailsList) -> {
if (billingResult.getResponseCode() == BillingResponseCode.OK && fetchedProductDetailsList != null) {
this.productDetailsList = fetchedProductDetailsList.getProductDetailsList();
// Now that the list is populated, you can use it.
// For example, find a specific product.
if (!this.productDetailsList.isEmpty()) {
this.productDetails = this.productDetailsList.get(0);
// Any methods that require productDetails should be called from here.
}
}
});
// [END android_playbilling_queryproductdetails_java]
}
public void consumeProduct(Purchase purchase) {
// [START android_playbilling_consumeproduct_java]
ConsumeParams consumeParams =
ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build();
ConsumeResponseListener listener =
(billingResult, purchaseToken) -> {
if (billingResult.getResponseCode() == BillingResponseCode.OK) {
// Handle the success of the consume operation.
}
};
billingClient.consumeAsync(consumeParams, listener);
// [END android_playbilling_consumeproduct_java]
}
public void acknowledgePurchase(Purchase purchase) {
// [START android_playbilling_acknowledge_java]
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams, (billingResult) -> {
// Acknowledgment handled.
});
}
}
// [END android_playbilling_acknowledge_java]
}
public void getBillingConfigAsync() {
// [START android_playbilling_getbillingconfig_java]
GetBillingConfigParams getBillingConfigParams = GetBillingConfigParams.newBuilder().build();
billingClient.getBillingConfigAsync(
getBillingConfigParams,
(billingResult, billingConfig) -> {
if (billingResult.getResponseCode() == BillingResponseCode.OK && billingConfig != null) {
String countryCode = billingConfig.getCountryCode();
} else {
// TODO: Handle errors
}
});
// [END android_playbilling_getbillingconfig_java]
}
public void changeSubscriptionPlan() {
if (productDetails == null) {
// Can't launch the flow if product details aren't loaded yet.
// You could initiate a query here or show an error message.
return;
}
String purchaseTokenOfExistingSubscription = "purchase_token";
// [START android_playbilling_subscription_replace_java]
ProductDetailsParams productDetailsParams =
ProductDetailsParams.newBuilder()
.setProductDetails(productDetails)
.build();
BillingFlowParams billingFlowParams =
BillingFlowParams.newBuilder()
.setSubscriptionUpdateParams(
SubscriptionUpdateParams.newBuilder()
.setOldPurchaseToken(purchaseTokenOfExistingSubscription)
.build())
.setProductDetailsParamsList(ImmutableList.of(productDetailsParams))
.build();
billingClient.launchBillingFlow(activity, billingFlowParams);
// [END android_playbilling_subscription_replace_java]
}
public void changeSubscriptionPlanDeprecated() {
if (productDetails == null || productDetails.getSubscriptionOfferDetails() == null || productDetails.getSubscriptionOfferDetails().isEmpty()) {
// Can't launch the flow if product details or offers aren't loaded yet.
return;
}
int selectedOfferIndex = 0;
// [START android_playbilling_subscription_update_deprecated_java]
String offerToken =
productDetails.getSubscriptionOfferDetails().get(selectedOfferIndex).getOfferToken();
BillingFlowParams billingFlowParams =
BillingFlowParams.newBuilder()
.setProductDetailsParamsList(
ImmutableList.of(
ProductDetailsParams.newBuilder()
// fetched via queryProductDetailsAsync
.setProductDetails(productDetails)
// offerToken can be found in
// ProductDetails=>SubscriptionOfferDetails
.setOfferToken(offerToken)
.build()))
.setSubscriptionUpdateParams(
SubscriptionUpdateParams.newBuilder()
// purchaseToken can be found in Purchase#getPurchaseToken
.setOldPurchaseToken("old_purchase_token")
.setSubscriptionReplacementMode(
SubscriptionUpdateParams.ReplacementMode.CHARGE_FULL_PRICE)
.build())
.build();
BillingResult billingResult = billingClient.launchBillingFlow(activity, billingFlowParams);
// ...
// [END android_playbilling_subscription_update_deprecated_java]
}
public void inAppMessaging() {
// [START android_playbilling_inappmessaging_java]
InAppMessageParams inAppMessageParams =
InAppMessageParams.newBuilder()
.addInAppMessageCategoryToShow(
InAppMessageParams.InAppMessageCategoryId.TRANSACTIONAL)
.build();
billingClient.showInAppMessages(
activity,
inAppMessageParams,
(inAppMessageResult) -> {
if (inAppMessageResult.getResponseCode()
== InAppMessageResult.InAppMessageResponseCode.NO_ACTION_NEEDED) {
// an in-app message was already displayed within the last day
}
});
// [END android_playbilling_inappmessaging_java]
}
// Unused methods from the original file have been removed for clarity
// (e.g., onPurchasesUpdated, handlePurchaseRecap) as their logic is
// now integrated into the PurchasesUpdatedListener in the constructor.
}