Google Play хранит / обновляет пользователя В истории приложения в кеше, который можно проверить без сети.Если вы хотите проверить с сетью, Google также поддерживает то же самое.
Ниже я приведу простой урок, надеюсь, он поможет вам понять.
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;
}
}
}