我目前正在使用this库在我的应用程序中实现应用内结算。由于简单,我选择使用这个库。
这是我实施它的方式:
public class MainActivity extends AppCompatActivity {
private Button buyButton;
BillingProcessor bp;
TextView freeorfull;
String LICENSE_KEY = "MyKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
freeorfull = (TextView) findViewById(R.id.freeorfull);
buyButton = (Button) findViewById(R.id.buyButton);
buyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!BillingProcessor.isIabServiceAvailable(getApplicationContext())) {
Toast.makeText(getApplicationContext(), "Please upgrade Android Market/Play store to version >= 3.9.16",
Toast.LENGTH_LONG).show();
} else {
bp.purchase(MainActivity.this, "myProductKey");
}
}
});
buyit();
if (bp.isPurchased("mybuyfullversion")) {
freeorfull.setText("FULL oncreate");
}
}
public void buyit() {
bp = new BillingProcessor(this, LICENSE_KEY, new BillingProcessor.IBillingHandler() {
@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
freeorfull.setText("FULL in buyit");
Toast.makeText(getApplicationContext(), "onProductPurchased",
Toast.LENGTH_LONG).show();
}
@Override
public void onBillingError(int errorCode, @Nullable Throwable error) {
Toast.makeText(getApplicationContext(), "onBillingError",
Toast.LENGTH_LONG).show();
if (bp.isPurchased("mybuyfullversion")) {
freeorfull.setText("FULL oncreate");
}
}
@Override
public void onBillingInitialized() {
Toast.makeText(getApplicationContext(), "onBillingInitialized",
Toast.LENGTH_LONG).show();
}
@Override
public void onPurchaseHistoryRestored() {
Toast.makeText(getApplicationContext(), "onPurchaseHistoryRestored",
Toast.LENGTH_LONG).show();
}
});
}
@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();
}
}
我尝试从Google Play控制台测试退款用户,一段时间后退款并且用户(我)收到退款,但应用程序仍显示该商品已被购买。
我正在寻找一种检查用户是否已退款的方法,然后将UI / TextView更改回“免费版”。
我已经看到可以使用以下内容:
Purchase removeAdsPurchase = inventory.getPurchase(SKU_REMOVE_ADS);
if(removeAdsPurchase != null) {
int purchaseStateForRemoveAds = removeAdsPurchase.getPurchaseState();
if(purchaseStateForRemoveAds == 1) {
//Do cancelled purchase stuff here
}
else if(purchaseStateForRemoveAds == 2) {
//Do refunded purchase stuff here
}
}
但是这个库没有Purchase
class,我不明白库存是指什么。
据我了解,我应该让PurchaseState检查购买是否已退款。我该怎么办?
答案 0 :(得分:1)
这就是我使用相同的结算库解决类似问题的方法。要在退款后重置应用,您必须使用BuyActivity或任何可以检查购买状态的 bp.loadOwnedPurchasesFromGoogle()方法。 请注意方法
bp.loadOwnedPurchasesFromGoogle()
需要一段时间才能从Google服务器更新购买更新,因此请勿等待它。
因此,要检查项目是否已购买(包括退款),请尝试:
if(bp.isPurchased(PRODUCT_ID)){
// item is purchased
}
else{
//item not purchased any longer so it is up for buy
}
答案 1 :(得分:0)