launchBillingFlow method

Future<BillingResultWrapper> launchBillingFlow({
  1. required String product,
  2. String? offerToken,
  3. String? accountId,
  4. String? obfuscatedProfileId,
  5. String? oldProduct,
  6. String? purchaseToken,
  7. ProrationMode? prorationMode,
})

Attempt to launch the Play Billing Flow for a given productDetails.

The productDetails needs to have already been fetched in a queryProductDetails call. The accountId is an optional hashed string associated with the user that's unique to your app. It's used by Google to detect unusual behavior. Do not pass in a cleartext accountId, and do not use this field to store any Personally Identifiable Information (PII) such as emails in cleartext. Attempting to store PII in this field will result in purchases being blocked. Google Play recommends that you use either encryption or a one-way hash to generate an obfuscated identifier to send to Google Play.

Specifies an optional obfuscatedProfileId that is uniquely associated with the user's profile in your app. Some applications allow users to have multiple profiles within a single account. Use this method to send the user's profile identifier to Google. Setting this field requests the user's obfuscated account id.

Calling this attemps to show the Google Play purchase UI. The user is free to complete the transaction there.

This method returns a BillingResultWrapper representing the initial attempt to show the Google Play billing flow. Actual purchase updates are delivered via the PurchasesUpdatedListener.

This method calls through to BillingClient#launchBillingFlow. It constructs a BillingFlowParams instance by setting the given productDetails, the given accountId and the obfuscatedProfileId (https://developer.android.com/reference/com/android/billingclient/api/BillingFlowParams.Builder#setobfuscatedprofileid).

When this method is called to purchase a subscription through an offer, an offerToken can be passed in.

When this method is called to purchase a subscription, an optional oldProduct can be passed in. This will tell Google Play that rather than purchasing a new subscription, the user needs to upgrade/downgrade the existing subscription. The oldProduct and purchaseToken are the product id and purchase token that the user is upgrading or downgrading from. purchaseToken must not be null if oldProduct is not null. The prorationMode is the mode of proration during subscription upgrade/downgrade. This value will only be effective if the oldProduct is also set.

Implementation

Future<BillingResultWrapper> launchBillingFlow(
    {required String product,
    String? offerToken,
    String? accountId,
    String? obfuscatedProfileId,
    String? oldProduct,
    String? purchaseToken,
    ProrationMode? prorationMode}) async {
  assert((oldProduct == null) == (purchaseToken == null),
      'oldProduct and purchaseToken must both be set, or both be null.');
  return resultWrapperFromPlatform(
      await _hostApi.launchBillingFlow(PlatformBillingFlowParams(
    product: product,
    prorationMode: const ProrationModeConverter().toJson(prorationMode ??
        ProrationMode.unknownSubscriptionUpgradeDowngradePolicy),
    offerToken: offerToken,
    accountId: accountId,
    obfuscatedProfileId: obfuscatedProfileId,
    oldProduct: oldProduct,
    purchaseToken: purchaseToken,
  )));
}