requestPayUrl method

Future<String> requestPayUrl({
  1. required String paymentToken,
  2. String? identifier,
  3. String? subtype,
})

Request wallet URL

Implementation

Future<String> requestPayUrl({
  required String paymentToken,
  String? identifier,
  String? subtype,
}) async {
  try {
    final requestData = <String, dynamic>{
      "payment_token": paymentToken,
    };

    if (identifier != null || subtype != null) {
      requestData["source"] = <String, dynamic>{};
      if (identifier != null) {
        requestData["source"]["identifier"] = identifier;
      }
      if (subtype != null) {
        requestData["source"]["subtype"] = subtype;
      }
    }
 log('requestData: $requestData');
    final response = await _apiService.post<Map<String, dynamic>>(
      ApiConstants.wallet,
      data: requestData,
    );
 log('response: $response');
    if (response.statusCode != null && response.statusCode! >= 200) {
      final body = response.data;
      log('redirect_url: ${body?["redirect_url"]}');
      log('iframe_redirection_url: ${body?["iframe_redirection_url"]}');
      final redirectUrl = body?["redirect_url"]?.toString();
      final iframeUrl = body?["iframe_redirection_url"]?.toString();
      final redirectionUrl = body?["redirection_url"]?.toString();

      final walletUrl = (redirectUrl == null || redirectUrl.isEmpty)
          ? iframeUrl
          : redirectUrl;

      return walletUrl ?? redirectionUrl ?? '';
    } else {
      throw const WalletUrlException();
    }
  } catch (e, stackTrace) {
    if (e is PaymobException) rethrow;
    log('error: $e');
    log('stackTrace: $stackTrace');
    throw   WalletUrlException('Failed to get wallet URL $e $stackTrace');
  }
}