LokotroOtpVerifyResponse.fromJson constructor

LokotroOtpVerifyResponse.fromJson(
  1. Map<String, dynamic> json
)

Implementation

factory LokotroOtpVerifyResponse.fromJson(Map<String, dynamic> json) {
  // Handle both nested 'data' response format and flat response format
  final data = json['data'] as Map<String, dynamic>? ?? json;
  final paymentInfo = data['payment_info'] as Map<String, dynamic>?;

  return LokotroOtpVerifyResponse(
    success: data['success'] as bool? ?? json['success'] as bool? ?? false,
    status: data['status'] as String? ?? json['status'] as String? ?? 'unknown',
    // Support both old field names and new standardized format
    transactionReference: data['transaction_reference'] as String?
        ?? data['system_reference'] as String?
        ?? json['system_reference'] as String?
        ?? json['transaction_id'] as String?
        ?? paymentInfo?['reference'] as String?
        ?? paymentInfo?['identifier'] as String?,
    debitAmount: (data['debit_amount'] as num?)?.toDouble()
        ?? (data['amount'] as num?)?.toDouble()
        ?? (json['amount'] as num?)?.toDouble()
        ?? (paymentInfo?['transactional_amount'] as num?)?.toDouble(),
    walletBalance: (data['wallet_balance'] as num?)?.toDouble()
        ?? (json['wallet_balance'] as num?)?.toDouble(),
    emailsSent: (data['emails_sent'] as List?)?.cast<String>(),
    completedAt: data['completed_at'] as String?
        ?? paymentInfo?['completed_at'] as String?
        ?? data['verified_at'] as String?,
    message: json['message'] as String? ?? data['message'] as String? ?? '',
    paymentId: data['payment_id'] as String? ?? json['payment_id'] as String?,
    paymentInfo: paymentInfo,
  );
}