retrieveEphemeralKey method

Future<EphemeralKey> retrieveEphemeralKey()

Implementation

Future<EphemeralKey> retrieveEphemeralKey() async {
  if (_shouldRefreshKey()) {
    String key;
    try {
      key = await ephemeralKeyProvider(API_VERSION);
    } catch (error) {
      final e = StripeAPIError(null, {
        StripeAPIError.FIELD_MESSAGE:
            "Failed to retrive ephemeralKey from server",
      });
      throw StripeAPIException(e);
    }

    try {
      Map<String, dynamic> decodedKey = json.decode(key);
      _ephemeralKey = EphemeralKey.fromJson(decodedKey);
    } catch (error) {
      final e = StripeAPIError(null, {
        StripeAPIError.FIELD_MESSAGE:
            "Failed to parse Ephemeral Key, Please return the response as it is as you recieved from stripe server",
      });
      throw StripeAPIException(e);
    }

    return _ephemeralKey;
  } else {
    return _ephemeralKey;
  }
}