purchase method

Future<PurchaseResult> purchase(
  1. StoreProduct product, {
  2. String? promotionalOfferId,
  3. SubscriptionOption? option,
  4. Discount? winBackOffer,
})

Purchases product (optionally via a specific Android option), unpacking productId/productType/basePlanId/offerId for the platform-interface call — callers work with the public StoreProduct / SubscriptionOption models, never raw ids, matching the Swift/Kotlin façades' ergonomics rather than the wire shape.

winBackOffer buys product at a win-back offer — one eligibleWinBackOffers returned (iOS 18+: StoreKit's win-back purchase option, nothing signed; Android: exactly that Play offer, tagged rovenue-winback). It excludes promotionalOfferId and option (RovenueErrorKind.invalidArgument); a discount that is not DiscountType.winBack or has no identifier throws RovenueErrorKind.ineligible with serverCode WIN_BACK_OFFER_UNAVAILABLE before the store opens, as does an offer the store does not list for the product (or iOS before 18).

Implementation

Future<PurchaseResult> purchase(
  StoreProduct product, {
  String? promotionalOfferId,
  SubscriptionOption? option,
  Discount? winBackOffer,
}) async {
  if (winBackOffer != null) {
    if (promotionalOfferId != null || option != null) {
      throw const RovenueException(
        kind: RovenueErrorKind.invalidArgument,
        detail: 'Pass one offer: winBackOffer cannot be combined with promotionalOfferId or option.',
      );
    }
    final id = winBackOffer.identifier;
    if (winBackOffer.type != DiscountType.winBack || id == null || id.isEmpty) {
      throw RovenueException(
        kind: RovenueErrorKind.ineligible,
        detail: winBackOffer.type != DiscountType.winBack
            ? 'Win-back offer unavailable: the discount is a ${winBackOffer.type.name} offer, not a win-back offer.'
            : 'Win-back offer unavailable: the discount has no identifier.',
        serverCode: _winBackOfferUnavailableCode,
      );
    }
    return _platform.purchase(product.id, product.type, winBackOfferId: id);
  }
  return _platform.purchase(
    product.id,
    product.type,
    promotionalOfferId: promotionalOfferId,
    basePlanId: option?.basePlanId,
    offerId: option?.offerId,
  );
}