Skip to content

refactor(click-to-pay): extract parsers, scripts, and webview management#115

Open
manideepk90 wants to merge 1 commit into
mainfrom
refactor/c2p-session-launcher
Open

refactor(click-to-pay): extract parsers, scripts, and webview management#115
manideepk90 wants to merge 1 commit into
mainfrom
refactor/c2p-session-launcher

Conversation

@manideepk90

Copy link
Copy Markdown
Contributor

Type of Change

  • Bugfix
  • Feature
  • Refactor
  • Chore
  • CI/CD
  • Docs

Summary

Extract three major concerns from DefaultClickToPaySessionLauncher into separate files for better
maintainability and testability:

1. JSON Parsing (ClickToPayResponseDecoder.kt)

  • Centralized all JSONObject/JSONArray parsing logic
  • Added type-safe DecodeResult<T> sealed class for proper error handling
  • Decodes: CustomerPresence, CardsStatus, RecognizedCards, CheckoutResponse, SignOutResponse

2. JavaScript Code (ClickToPayScripts.kt)

  • Moved all inline JS strings to dedicated Kotlin functions
  • Functions for: SDK initialization, session management, card operations, checkout
  • Keeps main launcher code clean and JavaScript logic easier to maintain

3. WebView Lifecycle (ClickToPayWebViewManager.kt)

  • Extracted WebView initialization, attach/detach, and destroy logic
  • Centralized pending request management with ConcurrentHashMap
  • Proper Mutex-based synchronization for lifecycle operations
  • Correlation ID tracking for network requests

Benefits

  • Reduced DefaultClickToPaySessionLauncher from 1,272 to 837 lines (34% reduction)
  • Better separation of concerns - each class has a single responsibility
  • Easier to test individual components in isolation
  • Type-safe error handling with DecodeResult<T>
  • No changes to public API - fully backward compatible

Bug Fixes Applied During Refactor

  1. Fixed requestId mismatch in evaluateJavascriptOnMainThread - was ignoring passed requestId and
    creating new one, causing hangs
  2. Fixed mixedContentMode value - corrected typo "compactability""always"

Verification

  • Logger EventNames and values match original exactly
  • All 9 JS evaluation call sites pass requestId correctly
  • Build compiles successfully
  • No memory leaks introduced (same cleanup as original)

Files Changed

File Change
ClickToPayResponseDecoder.kt New - JSON parsing with DecodeResult
ClickToPayScripts.kt New - All JavaScript code extraction
ClickToPayWebViewManager.kt New - WebView lifecycle management
DefaultClickToPaySessionLauncher.kt Modified - Delegates to new classes

Affected Area & Impact

  • Android Click-To-Pay SDK
  • iOS
  • Shared Core

Testing

  • Compiled successfully (./gradlew :hyperswitch-sdk-android-click-to-pay:compileDebugKotlin)
  • Manually tested in Android demo app
  • Verified logger output matches original

Checklist

  • Code follows project style guidelines
  • No new warnings introduced
  • Logging parity verified with main branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant