验证用户(电子邮件地址)是否从Google Play商店购买了我的应用

时间:2019-09-19 13:07:21

标签: android google-play

我有一个付费应用,安装后用户可以使用购买该应用的电子邮件地址进行注册。 我想知道这个电子邮件地址是否从Google Play购买了我的应用(验证),否则拒绝其注册。有什么办法(API)?

3 个答案:

答案 0 :(得分:1)

Google Play在缓存中的应用历史记录中存储/更新用户,而无需网络即可对其进行验证。如果您想与网络联系,谷歌也支持。

下面我提供了一个简单的课程,希望它能帮助您理解。

public class Verification implements PurchasesUpdatedListener, AcknowledgePurchaseResponseListener {
    private final BillingCallBack billingCallBack;
    private final boolean isPurchasedWithNetwrok;
    private Activity mActivity;
    private static final String TAG = "Verification";
    private BillingClient billingClient;

    private Verification(Activity activity, BillingCallBack billingCallBack, boolean isPurchasedWithNetwrok) {
        this.mActivity = activity;
        this.billingCallBack = billingCallBack;
        this.isPurchasedWithNetwrok = isPurchasedWithNetwrok;
        initBilling();
    }

    public static void getInstance(Activity activity, BillingCallBack billingCallBack, boolean isPurchasedWithNetwrok) {
        new Verification(activity, billingCallBack, isPurchasedWithNetwrok);
    }

    private void initBilling() {
        billingClient = BillingClient.newBuilder(mActivity).enablePendingPurchases().setListener(this).build();
        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
                    verifyWhetherUserIsSuperWithoutNetworkCall();
                } else {
                    billingCallBack.onError(BillingConstants.CONNECTION_ERROR);
                }
            }

            @Override
            public void onBillingServiceDisconnected() {
                billingClient.startConnection(this);
            }
        });

    }

    private void verifyWhetherUserIsSuperWithNetworkCall() {

        billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS, (billingResult, purchaseHistoryRecordList) -> {
            if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK)
                if (purchaseHistoryRecordList != null) {
                    handlePurchase(purchaseHistoryRecordList.get(0));
                } else {
                    //  billingCallBack.onBillingError(billingResult.getResponseCode());
                }
        });

    }

    private void verifyWhetherUserIsSuperWithoutNetworkCall() {
        Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS);
        if (purchasesResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
            if (purchasesResult.getPurchasesList().size() > 0) {
                for (Purchase purchase : purchasesResult.getPurchasesList()) {
                    if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
                        handlePurchase(purchasesResult.getPurchasesList().get(0));
                    } else if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING) {
                        billingCallBack.onError(BillingConstants.PENDING);
                    }
                }
            } else {
                billingCallBack.onError(BillingConstants.HISTORY_RESTORE_ERROR);
            }
        } else {
            billingCallBack.onError(BillingConstants.HISTORY_RESTORE_ERROR);
        }
    }

    private void handlePurchase(Purchase purchase) {
        if (!verifyValidSignature(purchase.getOriginalJson(), purchase.getSignature())) {
            billingCallBack.onError(BillingConstants.HISTORY_RESTORE_ERROR);
        } else {
            Log.d(TAG, "Got a verified purchase: " + purchase);
            if (!purchase.isAcknowledged()) {
                AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
                        .setPurchaseToken(purchase.getPurchaseToken())
                        .build();
                if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
                    billingClient.acknowledgePurchase(acknowledgePurchaseParams, this);
                } else if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING) {
                    billingCallBack.onError(BillingConstants.PENDING);
                }
            } else {
                billingCallBack.onSuccess(BillingConstants.HISTORY_RESTORED);
            }
        }

    }

    private void handlePurchase(PurchaseHistoryRecord purchase) {
        if (!verifyValidSignature(purchase.getOriginalJson(), purchase.getSignature())) {
            //       AppPreference.getInstance(mActivity.getApplicationContext()).setIsSuper(mActivity.getString(R.string.is_super), true);
            Log.i(TAG, "Got a purchase: " + purchase + "; but signature is bad. Skipping...");
            billingCallBack.onError(purchase.getSignature());
        } else {
            Log.d(TAG, "Got a verified purchase: " + purchase);
            if (!purchase.isAcknowledged()) {
                AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder()
                        .setPurchaseToken(purchase.getPurchaseToken())
                        .build();
                if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
                    billingClient.acknowledgePurchase(acknowledgePurchaseParams, this);
                } else if (purchase.getPurchaseState() == Purchase.PurchaseState.PENDING) {
                    billingCallBack.onPurchaseError(purchase.getPurchaseState());
                }
            } else {
                billingCallBack.onBillingResponse(purchase.getPurchaseState());
            }
        }

    }

    @Override
    public void onPurchasesUpdated(BillingResult billingResult, @Nullable List<Purchase> purchases) {
        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchases != null) {
            handlePurchase(purchases.get(0));
        } else {
            billingCallBack.onError(BillingConstants.HISTORY_RESTORE_ERROR);
        }
    }

//    private static final String BASE_64_ENCODED_PUBLIC_KEY = "Provide your Base_64_code from the Play console";

    private boolean verifyValidSignature(String signedData, String signature) {
        try {
            return Security.verifyPurchase(BuildConfig.BASE_64_ENCODED_PUBLIC_KEY, signedData, signature);
        } catch (IOException e) {
            Log.e(TAG, "Got an exception trying to validate a purchase: " + e);
            return true;
        }
    }


    @Override
    public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
            billingCallBack.onSuccess(BillingConstants.HISTORY_RESTORED);
            //    AppPreference.getInstance(mActivity).setIsSuper("isSuper", true);
        } else billingCallBack.onError(BillingConstants.HISTORY_RESTORE_ERROR);

    }

    public void destroy() {
        Log.d(TAG, "Destroying the manager.");
        if (billingClient != null && billingClient.isReady()) {
            billingClient.endConnection();
            billingClient = null;
        }
    }


}

答案 1 :(得分:0)

此处可识别购买应用的API  识别用户

packageName 您的应用程序包名称

subscriptionId productId(应用)

令牌用户购买令牌

google app purchase identification

答案 2 :(得分:0)

  • 您可以按照以下步骤操作

项目的build.gradle:

buildscript {
 dependencies {
  classpath 'com.android.tools.build:bundletool:0.9.0'
 }
}

应用模块的build.gradle:

实现'com.google.android.play:core:1.6.1' 扩展应用程序的类:

public void onCreate() {
if (MissingSplitsManagerFactory.create(this).disableAppIfMissingRequiredSplits()) {
    // Skip app initialization.
    return;
}
super.onCreate();
.....
}

检查清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication" >
    <application
        ...
        android:name="com.google.android.play.core.missingsplits.MissingSplitsDetectingApplication" >
    </application>
    ...
</manifest>

通过这种集成,谷歌将自动识别是否缺少任何拆分的apk,并显示一个弹出消息“安装失败”,并且还会重定向到Play商店下载屏幕,用户可以在其中通过Google Play商店正确安装该应用。 / p>

选中此link以获取更多信息。

您也可以使用此方法

public static boolean isStoreVersion(Context context) {
    boolean result = false;

    try {
        String installer = context.getPackageManager()
                                    .getInstallerPackageName(context.getPackageName());
        result = !TextUtils.isEmpty(installer);
    } catch (Throwable e) {          
    }

    return result;
}