, если кто-то переустанавливает ваше приложение, потерял купленный предмет, но он / она может купить снова бесплатно . Я использую SharedPreferences для этого, и он работает.
Вы можете управлять этим с помощью метода ITEM_ALREAY_OWNED
if(billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
preferences.edit().putBoolean("show_ads", false).apply();
Toast.makeText(this, "Ads removed because you buy before", Toast.LENGTH_LONG).show();
}
и всех
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> list) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK
&& list != null) {
for (Purchase purchase : list) {
if(purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED){
if(!purchase.isAcknowledged()){
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
mBillingClient.acknowledgePurchase(acknowledgePurchaseParams, new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
if(billingResult.getResponseCode()== BillingClient.BillingResponseCode.OK) {
Toast.makeText(MainActivity.this, "Ads Removed", Toast.LENGTH_SHORT).show();
preferences.edit().putBoolean("show_ads", false).apply();
startActivity(new Intent(MainActivity.this, MainActivity.class));
finish();
}else{
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_LONG).show();
}
}
});
}
}
}
} else if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.USER_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else if(billingResult.getResponseCode() == BillingClient.BillingResponseCode.ITEM_ALREADY_OWNED) {
preferences.edit().putBoolean("show_ads", false).apply();
Toast.makeText(this, "Ads removed because you buy before", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
и настроить метод выставления счетов
private void setupBilling() {
mBillingClient = BillingClient.newBuilder(MainActivity.this).setListener(this).enablePendingPurchases().build();
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Toast.makeText(MainActivity.this, "Ödeme sistemi etkin", Toast.LENGTH_SHORT).show();
final List<String> skuList = new ArrayList<> ();
skuList.add("remove_ads");
final SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> list) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && list != null) {
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(list.get(0))
.build();
mBillingClient.launchBillingFlow(MainActivity.this, flowParams);
}
}
});
}else{
Toast.makeText(MainActivity.this, "Ödeme sistemi için google play hesabını kontrol ediniz", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onBillingServiceDisconnected() {
Toast.makeText(MainActivity.this, "Ödeme sistemi şuanda geçerli değil", Toast.LENGTH_SHORT).show();
}
});
}