android: Inapp billing: Error response: 7:Item Already Owned

前端 未结 5 1032
面向向阳花
面向向阳花 2021-01-31 09:09

I am learning to implement an in-app billing for my app such that people can for example, donate $ when press the donate button.

The user is allowed to donate more than

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-31 09:21

    I'm using: implementation 'com.android.billingclient:billing:2.0.0' and had the same error on purchase process.

    • the point is: before of launching the purchase we should consume the pending purchases.
    • please see the code snippet below:

          List skuList = new ArrayList();
          skuList.add(THE_IAP_ID);
          BillingClient.Builder billingClientBuilder = BillingClient.newBuilder(context).setListener(new PurchasesUpdatedListener() {
              @Override
              public void onPurchasesUpdated(BillingResult billingResult, @Nullable List purchases) {
                   // here we have to check the result ...
      
              });
      
              billingClientBuilder.enablePendingPurchases();
              billingClient = billingClientBuilder.build();
              billingClient.startConnection(new BillingClientStateListener() {
              @Override
              public void onBillingSetupFinished(BillingResult billingResult) {
                  if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                      // The BillingClient is ready - query purchases.
      
                      Purchase.PurchasesResult pr = billingClient.queryPurchases(BillingClient.SkuType.INAPP);
                      List pList = pr.getPurchasesList();
                      for (Purchase iitem : pList) {
                          ConsumeParams consumeParams = ConsumeParams.newBuilder()
                                  .setPurchaseToken(iitem.getPurchaseToken())
                                  .build();
                          billingClient.consumeAsync(consumeParams, consumeResponseListener);
                      }
      
                      // process the purchase
                  } else {
                      // cancelled or s.e. 
                      ...
                  }
              }
      

    Best Regards, Have you fun :)

提交回复
热议问题