fromDetails method

Future<PaywallProduct> fromDetails(
  1. String slot,
  2. ProductDetails details
)

Implementation

Future<PaywallProduct> fromDetails(
  String slot,
  ProductDetails details,
) async {
  String period = '';
  String? basePlanId;
  var price = details.price;
  var rawPrice = details.rawPrice;
  var currency = details.currencyCode;
  String? trial;
  String? introOffer;
  var eligible = false;
  if (details is AppStoreProduct2Details) {
    final subscription = details.sk2Product.subscription;
    if (subscription != null) {
      period = _period(
        subscription.subscriptionPeriod.value,
        subscription.subscriptionPeriod.unit.name,
      );
      final introductory = subscription.promotionalOffers.where(
        (offer) => offer.type == SK2SubscriptionOfferType.introductory,
      );
      if (introductory.isNotEmpty && appleTrialEligibility != null) {
        eligible = await appleTrialEligibility!(details.id);
        if (eligible) {
          final offer = introductory.first;
          introOffer = _period(
            offer.period.value * offer.periodCount,
            offer.period.unit.name,
          );
          if (offer.paymentMode ==
              SK2SubscriptionOfferPaymentMode.freeTrial) {
            trial = introOffer;
          }
        }
      }
    }
  } else if (details is AppStoreProductDetails) {
    final subscription = details.skProduct;
    if (subscription.subscriptionPeriod != null) {
      period = _period(
        subscription.subscriptionPeriod!.numberOfUnits,
        subscription.subscriptionPeriod!.unit.name,
      );
    }
    final offer = subscription.introductoryPrice;
    if (offer != null && appleTrialEligibility != null) {
      eligible = await appleTrialEligibility!(details.id);
      if (eligible) {
        introOffer = _period(
          offer.subscriptionPeriod.numberOfUnits * offer.numberOfPeriods,
          offer.subscriptionPeriod.unit.name,
        );
        if (offer.paymentMode == SKProductDiscountPaymentMode.freeTrail) {
          trial = introOffer;
        }
      }
    }
  } else if (details is GooglePlayProductDetails &&
      details.subscriptionIndex != null) {
    final offer = details
        .productDetails
        .subscriptionOfferDetails![details.subscriptionIndex!];
    if (offer.pricingPhases.isNotEmpty) {
      final recurring = offer.pricingPhases.last;
      basePlanId = offer.basePlanId;
      price = recurring.formattedPrice;
      rawPrice = recurring.priceAmountMicros / 1000000;
      currency = recurring.priceCurrencyCode;
      period = _isoPeriod(recurring.billingPeriod);
      final introductory = offer.pricingPhases.first;
      if (offer.pricingPhases.length > 1) {
        // Play Billing returns offers eligible for the current account.
        eligible = true;
        introOffer =
            '${introductory.billingCycleCount} × ${_isoPeriod(introductory.billingPeriod)}';
        if (introductory.priceAmountMicros == 0) trial = introOffer;
      }
    }
  }
  return PaywallProduct(
    slot: slot,
    storeId: details.id,
    price: price,
    currency: currency,
    basePlanId: basePlanId,
    period: period,
    introOffer: introOffer,
    trial: trial,
    eligibleForTrial: eligible && trial != null,
    rawPrice: rawPrice,
    currencySymbol: details.currencySymbol,
  );
}