我正在尝试为Android应用程序创建应用程序内结算,以允许用户购买订阅。我已经做到了,我能够购买订阅,但是我没有得到的是如何将应用程序中的某些功能限制为那些尚未订阅的人?
我似乎找不到关于它的任何教程。如果用户尚未订阅,我想按一下按钮便会提示一个应用内结算窗口。我可以用这段代码来实现。
public void launchBillingFLow(@SkuType String skuType, String productId) {
Runnable launchBillingRequest = () -> {
BillingFlowParams mBillingFlowParams;
mBillingFlowParams = BillingFlowParams.newBuilder()
.setSku(productId)
.setType(skuType)
.build();
mBillingClient.launchBillingFlow((Activity) context, mBillingFlowParams);
};
executeServiceRequest(launchBillingRequest);
}
但是如果用户已经订阅怎么办?所以问题是,我该如何检查用户是否已订阅并执行按钮按下操作,以及是否不显示计费窗口。仅当用户连接到Internet时,我才能获得该信息吗?我需要将该信息存储在设备上吗?
答案 0 :(得分:2)
使用此订阅方法获取购买详细信息-
使用BillingFlowParams.Builder显示应用内计费流程
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
if (billingResponseCode == BillingClient.BillingResponse.OK) {
// The billing client is ready. You can query purchases here.
final Purchase.PurchasesResult purchasesResult = mBillingClient.queryPurchases(BillingClient.SkuType.SUBS);
for (Purchase sourcePurchase : purchasesResult.getPurchasesList()) {
if (sourcePurchase != null) {
ConsumeResponseListener listener = new ConsumeResponseListener() {
@Override
public void onConsumeResponse(final int responseCode, final String purchaseToken) {
// Log.d("anupam2", responseCode + " <-------> "+ purchasesResult.getPurchasesList() + " <-------> " + purchaseToken);
}
};
mBillingClient.consumeAsync(sourcePurchase.getPurchaseToken(), listener);
} else {
}
}
if (purchasesResult.getPurchasesList().size() > 0) {
// Log.d("anupam3", purchasesResult.getPurchasesList().size() + "");
} else {
// Log.d("anupam4", purchasesResult.getPurchasesList().size() + "");
BillingFlowParams.Builder builder = BillingFlowParams.newBuilder().setSku("234r23446").setType(BillingClient.SkuType.SUBS);
int responseCode = mBillingClient.launchBillingFlow(SplashActivity.this, builder.build());
if (responseCode == 7) {
//Item already purchased
}
}
}
}
@Override
public void onBillingServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Toast.makeText(SplashActivity.this, "Failed", Toast.LENGTH_LONG).show();
}
});
mBillingClient = BillingClient.newBuilder(this).setListener(new PurchasesUpdatedListener() {
@Override
public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
// Log.d("anupam", responseCode + "");
if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
for (Purchase purchase : purchases) {
//List of purchases
}
} else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
Toast.makeText(SplashActivity.this, "Sorry, you have canceled purchase Subscription.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SplashActivity.this, "Sorry, some error occurred.", Toast.LENGTH_SHORT).show();
}
}
}).build();
现在,您可以做的是在启动画面中调用此函数,并根据响应代码使用sharedpreference或应用程序中的全局变量保存值。检查您的订阅ID的响应代码是否为7。如果已订阅(对于responseCode == 7,则将值保存为已订阅),则不显示高级功能。
希望有帮助!