我最近一直在尝试在我的应用中实现android的应用内计费。但是当我使用“测试卡:几分钟后拒绝”时,在取消购买之前需要很多小时(~24小时)。
我已经阅读了所有关于play计费库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? /////
}
}
}
我希望我的用户能够购买多少提示,他们想要的。我不想让他们等待等待的购买被取消。
有没有什么办法可以取消一个待定的购买?或者让用户在下次尝试购买时完成购买?
发布于 2019-11-07 06:57:52
开发商不能取消挂起的订单。只有用户可以。
如果用户有一个挂起的订单,那么一旦调用queryPurchases(),您就可以知道了。您可以检查purchaseState和purchaseState.PENDING,表明用户有一个待定的购买。您可以以不同的方式呈现项目按钮,以提醒用户完成订单。
https://stackoverflow.com/questions/58067519
复制相似问题