1

I'm trying to implement in app purchase subscription to my android java app and following the tutorials from this link

https://developer.android.com/google/play/billing/integrate#java.

I created a new test project and following the steps, but not sure what do I have to put in the following two fields.

 BillingClient client = ...
 AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = ...

public class MainActivity extends AppCompatActivity {

private PurchasesUpdatedListener purchasesUpdatedListener = new PurchasesUpdatedListener() {
    @Override
    public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
                && purchases != null) {
            for (Purchase purchase : purchases) {
                handlePurchase(purchase);
            }
        } else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
            // Handle an error caused by a user cancelling the purchase flow.
        } else {
            // Handle any other error codes.
        }
    }

    void handlePurchase(Purchase purchase) {
        // Purchase retrieved from BillingClient#queryPurchases or your PurchasesUpdatedListener.
        //Purchase purchase = ...;

        // Verify the purchase.
        // Ensure entitlement was not already granted for this purchaseToken.
        // Grant entitlement to the user.

        ConsumeParams consumeParams =
                ConsumeParams.newBuilder()
                        .setPurchaseToken(purchase.getPurchaseToken())
                        .build();

        ConsumeResponseListener listener = new ConsumeResponseListener() {
            @Override
            public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    // Handle the success of the consume operation.
                    BillingClient client = ...
                    AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = ...

                    void handlePurchase(Purchase purchase) {
                        if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
                            if (!purchase.isAcknowledged()) {
                                AcknowledgePurchaseParams acknowledgePurchaseParams =
                                        AcknowledgePurchaseParams.newBuilder()
                                                .setPurchaseToken(purchase.getPurchaseToken())
                                                .build();
                                client.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
                            }
                        }
                    }

                }
            }
        };

        billingClient.consumeAsync(consumeParams, listener);
    }
};

private BillingClient billingClient = BillingClient.newBuilder(this)
        .setListener(purchasesUpdatedListener)
        .enablePendingPurchases()
        .build();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    billingClient.startConnection(new BillingClientStateListener() {
        @Override
        public void onBillingSetupFinished(BillingResult billingResult) {
            if (billingResult.getResponseCode() ==  BillingClient.BillingResponseCode.OK) {
                // The BillingClient is ready. You can query purchases here.
                List<String> skuList = new ArrayList<>();
                skuList.add("4");
                SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
                params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
                billingClient.querySkuDetailsAsync(params.build(),
                        new SkuDetailsResponseListener() {
                            @Override
                            public void onSkuDetailsResponse(BillingResult billingResult,
                                                             List<SkuDetails> skuDetailsList) {
                                // Process the result.
                                for (SkuDetails skuDetails : skuDetailsList) {
                                    if (skuDetails.equals("4")) {
                                        BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
                                                .setSkuDetails(skuDetails)
                                                .build();
                                        int responseCode = billingClient.launchBillingFlow(MainActivity.this, billingFlowParams).getResponseCode();
                                    }
                                }
                            }
                        });

            }
        }
        @Override
        public void onBillingServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
        }
    });

}

}

Julia
  • 1,207
  • 4
  • 29
  • 47
  • I am a bit confused at what your question is, the link you provided seems to tell you how to initialise the `BillingClient` etc? – Henry Twist Apr 05 '21 at 22:15
  • I'm trying to add in app purchase to my app. The link shows me how to initialize and open the payment flow. There are two lines of code that I don't understand which I included in my question. I'm not sure how to initialize BillingClient client = ... and AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = ... in my class. – Julia Apr 06 '21 at 02:20
  • But you seem to already have the code instantiating your `BillingClient` in your `MainActivity` for example? `private BillingClient billingClient = BillingClient.newBuilder(...` – Henry Twist Apr 06 '21 at 02:52
  • In the code, I just put ... like in the link. If you search in the sample class that I posted. You see that I have 3 dots to initialize the BillingClient client and AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener. I want to know the proper way to initialize them. Do I just declare them like BillingClient client = new BillingClient() and AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener(), or do I have to reference anything from other parts of the code? – Julia Apr 06 '21 at 11:54

0 Answers0