Search code examples
javaandroidandroid-billing

All in-app purchases get cancelled and refunded within five minutes (Alpha test)


It is my first time implementing in-app billing for my app, and I have set up a one dollar donation that is bought through the click of a button. The following is my activity where all the purchase flow occurs:

public class DonationsActivity extends AppCompatActivity implements PurchasesUpdatedListener{

    private BillingClient billingClient;
    ConsumeResponseListener listener;
    private SkuDetails oneDetails;

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

        billingClient = BillingClient.newBuilder(DonationsActivity.this).setListener(this).enablePendingPurchases().build();
        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    List<String> skuList = new ArrayList<> ();
                    skuList.add("one_dollar_donation");
                    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) {
                                    for(SkuDetails theDetails : skuDetailsList)
                                    {
                                        if(theDetails.getSku().equals("one_dollar_donation"))
                                        {
                                            oneDetails = theDetails;
                                        }      
                                    }
                                }
                            });
                }
            }
            @Override
            public void onBillingServiceDisconnected() {

            }
        });

        listener = new ConsumeResponseListener() {
            @Override
            public void onConsumeResponse(BillingResult billingResult, String outToken) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {

                }
            }
        };
    }

    @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) {
            Toast.makeText(this, getString(R.string.canceled_donation), Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, getString(R.string.error_donation), Toast.LENGTH_LONG).show();
        }
    }

    private void handlePurchase(Purchase purchase)
    {
        if (!purchase.isAcknowledged())
        {
            ConsumeParams consumeParams = ConsumeParams.newBuilder().build();
            billingClient.consumeAsync(consumeParams, listener);
        }
    }

    public void donateOneDollar(View view)
    {
        BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                .setSkuDetails(oneDetails)
                .build();

        BillingResult responseCode = billingClient.launchBillingFlow(DonationsActivity.this, flowParams);
    }

My problem is that every time I make a test purchase it gets cancelled and refunded within the next five minutes. I am also unable to do more purchases during the five minutes period while the purchase is cancelled.

From my understanding this problem is caused because the purchase is not acknowledged (this is the reason why it gets refunded in 5 minutes), and I am not able to make more purchases during the time because Google thinks that I already own the item (I haven't consumed it i.e. the same problem)

I thought I acknowledged and consumed the purchase from within:

private void handlePurchase(Purchase purchase)
    {
        if (!purchase.isAcknowledged())
        {
            ConsumeParams consumeParams = ConsumeParams.newBuilder().build();
            billingClient.consumeAsync(consumeParams, listener);
        }
    }

But I obviously made a mistake, can anybody help me to understand what my mistake is, and how can I solve it?


Solution

  • After a long time trying to figure out the mistake I realized that I missed the setPurchaseToken method in the ConsumeParams, this is the reason the play store didn't know what purchase I consumed.