如何检查用户是否购买了 IAP 提供的产品
How to check if user bought a product offered by IAP
在我的应用程序中,我建议用户使用 Google Play IAP 进行捐赠,在 return 中,我删除了广告并解锁了高级功能。
当我的应用程序加载时我想检查用户是否进行了捐赠,如何通过代码知道在用户进行捐赠后我将调用以下代码允许用户在需要时进行未来捐赠。
所以,我想允许用户根据需要进行进一步的捐赠,但我想知道他是否已经捐赠以禁用广告和解锁高级功能。
BillingProcessor bp;
bp.consumePurchase(productId);
请注意,我的问题是关于 IAP 在线过程,而不是关于离线保存值并稍后检查。
我认为本指南应该有助于向您展示如何执行此操作:
https://developer.android.com/google/play/billing/billing_library_overview
Query cached purchases
To retrieve information about purchases that a
user makes from your app, call the queryPurchases() method with the
purchase type (SkuType.INAPP or SkuType.SUBS) on the Play Billing
Library client. For example:
PurchasesResult purchasesResult = mBillingClient.queryPurchases(SkuType.INAPP);
Google Play returns the
purchases made by the user account logged in to the device. If the
request is successful, the Play Billing Library stores the query
results in a List of Purchase objects.
Note: Only active subscriptions appear on this list. As long as the
in-app product is on this list, the user should have access to it. For
further information, refer to Handle SUBSCRPTION_ON_HOLD section of
the Add subscription-specific features document. To retrieve the list,
call the getPurchasesList() method on the PurchasesResult object. You
can then call a variety of methods on the Purchase object to view
relevant information about the item, such as its purchase state or
time. To view the types of product detail information that are
available, see the list of methods in the Purchase class.
Call queryPurchases() at least twice in your code:
Every time your app launches so that you can restore any purchases
that a user has made since the app last stopped. In your onResume()
method because a user can make a purchase when your app is in the
background (for example, redeeming a promo code in Play Store app).
Calling queryPurchases() on startup and resume guarantees that your
app finds out about all purchases and redemptions the user may have
made while the app wasn't running. Furthermore, if a user makes a
purchase while the app is running and your app misses it for any
reason, your app still finds out about the purchase the next time the
activity resumes and calls queryPurchases().
Query most recent purchases
The queryPurchases() method uses a cache
of the Google Play Store app without initiating a network request. If
you need to check the most recent purchase made by the user for each
product ID, you can use the queryPurchaseHistoryAsync() method and
pass the purchase type and a PurchaseHistoryResponseListener to handle
the query result.
queryPurchaseHistoryAsync() returns the most recent purchase made by
the user for each product ID, even if that purchase is expired,
cancelled, or consumed. Use the queryPurchases() method whenever
possible, as it uses the local cache, instead of the
queryPurchaseHistoryAsync() method. You could combine
queryPurchaseHistoryAsync() with a Refresh button allowing users to
update their list of purchases.
The following code demonstrates how you can override the
onPurchaseHistoryResponse() method:
mBillingClient.queryPurchaseHistoryAsync(SkuType.INAPP,
new PurchaseHistoryResponseListener() {
@Override
public void onPurchaseHistoryResponse(@BillingResponse int responseCode,
List purchasesList) {
if (responseCode == BillingResponse.OK
&& purchasesList != null) {
for (Purchase purchase : purchasesList) {
// Process the result.
}
}
} });
你可以使用这个:
Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS); //Or SkuType.INAPP
if (purchasesResult.getPurchasesList() != null) {
for (Purchase purchase : purchasesResult.getPurchasesList()) {
if (purchase.getSku().equals("your_product_id")) handlePurchase(purchase);
}
[...]
void handlePurchase(Purchase purchase) {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
premium = true; //In casse purchase was acknowledge before
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
premium = true;
}
};
billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
}
}
}
如有任何问题,请继续评论。
正如 nasch 和 AlexBSC 已经回答的那样,您必须获取可能进行的购买。
但是,最新的执行此操作的方法是调用 BillingClient.queryPurchasesAsync()
,如 here 中所述。您至少应该在 onResume()
和 onCreate()
.
中调用它
比如这样,
billingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS, new PurchasesResponseListener() {
@Override
public void onQueryPurchasesResponse(BillingResult billingResult, List<Purchase> purchases) {
if (billingResult.getResponseCode() == OK
&& purchases != null) {
for (Purchase purchase : purchases) {
handlePurchase(purchase);
}
}
}
});
关注 these steps 应该会让你走得更远。
在我的应用程序中,我建议用户使用 Google Play IAP 进行捐赠,在 return 中,我删除了广告并解锁了高级功能。
当我的应用程序加载时我想检查用户是否进行了捐赠,如何通过代码知道在用户进行捐赠后我将调用以下代码允许用户在需要时进行未来捐赠。
所以,我想允许用户根据需要进行进一步的捐赠,但我想知道他是否已经捐赠以禁用广告和解锁高级功能。
BillingProcessor bp;
bp.consumePurchase(productId);
请注意,我的问题是关于 IAP 在线过程,而不是关于离线保存值并稍后检查。
我认为本指南应该有助于向您展示如何执行此操作:
https://developer.android.com/google/play/billing/billing_library_overview
Query cached purchases
To retrieve information about purchases that a user makes from your app, call the queryPurchases() method with the purchase type (SkuType.INAPP or SkuType.SUBS) on the Play Billing Library client. For example:
PurchasesResult purchasesResult = mBillingClient.queryPurchases(SkuType.INAPP);
Google Play returns the purchases made by the user account logged in to the device. If the request is successful, the Play Billing Library stores the query results in a List of Purchase objects.
Note: Only active subscriptions appear on this list. As long as the in-app product is on this list, the user should have access to it. For further information, refer to Handle SUBSCRPTION_ON_HOLD section of the Add subscription-specific features document. To retrieve the list, call the getPurchasesList() method on the PurchasesResult object. You can then call a variety of methods on the Purchase object to view relevant information about the item, such as its purchase state or time. To view the types of product detail information that are available, see the list of methods in the Purchase class.
Call queryPurchases() at least twice in your code:
Every time your app launches so that you can restore any purchases that a user has made since the app last stopped. In your onResume() method because a user can make a purchase when your app is in the background (for example, redeeming a promo code in Play Store app). Calling queryPurchases() on startup and resume guarantees that your app finds out about all purchases and redemptions the user may have made while the app wasn't running. Furthermore, if a user makes a purchase while the app is running and your app misses it for any reason, your app still finds out about the purchase the next time the activity resumes and calls queryPurchases().
Query most recent purchases
The queryPurchases() method uses a cache of the Google Play Store app without initiating a network request. If you need to check the most recent purchase made by the user for each product ID, you can use the queryPurchaseHistoryAsync() method and pass the purchase type and a PurchaseHistoryResponseListener to handle the query result.
queryPurchaseHistoryAsync() returns the most recent purchase made by the user for each product ID, even if that purchase is expired, cancelled, or consumed. Use the queryPurchases() method whenever possible, as it uses the local cache, instead of the queryPurchaseHistoryAsync() method. You could combine queryPurchaseHistoryAsync() with a Refresh button allowing users to update their list of purchases.
The following code demonstrates how you can override the onPurchaseHistoryResponse() method:
mBillingClient.queryPurchaseHistoryAsync(SkuType.INAPP,
new PurchaseHistoryResponseListener() {
@Override
public void onPurchaseHistoryResponse(@BillingResponse int responseCode,
List purchasesList) {
if (responseCode == BillingResponse.OK
&& purchasesList != null) {
for (Purchase purchase : purchasesList) {
// Process the result.
}
}
} });
你可以使用这个:
Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS); //Or SkuType.INAPP
if (purchasesResult.getPurchasesList() != null) {
for (Purchase purchase : purchasesResult.getPurchasesList()) {
if (purchase.getSku().equals("your_product_id")) handlePurchase(purchase);
}
[...]
void handlePurchase(Purchase purchase) {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
premium = true; //In casse purchase was acknowledge before
if (!purchase.isAcknowledged()) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
premium = true;
}
};
billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
}
}
}
如有任何问题,请继续评论。
正如 nasch 和 AlexBSC 已经回答的那样,您必须获取可能进行的购买。
但是,最新的执行此操作的方法是调用 BillingClient.queryPurchasesAsync()
,如 here 中所述。您至少应该在 onResume()
和 onCreate()
.
比如这样,
billingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS, new PurchasesResponseListener() {
@Override
public void onQueryPurchasesResponse(BillingResult billingResult, List<Purchase> purchases) {
if (billingResult.getResponseCode() == OK
&& purchases != null) {
for (Purchase purchase : purchases) {
handlePurchase(purchase);
}
}
}
});
关注 these steps 应该会让你走得更远。