googleSubscription static method
Parses purchases.subscriptionsv2.get.
Valid when the state grants access (active, canceled or grace period)
and the latest line item has not expired yet. A canceled
subscription keeps access until expiryTime; a pending one does not.
Implementation
static VerificationResult googleSubscription(
Map<String, dynamic> json, {
required DateTime now,
}) {
final state =
_googleSubscriptionStates[json['subscriptionState']] ??
VerificationState.unknown;
final lineItems = (json['lineItems'] as List<dynamic>? ?? [])
.cast<Map<String, dynamic>>();
Map<String, dynamic>? latest;
DateTime? expiresAt;
for (final item in lineItems) {
final expiry = DateTime.tryParse(item['expiryTime'] as String? ?? '');
if (latest == null ||
(expiry != null &&
(expiresAt == null || expiry.isAfter(expiresAt)))) {
latest = item;
expiresAt = expiry;
}
}
final grantsAccess =
state == VerificationState.active ||
state == VerificationState.canceled ||
state == VerificationState.gracePeriod;
final notExpired = expiresAt == null
? state != VerificationState.canceled
: expiresAt.isAfter(now);
final autoRenewingPlan = latest?['autoRenewingPlan'] as Map?;
final bool? willAutoRenew = autoRenewingPlan != null
? autoRenewingPlan['autoRenewEnabled'] as bool? ?? false
: (latest?['prepaidPlan'] != null ? false : null);
return VerificationResult(
isValid: grantsAccess && notExpired,
state: state,
platform: StorePlatform.google,
productId: latest?['productId'] as String?,
expiresAt: expiresAt,
willAutoRenew: willAutoRenew,
isSandbox: json.containsKey('testPurchase'),
raw: json,
);
}