我正在使用Android应用内结算v3库(here is the link)。我正在制作的应用程序将Google Play奖励转换为现金,然后将其转移到用户所需的移动钱包中。我面临的一个问题是,根据我的应用程序的功能,应该准备一次又一次地购买单个产品,但是当我购买该商品并尝试再次购买同一商品时,它向我显示了所示的成功付款活动当。。。的时候 onProductPurchased()被调用。
我知道我必须将消耗品添加到Play控制台,但是我找不到正确的方法,也找不到如何消费的方法。这是我第一次处理应用内购买;请指导我完成整个过程。这是我的应用程序的代码和一些屏幕截图。
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;
}
}
以下是屏幕截图,可帮助您更好地理解
Mobile wallet selection screen
The products in my console (managed, no idea how to add consumable products)