我最近一直在尝试对我的应用实施android应用内结算。但是,当我使用“测试卡:几分钟后下降”时,取消购买要花费很多小时(约24小时)。
我已经阅读了游戏计费库https://developer.android.com/google/play/billing/billing_overview上的所有文档,并在此处进行了实现。
public class StoreActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
ExtendedFloatingActionButton eFabNoAds = findViewById(R.id.efab_no_ads);
eFabNoAds.setOnClickListener((v) -> {
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setSkuDetails(MainActivity.skuDetailsList.get(0)).build();
MainActivity.billingClient.launchBillingFlow(this, billingFlowParams);
});
ExtendedFloatingActionButton eFab20Hints = findViewById(R.id.efab_20hints);
eFab20Hints.setOnClickListener((v) -> {
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder()
.setSkuDetails(MainActivity.skuDetailsList.get(1)).build();
MainActivity.billingClient.launchBillingFlow(this, billingFlowParams);
});
}
public class MainActivity extends AppCompatActivity implements PurchasesUpdatedListener
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
setupBillingClient();
}
...
private void setupBillingClient() {
billingClient = BillingClient
.newBuilder(this)
.enablePendingPurchases()
.setListener(this).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Toast.makeText(MainActivity.this, "Success to connect Billing", Toast.LENGTH_SHORT).show();
SkuDetailsParams params = SkuDetailsParams.newBuilder()
.setSkusList(Arrays.asList("no_ads", "hints"))
.setType(BillingClient.SkuType.INAPP)
.build();
billingClient.querySkuDetailsAsync(params, (billingRes, skuDetailsList) -> {
if (billingRes.getResponseCode() == BillingClient.BillingResponseCode.OK) {
MainActivity.skuDetailsList = skuDetailsList;
}
});
}
}
@Override
public void onBillingServiceDisconnected() {
billingClient = null;
}
});
}
@Override
public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
//If user clicks TAP-BUY, will retrieve data here
if (purchases != null) {
for (Purchase p : purchases) {
handlePurchase(p);
}
Toast.makeText(this, "Purchase item: " + purchases.size(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Purchase list empty", Toast.LENGTH_SHORT).show();
}
}
AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
@Override
public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
Log.i(TAG, billingResult.getDebugMessage());
}
};
ConsumeResponseListener consumeResponseListener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
Log.i(TAG, billingResult.getDebugMessage());
}
};
void handlePurchase(Purchase purchase) {
if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
// Grant entitlement to the user.
// Acknowledge the purchase if it hasn't already been acknowledged.
if (!purchase.isAcknowledged()) {
if (purchase.getSku().equals("no_ads")) {
AcknowledgePurchaseParams acknowledgePurchaseParams =
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
} else {
ConsumeParams consumeParams = ConsumeParams.newBuilder()
.setPurchaseToken(purchase.getPurchaseToken())
.build();
billingClient.consumeAsync(consumeParams, consumeResponseListener);
}
}
} else if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING) {
// Here you can confirm to the user that they've started the pending
// purchase, and to complete it, they should follow instructions that
// are given to them. You can also choose to remind the user in the
// future to complete the purchase if you detect that it is still
// pending.
////// DO I NEED TO DO ANYTHING HERE? /////
}
}
}
我希望我的用户能够购买所需数量的提示。而且我不希望他们等待取消的待定购买。
是否可以通过某种方式取消待处理的购买?还是让用户下次尝试购买时完成购买?
开发人员无法取消挂单。只有用户可以。
[如果用户有一个待处理的订单,那么一旦调用queryPurchases(),您就可以知道。您可以检查purchaseState和purchaseState。PENDING表示用户有待处理的购买。您可以用其他方式渲染项目按钮,以提醒用户完成订单。