简体   繁体   English

检查用户在 Android 上的 Google Play 国家/地区是否支持应用内购买

[英]Checking if in-app purchases are supported in user's Google Play country on Android

I require in-app purchases for certain functionality in my Android App.我的 Android 应用程序中的某些功能需要应用程序内购买。 For users in countries where in-app purchases are not supported I want to make this functionality available for free.对于不支持应用内购买的国家/地区的用户,我想免费提供此功能。

How can one check if in-app purchases are supported in the user's Google Play country?如何检查用户所在的 Google Play 国家/地区是否支持应用内购买?

Problems with things I tried:我尝试过的问题:

  1. It seems not possible to determine the Play Store Country.似乎无法确定 Play 商店国家/地区。

  2. Checking BillingClient.BillingResponseCode for BILLING_UNAVAILABLE does not help as this response may also be returned if "The Play Store app on the user's device is out of date" or "Google Play is unable to charge the user's payment method".检查BILLING_UNAVAILABLEBillingClient.BillingResponseCode没有帮助,因为如果“用户设备上的 Play 商店应用程序已过期”或“Google Play 无法向用户的付款方式收费”,也可能会返回此响应。

  3. The debugMessage in the BillingResult returned by the onBillingSetupFinished callback of BillingClient.startConnection seems also not helpful as it incorrectly says "Google Play In-app Billing API version is less than 3" for unsupported countries. debugMessageBillingResult回调返回的onBillingSetupFinished中的BillingClient.startConnection似乎也没有帮助,因为对于不受支持的国家/地区,它错误地表示“Google Play In-app Billing API version is less than 3”。 (I tried for China using VPN.) (我尝试使用 VPN 在中国使用。)

  4. Checking the user's IP address would give wrong results if the user is in a country other than his Play country or uses a VPN.如果用户不在他的 Play 国家或使用 VPN,检查用户的 IP 地址将给出错误的结果。

  5. Using location APIs would require location permissions and also give wrong results if the user is in a foreign country.使用位置 API 需要位置权限,如果用户在国外,也会给出错误的结果。

To check if in-app purchases are supported in the user's Google Play country, you can use the isBillingSupported method of the BillingClient class.要检查用户的 Google Play 国家/地区是否支持应用内购买,您可以使用BillingClient类的isBillingSupported方法。

Here is an example of how you can use this method:以下是如何使用此方法的示例:

BillingClient billingClient = BillingClient.newBuilder(context).build();

billingClient.isBillingSupported(BillingClient.SkuType.INAPP, packageName, new BillingClient.BillingResponseListener() {
    @Override
    public void onBillingResponse(int responseCode, @Nullable List<String> list) {
        if (responseCode == BillingClient.BillingResponse.OK) {
            // In-app purchases are supported in the user's Google Play country
        } else {
            // In-app purchases are not supported in the user's Google Play country
        }
    }
});

Note that you will need to specify the packageName of your app when calling this method.请注意,调用此方法时需要指定应用程序的packageName

To check if in-app purchases are supported in the user's Google Play country, you can use the isBillingSupported() method of the BillingClient class.要检查用户所在的 Google Play 国家/地区是否支持应用内购买,您可以使用BillingClient类的isBillingSupported()方法。 This method returns a BillingResponse code, which indicates the result of the isBillingSupported() request.此方法返回一个 BillingResponse 代码,它指示isBillingSupported()请求的结果。

Here's an example of how you can use this method in your app:以下是如何在您的应用中使用此方法的示例:

BillingClient billingClient = BillingClient.newBuilder(context).setListener(new PurchasesUpdatedListener() {
    @Override
    public void onPurchasesUpdated(int responseCode, @Nullable List<Purchase> purchases) {
        // Handle purchase updates here
    }
}).build();

billingClient.startConnection(new BillingClientStateListener() {
    @Override
    public void onBillingSetupFinished(@BillingClient.BillingResponse int billingResponseCode) {
        if (billingResponseCode == BillingClient.BillingResponse.OK) {
            // Billing client is ready
            int responseCode = billingClient.isBillingSupported(BillingClient.SkuType.INAPP);
            if (responseCode == BillingClient.BillingResponse.OK) {
                // In-app purchases are supported
            } else {
                // In-app purchases are not supported
            }
        }
    }

    @Override
    public void onBillingServiceDisconnected() {
        // Try to restart the connection on the next request to
        // Google Play by calling the startConnection() method.
    }
});

Note that you need to specify the type of in-app purchase you want to check for (eg, BillingClient.SkuType.INAPP for one-time products or BillingClient.SkuType.SUBS for subscriptions) when calling the isBillingSupported() method.请注意,在调用isBillingSupported()方法时,您需要指定要检查的应用内购买类型(例如, BillingClient.SkuType.INAPP用于一次性产品或BillingClient.SkuType.SUBS用于订阅)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM