buildCheckoutDispatchScript function

String buildCheckoutDispatchScript(
  1. String eventJson
)

Builds a JavaScript snippet that dispatches eventJson into the WebView.

Matches the RN SDK pattern: tries window.onMessageFromRN() first, falls back to window.dispatchEvent(MessageEvent).

Implementation

String buildCheckoutDispatchScript(String eventJson) {
  final escaped = eventJson
      .replaceAll('\\', '\\\\')
      .replaceAll("'", "\\'")
      .replaceAll('\n', '\\n')
      .replaceAll('\r', '\\r')
      .replaceAll('\u2028', '\\u2028')
      .replaceAll('\u2029', '\\u2029');
  return """
(() => {
  if (window.onMessageFromRN) {
    window.onMessageFromRN('$escaped');
    return;
  }
  try {
    const msg = JSON.parse('$escaped');
    window.dispatchEvent(new MessageEvent('message', { data: msg }));
  } catch (_) {}
})();
""";
}