buildIosPurchasePayload function

Map<String, dynamic>? buildIosPurchasePayload(
  1. String nativeType,
  2. Object? iosProps
)

Build iOS purchase payload from props

Implementation

Map<String, dynamic>? buildIosPurchasePayload(
  String nativeType,
  Object? iosProps,
) {
  if (iosProps == null) {
    return null;
  }

  Map<String, dynamic> propsJson;
  if (iosProps is gentype.RequestPurchaseIosProps) {
    propsJson = iosProps.toJson();
  } else if (iosProps is gentype.RequestSubscriptionIosProps) {
    propsJson = iosProps.toJson();
  } else {
    return null;
  }

  final String? sku = propsJson['sku'] as String?;
  if (sku == null || sku.isEmpty) {
    return null;
  }

  final payload = <String, dynamic>{
    'sku': sku,
    'type': nativeType,
    'andDangerouslyFinishTransactionAutomatically':
        (propsJson['andDangerouslyFinishTransactionAutomatically'] as bool?) ??
            false,
  };

  final String? appAccountToken = propsJson['appAccountToken'] as String?;
  if (appAccountToken != null && appAccountToken.isNotEmpty) {
    payload['appAccountToken'] = appAccountToken;
  }

  final dynamic quantityValue = propsJson['quantity'];
  if (quantityValue is int) {
    payload['quantity'] = quantityValue;
  } else if (quantityValue is num) {
    payload['quantity'] = quantityValue.toInt();
  }

  final dynamic offerValue = propsJson['withOffer'];
  if (offerValue is Map) {
    payload['withOffer'] = offerValue
        .map<String, dynamic>((key, value) => MapEntry(key.toString(), value));
  }

  payload.removeWhere((_, value) => value == null);
  return payload;
}