在onPurchasesUpdated中,如何获取在BillingFlowParams中设置的accountid

时间:2018-07-05 03:43:21

标签: android in-app-billing

我正在将com.android.billingclient:billing:1.1用于android的应用内结算。我从以下代码开始付款:

BillingFlowParams flowParams = BillingFlowParams.newBuilder()
                            .setSku(productId)
                            .setType(BillingClient.SkuType.INAPP)
                            .setAccountId(accountId)
                            .build();
billingClient.launchBillingFlow(this, flowParams);

它将回调:

void onPurchasesUpdated(@BillingResponse int responseCode, @Nullable List<Purchase> purchases);

问题是如何获取BillingFlowParams中的帐户?

2 个答案:

答案 0 :(得分:0)

首先,我们必须重写onPurchasesUpdated()来处理响应代码。

@Override
void onPurchasesUpdated(BillingResult billingResult, List<Purchase> purchases) {
    if (billingResult.getResponseCode() == BillingResponse.OK
            && purchases != null) {
        for (Purchase purchase : purchases) {
            handlePurchase(purchase);
        }
    } else if (billingResult.getResponseCode() == BillingResponse.USER_CANCELED) {
        // Handle an error caused by a user cancelling the purchase flow.
    } else {
        // Handle any other error codes.
    }
}

然后,我们只有在billingResult.getResponseCode()BillingResponse.OKBillingClient client = ... AcknowledgePurchaseResponseListener acknowledgePurchaseResponseListener = ... void handlePurchase(Purchase purchase) { if (purchase.getState() == PurchaseState.PURCHASED) { // Grant entitlement to the user. ... // Acknowledge the purchase if it hasn't already been acknowledged. if (!purchase.isAcknowledged()) { AcknowledgePurchaseParams acknowledgePurchaseParams = AcknowledgePurchaseParams.newBuilder() .setPurchaseToken(purchase.getPurchaseToken()) .build(); client.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener); } } } 的情况下,才能获取购买令牌以确认正确持有的任何交易才能获得所需的帐户。

void handlePurchase(Purchase purchase) {
    if (purchase.getState() == PurchaseState.PURCHASED) {
        // Acknowledge purchase and grant the item to the user
    } else if (purchase.getState() == PurchaseState.PENDING) {
        // Here you can confirm to the user that they've started the pending
    }
}

然后只剩下一件事:

class Purchase

这里public methods有许多getOrderId(),例如getOriginalJson()和{{1}},用于获取结算帐户的详细信息。 More details

答案 1 :(得分:0)

我们建议您使用单向哈希值生成 用户ID中的字符串,并将散列的字符串存储在文档中提到的此字段中

/**
 * Specify an optional obfuscated string that is uniquely associated with the user's account in
 * your app.
 *
 * <p>If you pass this value, Google Play can use it to detect irregular activity, such as many
 * devices making purchases on the same account in a short period of time. Do not use the
 * developer ID or the user's Google ID for this field. In addition, this field should not
 * contain the user's ID in cleartext. **We recommend that you use a one-way hash to generate a
 * string from the user's ID and store the hashed string in this field**.
 *
 * <p>Optional:
 *
 * <ul>
 *   <li>To buy in-app item
 *   <li>To create a new subscription
 *   <li>To replace an old subscription
 * </ul>
 */
public Builder setAccountId(String accountId) {
  this.mAccountId = accountId;
  return this;
}
  

与应用程序中的用户帐户唯一关联的可选混淆字符串。如果您传递此值,则Google Play可以使用它来检测不正常的活动,例如许多设备在短时间内在同一帐户上进行购物