如何确认 android 中的应用内购买?
How to acknowledge in-app purchases in android?
我已经浏览过 Play 结算库
https://developer.android.com/google/play/billing/billing_library_overview
您必须在三天内确认所有购买。未能正确确认购买会导致这些购买被退款。
该过程并未明确说明如何确认购买。
这是我试过的
这是正确的方法吗?
提前致谢
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK&&purchases!=null){
Toast.makeText(this, "Purchase Successful", Toast.LENGTH_SHORT).show();
for(Purchase purchase:purchases){
handlePurchase(purchase);
}
}else if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.USER_CANCELED){
Toast.makeText(this, "Purchase Cancelled", Toast.LENGTH_SHORT).show();
}else if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED){
Toast.makeText(this, "Already Purchased", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(this, billingResult.getDebugMessage(), Toast.LENGTH_SHORT).show();
}
//in handlePurchase()
if(!purchase.isAcknowledged())
{
AcknowledgePurchaseParams acknowledgePurchaseParams
= AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.setDeveloperPayload(purchase.getDeveloperPayload())
.build();
client.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK){
Toast.makeText(RemoveAdsActivity.this, "Purchase Acknowledged", Toast.LENGTH_SHORT).show();
}
}
});
}
它提到在 link 将近一半时确认购买。根据类型的不同,确认购买的方式也不同。
private BillingClient mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
//For non-consumables:
mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener());
//For Consumables:
client.consumeAsync(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
我发布的 link 包含一个关于如何处理订阅的示例。
更新
以下是确认 non-consumable 和消费品购买的方法,从 non-consumable 开始:
首先,创建 AcknowledgePurchaseParams
Class 对象。为此,您需要您应该能够轻松获得的购买令牌,因为您应该在 onPurchasesUpdated
方法或您在 onPurchasesUpdated
之后将购买传递给的其他方法中调用它:
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
接下来创建将用作第二个参数的侦听器。这将允许您在确认购买后做一些事情。我在这个例子中显示了一个 snackbar 消息(根据 worbel 的评论,你可以,而且可能应该检查这个 billingResult 的结果):
AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
getMessage("Purchase acknowledged");
}
};
创建这些后,使用您的 BillingClient 调用 acknowledgePurchase
方法:
mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
购买应该被成功确认。
这对 non-consumable 项使用 acknowledgePurchase
。
消耗品购买
这很相似,只是它们的名称发生了变化 - 请参阅上例中对它们的解释:
第一个参数-Params-set-up:
ConsumeParams consumeParams = ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
第二个参数-Listener-set-up:
ConsumeResponseListener consumeResponseListener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
getMessage("Purchase acknowledged");
}
}
现在使用您的 BillingClint 和 consumeAsync:
mBillingClient.consumeAsync(consumeParams, consumeResponseListener);
如果您是新手并使用计费库 4.0.0,那么以上代码将不起作用,因为现在所有计费过程都已放在后台线程中,因此请确保您在计费期间不要调用任何 ui 更新代码过程。
使用:
purchase.getSkus.contains("sku here");
而不是
purchase.getSku.equals("sku here");
我已经浏览过 Play 结算库 https://developer.android.com/google/play/billing/billing_library_overview 您必须在三天内确认所有购买。未能正确确认购买会导致这些购买被退款。 该过程并未明确说明如何确认购买。 这是我试过的 这是正确的方法吗? 提前致谢
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK&&purchases!=null){
Toast.makeText(this, "Purchase Successful", Toast.LENGTH_SHORT).show();
for(Purchase purchase:purchases){
handlePurchase(purchase);
}
}else if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.USER_CANCELED){
Toast.makeText(this, "Purchase Cancelled", Toast.LENGTH_SHORT).show();
}else if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED){
Toast.makeText(this, "Already Purchased", Toast.LENGTH_SHORT).show();
} else{
Toast.makeText(this, billingResult.getDebugMessage(), Toast.LENGTH_SHORT).show();
}
//in handlePurchase()
if(!purchase.isAcknowledged())
{
AcknowledgePurchaseParams acknowledgePurchaseParams
= AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.setDeveloperPayload(purchase.getDeveloperPayload())
.build();
client.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK){
Toast.makeText(RemoveAdsActivity.this, "Purchase Acknowledged", Toast.LENGTH_SHORT).show();
}
}
});
}
它提到在 link 将近一半时确认购买。根据类型的不同,确认购买的方式也不同。
private BillingClient mBillingClient = BillingClient.newBuilder(mActivity).setListener(this).build();
//For non-consumables:
mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener());
//For Consumables:
client.consumeAsync(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
我发布的 link 包含一个关于如何处理订阅的示例。
更新
以下是确认 non-consumable 和消费品购买的方法,从 non-consumable 开始:
首先,创建 AcknowledgePurchaseParams
Class 对象。为此,您需要您应该能够轻松获得的购买令牌,因为您应该在 onPurchasesUpdated
方法或您在 onPurchasesUpdated
之后将购买传递给的其他方法中调用它:
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
接下来创建将用作第二个参数的侦听器。这将允许您在确认购买后做一些事情。我在这个例子中显示了一个 snackbar 消息(根据 worbel 的评论,你可以,而且可能应该检查这个 billingResult 的结果):
AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
getMessage("Purchase acknowledged");
}
};
创建这些后,使用您的 BillingClient 调用 acknowledgePurchase
方法:
mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
购买应该被成功确认。
这对 non-consumable 项使用 acknowledgePurchase
。
消耗品购买
这很相似,只是它们的名称发生了变化 - 请参阅上例中对它们的解释:
第一个参数-Params-set-up:
ConsumeParams consumeParams = ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
第二个参数-Listener-set-up:
ConsumeResponseListener consumeResponseListener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
getMessage("Purchase acknowledged");
}
}
现在使用您的 BillingClint 和 consumeAsync:
mBillingClient.consumeAsync(consumeParams, consumeResponseListener);
如果您是新手并使用计费库 4.0.0,那么以上代码将不起作用,因为现在所有计费过程都已放在后台线程中,因此请确保您在计费期间不要调用任何 ui 更新代码过程。
使用:
purchase.getSkus.contains("sku here");
而不是
purchase.getSku.equals("sku here");