I am using the Android In-App Billing v3 Library (here is the link). The application I'm making converts Google Play Rewards to cash which is transfer to the desired mobile wallet of the user. I am facing a problem that according to how my app functions a single product should be ready to be purchased over and over again but when I buy the item and try to buy the same item again, it shows me the successful payment activity that is shown when the onProductPurchased() is called.
I know that I have to add consumable products to the play Console but I couldn't find the right way and neither the way how to consume that. This is my first time handling in-app purchases; please guide me through the process. Here are the code and some screenshots of my application.
package com.payapp.app;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import com.anjlab.android.iab.v3.BillingProcessor;
import com.anjlab.android.iab.v3.TransactionDetails;
import java.util.ArrayList;
import java.util.List;
public class Paytm extends AppCompatActivity implements
BillingProcessor.IBillingHandler {
BillingProcessor bp;
Button cont;
String selectedPrice;
Spinner price;
List<String> price_array = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paytm);
getSupportActionBar().setTitle("Paytm");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
cont = findViewById(R.id.continuebtn);
price_array.add("50");
price_array.add("100");
price_array.add("150");
price_array.add("200");
price_array.add("500");
price_array.add("1000");
/* Spinner Initialization starts */
price = findViewById(R.id.price);
price.setSelection(0);
price.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedPrice = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
selectedPrice = "Football";
}
});
ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, price_array);
categoriesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
price.setAdapter(categoriesAdapter);
bp = new BillingProcessor(this, "<Removed the license key>", this);
bp.initialize();
cont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (selectedPrice.equals("50"))
{
bp.purchase(Paytm.this,"pay_50");}
if (selectedPrice.equals("100"))
{
bp.purchase(Paytm.this,"pay_100");}
if (selectedPrice.equals("150"))
{
bp.purchase(Paytm.this,"pay_150");}
if (selectedPrice.equals("200"))
{
bp.purchase(Paytm.this,"pay_200");}
if (selectedPrice.equals("500"))
{
bp.purchase(Paytm.this,"pay_500");}
if (selectedPrice.equals("1000"))
{
bp.purchase(Paytm.this,"pay_1000");}
}
});
}
@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
startActivity(new Intent(Paytm.this,PaymentSuccess.class));
}
@Override
public void onPurchaseHistoryRestored() {
}
@Override
public void onBillingError(int errorCode, @Nullable Throwable error) {
startActivity(new Intent(Paytm.this,PaymentFailed.class));
}
@Override
public void onBillingInitialized() {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!bp.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onDestroy() {
if (bp != null) {
bp.release();
}
super.onDestroy();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
onBackPressed();
return true;
}
}
Here are the screenshots for better understanding
Mobile wallet selection screen
The products in my console (managed, no idea how to add consumable products)
Any added product can be used as consumable product. However to achieve this you need to consume the purchased product.
If you read the documentation in the Android In-App Billing v3 Library you'll find your answer.