soapRequest method

Future<Response> soapRequest({
  1. required String url,
  2. required String soapAction,
  3. required String envelope,
  4. Map<String, String>? headers,
})

Implementation

Future<http.Response> soapRequest({
  required String url,
  required String soapAction,
  required String envelope,
  Map<String, String>? headers,
}) async {
  final soapHeaders = {
    "Content-Type": "text/xml; charset=utf-8",
    "SOAPAction": soapAction,
  };

  if (headers != null) {
    soapHeaders.addAll(headers);
  }

  // Load secure hash if enabled
  if (_isAppAttestationEnabled) {
    String fileString = decryptFileData(
      await rootBundle.load('packages/quixxi/asset/info.txt'),
    );
    Map<String, dynamic> jsonData = jsonDecode(fileString);
    String appAttestationKey = jsonData['secure_hash'];

    if (_sendHashInHeader) {
      soapHeaders['SecureHash'] = appAttestationKey;
    }

    if (_sendHashInPayload) {
      // Insert SecureHash *inside* <soap:Body> just before </soap:Body>
      envelope = envelope.replaceFirstMapped(
        RegExp(r'</soap:Body>', caseSensitive: false),
        (match) =>
            '<SecureHash>$appAttestationKey</SecureHash>${match.group(0)}',
      );

      // Fallback if <soap:Body> is missing
      if (!envelope.contains('<SecureHash>')) {
        envelope = envelope.replaceFirst(
          RegExp(r'</soap:Envelope>', caseSensitive: false),
          '<soap:Body><SecureHash>$appAttestationKey</SecureHash></soap:Body></soap:Envelope>',
        );
      }
    }
  }

  return _sendUnstreamed(
    "POST",
    url,
    soapHeaders,
    envelope,
    Encoding.getByName("utf-8"),
  );
}