getPaymentMethods method

  1. @override
Future<List<PaymentMethod>> getPaymentMethods({
  1. required String customerId,
})
override

Get the payment methods for a customer.

Implementation

@override
Future<List<PaymentMethod>> getPaymentMethods(
    {required String customerId}) async {
  try {
    final result = await methodChannel.invokeMethod(
      'getPaymentMethods',
      {'customerId': customerId},
    );
    if (result != null) {
      if (result is List) {
        // Handle the case where the result is a list (Android)
        final List<dynamic> resultList = result;
        final paymentMethods = resultList.map((item) {
          final Map<String, dynamic> itemMap =
              Map<String, dynamic>.from(item as Map);
          return PaymentMethod.fromMap(itemMap);
        }).toList();
        return paymentMethods;
      } else if (result is Map) {
        // Handle the case where the result is a map (iOS)
        final List<dynamic> resultList =
            result['paymentMethods'] as List<dynamic>;
        final paymentMethods = resultList.map((item) {
          final Map<String, dynamic> itemMap =
              Map<String, dynamic>.from(item as Map);
          return PaymentMethod.fromMap(itemMap);
        }).toList();
        return paymentMethods;
      } else {
        throw PlatformException(
          code: 'INVALID_RESULT',
          message: 'getPaymentMethods returned an invalid result',
        );
      }
    } else {
      throw PlatformException(
        code: 'NULL_RESULT',
        message: 'getPaymentMethods returned null',
      );
    }
  } on PlatformException {
    rethrow;
  }
}