Google Play Billing Library 3.0+ - 恢复购买
Google Play Billing Library 3.0+ - Restore purchase
有了 Google Play Billing Library v3.0+,我们有了新的购买流程,一切都在这里得到了完美的解释:
Google Play Billing
在旧版本的库中,我们将恢复如下内容:
bp = new BillingProcessor(this, MERCHANT_ID, new BillingProcessor.IBillingHandler() {
@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
String orderId = details.purchaseInfo.purchaseData.productId;
// we then compare the orderID with the SKU and see if the user purchased the item,
// however in the new version of the library there is nothing about restore
但是,文档中没有关于恢复购买的任何内容?
例如,我们有一个用例,您有一个有效的订阅和一个您购买的 IAP 产品。您删除该应用程序并重新安装。您如何恢复订阅和该 IAP 产品?
BillingProcessor 和 onProductPurchased 似乎不是 Play Billing Library(也不是 AIDL)的一部分,它更像是一个包装 class 由 anjlab(https://github.com/anjlab/android-inapp-billing-v3) 实现
为了满足您的需求,我认为 queryPurchases and queryPurchaseHistoryAsync 可以提供帮助。
基本上 queryPurchaseHistoryAsync 完成了这项工作,只是要小心传递 SKU 类型(inapp 或 subs)。
我的实现:
fun restorePurchaseInApp() {
bp.queryPurchaseHistoryAsync("inapp", this)
}
fun restorePurchaseInSubs() {
bp.queryPurchaseHistoryAsync("subs", this)
}
// bp is BillingClient
// the class should implement PurchaseHistoryResponseListener
override fun onPurchaseHistoryResponse(
p0: BillingResult,
p1: MutableList<PurchaseHistoryRecord>?
) {
if (p1 != null) {
Log.d("TMS", "onPurchaseHistoryResponse: " + p1.size)
}
if (p1 != null) {
for (item in p1) {
Log.d("TMS", "onPurchaseHistoryResponse sku: " + item.sku)
Log.d("TMS", "onPurchaseHistoryResponse signature: " + item.signature)
Log.d("TMS", "onPurchaseHistoryResponse purchaseToken: " + item.purchaseToken)
Log.d("TMS", "onPurchaseHistoryResponse purchaseTime: " + item.purchaseTime)
}
}
}
在那里你得到了购买的物品,仅此而已:)。我希望这会有所帮助,因为我浪费了很多时间来弄清楚一些如此简单的事情,而文档实现没有提到这一点。
使用 Google 在 Github 上可用的应用内结算库。
Android 应用内结算 API 的简单实现。
dependencies {
implementation 'com.github.moisoni97:google-inapp-billing:1.0.5'
}
◆ 创建 BillingConnector 实例 class。构造函数将采用 2 个参数:
● Context
● License key from *Play Console*
billingConnector = new BillingConnector(this, "license_key")
.setConsumableIds(consumableIds)
.setNonConsumableIds(nonConsumableIds)
.setSubscriptionIds(subscriptionIds)
.autoAcknowledge()
.autoConsume()
.enableLogging()
.connect();
◆ 实现监听器来处理事件结果和错误:
billingConnector.setBillingEventListener(new BillingEventListener() {
@Override
public void onProductsFetched(@NonNull List<SkuInfo> skuDetails) {
/*Provides a list with fetched products*/
}
@Override
public void onPurchasedProductsFetched(@NonNull List<PurchaseInfo> purchases) {
/*Provides a list with fetched purchased products*/
}
@Override
public void onProductsPurchased(@NonNull List<PurchaseInfo> purchases) {
/*Callback after a product is purchased*/
}
@Override
public void onPurchaseAcknowledged(@NonNull PurchaseInfo purchase) {
/*Callback after a purchase is acknowledged*/
/*
* Grant user entitlement for NON-CONSUMABLE products and SUBSCRIPTIONS here
*
* Even though onProductsPurchased is triggered when a purchase is successfully made
* there might be a problem along the way with the payment and the purchase won't be acknowledged
*
* Google will refund users purchases that aren't acknowledged in 3 days
*
* To ensure that all valid purchases are acknowledged the library will automatically
* check and acknowledge all unacknowledged products at the startup
* */
}
@Override
public void onPurchaseConsumed(@NonNull PurchaseInfo purchase) {
/*Callback after a purchase is consumed*/
/*
* CONSUMABLE products entitlement can be granted either here or in onProductsPurchased
* */
}
@Override
public void onBillingError(@NonNull BillingConnector billingConnector, @NonNull BillingResponse response) {
/*Callback after an error occurs*/
switch (response.getErrorType()) {
case CLIENT_NOT_READY:
//TODO - client is not ready yet
break;
case CLIENT_DISCONNECTED:
//TODO - client has disconnected
break;
case SKU_NOT_EXIST:
//TODO - sku does not exist
break;
case CONSUME_ERROR:
//TODO - error during consumption
break;
case ACKNOWLEDGE_ERROR:
//TODO - error during acknowledgment
break;
case ACKNOWLEDGE_WARNING:
/*
* This will be triggered when a purchase can not be acknowledged because the state is PENDING
* A purchase can be acknowledged only when the state is PURCHASED
*
* PENDING transactions usually occur when users choose cash as their form of payment
*
* Here users can be informed that it may take a while until the purchase complete
* and to come back later to receive their purchase
* */
//TODO - warning during acknowledgment
break;
case FETCH_PURCHASED_PRODUCTS_ERROR:
//TODO - error occurred while querying purchased products
break;
case BILLING_ERROR:
//TODO - error occurred during initialization / querying sku details
break;
case USER_CANCELED:
//TODO - user pressed back or canceled a dialog
break;
case SERVICE_UNAVAILABLE:
//TODO - network connection is down
break;
case BILLING_UNAVAILABLE:
//TODO - billing API version is not supported for the type requested
break;
case ITEM_UNAVAILABLE:
//TODO - requested product is not available for purchase
break;
case DEVELOPER_ERROR:
//TODO - invalid arguments provided to the API
break;
case ERROR:
//TODO - fatal error during the API action
break;
case ITEM_ALREADY_OWNED:
//TODO - failure to purchase since item is already owned
break;
case ITEM_NOT_OWNED:
//TODO - failure to consume since item is not owned
break;
}
}
});
开始购买
● 购买 non-consumable/consumable 产品:
billingConnector.purchase(this, "sku_id");
● 购买订阅:
billingConnector.subscribe(this, "sku_id");
● 取消订阅:
billingConnector.unsubscribe(this, "sku_id");
有了 Google Play Billing Library v3.0+,我们有了新的购买流程,一切都在这里得到了完美的解释: Google Play Billing
在旧版本的库中,我们将恢复如下内容:
bp = new BillingProcessor(this, MERCHANT_ID, new BillingProcessor.IBillingHandler() {
@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
String orderId = details.purchaseInfo.purchaseData.productId;
// we then compare the orderID with the SKU and see if the user purchased the item,
// however in the new version of the library there is nothing about restore
但是,文档中没有关于恢复购买的任何内容?
例如,我们有一个用例,您有一个有效的订阅和一个您购买的 IAP 产品。您删除该应用程序并重新安装。您如何恢复订阅和该 IAP 产品?
BillingProcessor 和 onProductPurchased 似乎不是 Play Billing Library(也不是 AIDL)的一部分,它更像是一个包装 class 由 anjlab(https://github.com/anjlab/android-inapp-billing-v3) 实现 为了满足您的需求,我认为 queryPurchases and queryPurchaseHistoryAsync 可以提供帮助。
基本上 queryPurchaseHistoryAsync 完成了这项工作,只是要小心传递 SKU 类型(inapp 或 subs)。
我的实现:
fun restorePurchaseInApp() {
bp.queryPurchaseHistoryAsync("inapp", this)
}
fun restorePurchaseInSubs() {
bp.queryPurchaseHistoryAsync("subs", this)
}
// bp is BillingClient
// the class should implement PurchaseHistoryResponseListener
override fun onPurchaseHistoryResponse(
p0: BillingResult,
p1: MutableList<PurchaseHistoryRecord>?
) {
if (p1 != null) {
Log.d("TMS", "onPurchaseHistoryResponse: " + p1.size)
}
if (p1 != null) {
for (item in p1) {
Log.d("TMS", "onPurchaseHistoryResponse sku: " + item.sku)
Log.d("TMS", "onPurchaseHistoryResponse signature: " + item.signature)
Log.d("TMS", "onPurchaseHistoryResponse purchaseToken: " + item.purchaseToken)
Log.d("TMS", "onPurchaseHistoryResponse purchaseTime: " + item.purchaseTime)
}
}
}
在那里你得到了购买的物品,仅此而已:)。我希望这会有所帮助,因为我浪费了很多时间来弄清楚一些如此简单的事情,而文档实现没有提到这一点。
使用 Google 在 Github 上可用的应用内结算库。
Android 应用内结算 API 的简单实现。
dependencies {
implementation 'com.github.moisoni97:google-inapp-billing:1.0.5'
}
◆ 创建 BillingConnector 实例 class。构造函数将采用 2 个参数:
● Context
● License key from *Play Console*
billingConnector = new BillingConnector(this, "license_key")
.setConsumableIds(consumableIds)
.setNonConsumableIds(nonConsumableIds)
.setSubscriptionIds(subscriptionIds)
.autoAcknowledge()
.autoConsume()
.enableLogging()
.connect();
◆ 实现监听器来处理事件结果和错误:
billingConnector.setBillingEventListener(new BillingEventListener() {
@Override
public void onProductsFetched(@NonNull List<SkuInfo> skuDetails) {
/*Provides a list with fetched products*/
}
@Override
public void onPurchasedProductsFetched(@NonNull List<PurchaseInfo> purchases) {
/*Provides a list with fetched purchased products*/
}
@Override
public void onProductsPurchased(@NonNull List<PurchaseInfo> purchases) {
/*Callback after a product is purchased*/
}
@Override
public void onPurchaseAcknowledged(@NonNull PurchaseInfo purchase) {
/*Callback after a purchase is acknowledged*/
/*
* Grant user entitlement for NON-CONSUMABLE products and SUBSCRIPTIONS here
*
* Even though onProductsPurchased is triggered when a purchase is successfully made
* there might be a problem along the way with the payment and the purchase won't be acknowledged
*
* Google will refund users purchases that aren't acknowledged in 3 days
*
* To ensure that all valid purchases are acknowledged the library will automatically
* check and acknowledge all unacknowledged products at the startup
* */
}
@Override
public void onPurchaseConsumed(@NonNull PurchaseInfo purchase) {
/*Callback after a purchase is consumed*/
/*
* CONSUMABLE products entitlement can be granted either here or in onProductsPurchased
* */
}
@Override
public void onBillingError(@NonNull BillingConnector billingConnector, @NonNull BillingResponse response) {
/*Callback after an error occurs*/
switch (response.getErrorType()) {
case CLIENT_NOT_READY:
//TODO - client is not ready yet
break;
case CLIENT_DISCONNECTED:
//TODO - client has disconnected
break;
case SKU_NOT_EXIST:
//TODO - sku does not exist
break;
case CONSUME_ERROR:
//TODO - error during consumption
break;
case ACKNOWLEDGE_ERROR:
//TODO - error during acknowledgment
break;
case ACKNOWLEDGE_WARNING:
/*
* This will be triggered when a purchase can not be acknowledged because the state is PENDING
* A purchase can be acknowledged only when the state is PURCHASED
*
* PENDING transactions usually occur when users choose cash as their form of payment
*
* Here users can be informed that it may take a while until the purchase complete
* and to come back later to receive their purchase
* */
//TODO - warning during acknowledgment
break;
case FETCH_PURCHASED_PRODUCTS_ERROR:
//TODO - error occurred while querying purchased products
break;
case BILLING_ERROR:
//TODO - error occurred during initialization / querying sku details
break;
case USER_CANCELED:
//TODO - user pressed back or canceled a dialog
break;
case SERVICE_UNAVAILABLE:
//TODO - network connection is down
break;
case BILLING_UNAVAILABLE:
//TODO - billing API version is not supported for the type requested
break;
case ITEM_UNAVAILABLE:
//TODO - requested product is not available for purchase
break;
case DEVELOPER_ERROR:
//TODO - invalid arguments provided to the API
break;
case ERROR:
//TODO - fatal error during the API action
break;
case ITEM_ALREADY_OWNED:
//TODO - failure to purchase since item is already owned
break;
case ITEM_NOT_OWNED:
//TODO - failure to consume since item is not owned
break;
}
}
});
开始购买
● 购买 non-consumable/consumable 产品:
billingConnector.purchase(this, "sku_id");
● 购买订阅:
billingConnector.subscribe(this, "sku_id");
● 取消订阅:
billingConnector.unsubscribe(this, "sku_id");