I have recently been trying to implement android in-app billing to my app. But when I use the "Test card: Decline after a few minutes" it takes many hours (~24h) before the purchase is canceled.
I have read all the documentation on the play billing library https://developer.android.com/google/play/billing/billing_overview and followed there implementation.
public class StoreActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
ExtendedFloatingActionButton eFabNoAds = findViewById(R.id.efab_no_ads);
eFabNoAds.setOnClickListener((v) -> {
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setSkuDetails(MainActivity.skuDetailsList.get(0)).build();
MainActivity.billingClient.launchBillingFlow(this, billingFlowParams);
});
ExtendedFloatingActionButton eFab20Hints = findViewById(R.id.efab_20hints);
eFab20Hints.setOnClickListener((v) -> {
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setSkuDetails(MainActivity.skuDetailsList.get(1)).build();
MainActivity.billingClient.launchBillingFlow(this, billingFlowParams);
});
}
public class MainActivity extends AppCompatActivity implements PurchasesUpdatedListener
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
setupBillingClient();
}
...
private void setupBillingClient() {
billingClient = BillingClient
.newBuilder(this)
.enablePendingPurchases()
.setListener(this).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Toast.makeText(MainActivity.this, "Success to connect Billing", Toast.LENGTH_SHORT).show();
SkuDetailsParams params = SkuDetailsParams.newBuilder()
.setSkusList(Arrays.asList("no_ads", "hints"))
.setType(BillingClient.SkuType.INAPP)
.build();
billingClient.querySkuDetailsAsync(params, (billingRes, skuDetailsList) -> {
if (billingRes.getResponseCode() == BillingClient.BillingResponseCode.OK) {
MainActivity.skuDetailsList = skuDetailsList;
}
});
}
}
@Override
public void onBillingServiceDisconnected() {
billingClient = null;
}
});
}
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
//If user clicks TAP-BUY, will retrieve data here
if (purchases != null) {
for (Purchase p : purchases) {
handlePurchase(p);
}
Toast.makeText(this, "Purchase item: " + purchases.size(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Purchase list empty", Toast.LENGTH_SHORT).show();
}
}
AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
Log.i(TAG, billingResult.getDebugMessage());
}
};
ConsumeResponseListener consumeResponseListener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
Log.i(TAG, billingResult.getDebugMessage());
}
};
void handlePurchase(Purchase purchase) {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
// Grant entitlement to the user.
// Acknowledge the purchase if it hasn't already been acknowledged.
if (!purchase.isAcknowledged()) {
if (purchase.getSku().equals("no_ads")) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
} else {
ConsumeParams consumeParams = ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.consumeAsync(consumeParams, consumeResponseListener);
}
}
} else if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING) {
// Here you can confirm to the user that they've started the pending
// purchase, and to complete it, they should follow instructions that
// are given to them. You can also choose to remind the user in the
// future to complete the purchase if you detect that it is still
// pending.
////// DO I NEED TO DO ANYTHING HERE? /////
}
}
}
I would like my users to be able to purchase how many hints as they want. And I do not want them to wait for the pending purchase to be canceled.
Is there somehow I can cancel a pending purchase? or make the user complete the purchase when he/she tries to purchase it next time?
Developers cannot cancel the pending order. Only users can.
If an user has a pending order, then you'll be able to tell once you call queryPurchases(). You can check the purchaseState and purchaseState.PENDING indicates that the user has a pending purchase. You can render the item button in a different way to remind user to complete the order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With