如何使用 Google Play 结算库传递用户 ID?

How to pass a user ID with the Google Play Billing Library?

documentation 内容为:

Starting on August 2, 2021, all new apps must use Billing Library version 3 or newer. By November 1, 2021, all updates to existing apps must use Billing Library version 3 or newer.

这个依赖项是什么:

implementation "com.android.billingclient:billing:3.0.2"

如何向计费客户端传递类似用户 ID 的内容?

可以通过 accountIdprofileId 传递 BillingFlowParams:

String accountId = "";
String profileId = "";

BillingFlowParams.Builder builder = BillingFlowParams.newBuilder().setSkuDetails(skuDetail);
if (this.accountId != null) {builder.setObfuscatedAccountId(accountId);}
if (this.profileId != null) {builder.setObfuscatedProfileId(profileId);}
BillingFlowParams billingFlowParams = builder.build();

然后在 onPurchasesUpdated() 可以从 Purchase:

检索 AccountIdentifiers
@Override
public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> items) {
    if(items != null && items.size() > 0) {
        for (Purchase item : items) {
            if (item.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
                ...
                AccountIdentifiers identifiers = item.getAccountIdentifiers();
                if(identifiers != null) {
                    String accountId = identifiers.getObfuscatedAccountId();
                    String profileId = identifiers.getObfuscatedProfileId();
                    ...
                }
            }
        }
    }
}