如何让用户在 android 应用内购买中一遍又一遍地购买相同的产品
How to enable a user to buy the same product over and over in android In-app purchases
我正在使用 Android 应用内结算 v3 库 (here is the link)。我正在制作的应用程序将 Google Play Rewards 转换为现金,然后转移到用户所需的移动钱包中。我面临一个问题,根据我的应用程序的功能,应该准备好一次又一次地购买单个产品,但是当我购买该商品并尝试再次购买同一商品时,它显示我已成功付款 activity 显示时
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
Product selection screen
The products in my console (managed, no idea how to add consumable products)
任何添加的产品都可以用作消耗品。然而,要实现这一点,您需要消费购买的产品。
如果您阅读 Android 应用内结算 v3 库中的文档,您会找到答案。
我正在使用 Android 应用内结算 v3 库 (here is the link)。我正在制作的应用程序将 Google Play Rewards 转换为现金,然后转移到用户所需的移动钱包中。我面临一个问题,根据我的应用程序的功能,应该准备好一次又一次地购买单个产品,但是当我购买该商品并尝试再次购买同一商品时,它显示我已成功付款 activity 显示时 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
Product selection screen
The products in my console (managed, no idea how to add consumable products)
任何添加的产品都可以用作消耗品。然而,要实现这一点,您需要消费购买的产品。
如果您阅读 Android 应用内结算 v3 库中的文档,您会找到答案。