validatePromoCode method

Future<Map<String, dynamic>?> validatePromoCode(
  1. String code, {
  2. String? appUserId,
})

Implementation

Future<Map<String, dynamic>?> validatePromoCode(
  String code, {
  String? appUserId,
}) async {
  try {
    final response = await post(
      endpoint: '/promo-codes/validate',
      body: {
        'code': code,
        if (appUserId != null && appUserId.isNotEmpty)
          'app_user_id': appUserId,
      },
    );
    return response.data as Map<String, dynamic>;
  } catch (e) {
    if (e is DioException && e.response?.statusCode == 404) {
      final body = e.response?.data;
      // Keep the existing status/boolean contract and the server's two
      // privacy-preserving reasons, including a prior grant's expiry.
      if (body is Map) {
        return {...Map<String, dynamic>.from(body), 'valid': false};
      }
      return {'valid': false, 'message': 'Invalid code'};
    }
    rethrow;
  }
}