purchase static method

Future<ApphudPurchaseResult> purchase({
  1. String? productId,
  2. ApphudProduct? product,
  3. String? offerIdToken,
  4. String? oldToken,
  5. int? replacementMode,
})

Purchase product and automatically submits App Store Receipt (iOS) or Google Play purchase token (Android) to Apphud.

  • parameter productId - Not recommended for use. Pass either productId or product object. Is an identifier of the native product that user wants to purchase. A/B analytics and other features may not work if purchasing via productId.
  • parameter product - Recommended for use. Is an ApphudProduct object from your ApphudPaywall. You must first configure paywalls and optionally placements in Apphud > Product Hub.
  • parameter offerIdToken - Android only. Required for Subscriptions. The identifier of the offer for initiating the purchase. Developer should retrieve it manually from SubscriptionOfferDetails object.
  • parameter oldToken - Android only. Optional. Specifies the Google Play Billing purchase token that the user is upgrading or downgrading from.
  • parameter replacementMode - Android only. Optional. Replacement mode (https://developer.android.com/reference/com/android/billingclient/api/BillingFlowParams.SubscriptionUpdateParams.ReplacementMode?hl=en)

Returns ApphudPurchaseResult object

Implementation

static Future<ApphudPurchaseResult> purchase({
  String? productId,
  ApphudProduct? product,
  String? offerIdToken,
  String? oldToken,
  int? replacementMode,
}) async {
  try {
    assert(
      !(productId == null && product == null),
      'Both productId and product can not be null at the same time',
    );
    assert(
      !(productId != null && product != null),
      'Both productId and product can not be non null at the same time',
    );
    dynamic json;
    if (productId != null) {
      json = await _channel.invokeMethod(
        'purchase',
        {
          'productId': productId,
          'offerIdToken': offerIdToken,
          'oldToken': oldToken,
          'replacementMode': replacementMode,
        },
      );
    } else if (product != null) {
      json = await _channel.invokeMethod(
        'purchaseProduct',
        product.toJson()
          ..remove('productDetails')
          ..remove('skProduct')
          ..addAll({
            'offerIdToken': offerIdToken,
            'oldToken': oldToken,
            'replacementMode': replacementMode,
          }),
      );
    }
    return ApphudPurchaseResult.fromJson(json);
  } catch (e) {
    return ApphudPurchaseResult(error: ApphudError(message: e.toString()));
  }
}