-
Notifications
You must be signed in to change notification settings - Fork 93
feat(pay): add WebView-based Information Capture for Pay SDK #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8f813a1
feat(pay): add WebView-based Information Capture for Pay SDK
jakubuid 7935e3a
Merge branch 'develop' of https://github.com/reown-com/reown-swift in…
jakubuid cb7d7a1
fix(pay): use correct WebView message handler name
jakubuid f8f736a
feat(pay): add loading indicator to WebView
jakubuid 580e11c
Merge branch 'develop' into feat/ic_webview
jakubuid fc95677
feat(pay): add prefill form data for IC WebView
jakubuid f4e31c6
chore: bump yttrium to 0.10.32
jakubuid 2a21721
feat(pay): improve IC WebView UX
jakubuid 8f1a78a
feat(pay): add top padding to IC WebView
jakubuid 7ba09aa
Merge branch 'develop' into feat/ic_webview
jakubuid 1977d7d
fix(pay): increase WebView top padding for smaller screens
jakubuid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
Example/WalletApp/PresentationLayer/Wallet/Pay/PayDataCollectionWebView.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import SwiftUI | ||
| import WebKit | ||
|
|
||
| struct PayDataCollectionWebView: UIViewRepresentable { | ||
| let url: URL | ||
| let onComplete: () -> Void | ||
| let onError: (String) -> Void | ||
|
|
||
| func makeCoordinator() -> Coordinator { | ||
| Coordinator(onComplete: onComplete, onError: onError) | ||
| } | ||
|
|
||
| func makeUIView(context: Context) -> WKWebView { | ||
| let config = WKWebViewConfiguration() | ||
|
|
||
| // Register message handler matching the IC page's expected name | ||
| config.userContentController.add(context.coordinator, name: "payDataCollectionComplete") | ||
jakubuid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| let webView = WKWebView(frame: .zero, configuration: config) | ||
| webView.navigationDelegate = context.coordinator | ||
jakubuid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| webView.uiDelegate = context.coordinator | ||
| webView.backgroundColor = .white | ||
| webView.scrollView.backgroundColor = .white | ||
|
|
||
| print("💳 [PayWebView] Loading URL: \(url)") | ||
| webView.load(URLRequest(url: url)) | ||
jakubuid marked this conversation as resolved.
Show resolved
Hide resolved
jakubuid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return webView | ||
| } | ||
|
|
||
| func updateUIView(_ uiView: WKWebView, context: Context) {} | ||
|
|
||
| class Coordinator: NSObject, WKScriptMessageHandler, WKNavigationDelegate, WKUIDelegate { | ||
jakubuid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let onComplete: () -> Void | ||
| let onError: (String) -> Void | ||
|
|
||
| init(onComplete: @escaping () -> Void, onError: @escaping (String) -> Void) { | ||
| self.onComplete = onComplete | ||
| self.onError = onError | ||
| } | ||
|
|
||
| // JavaScript calls: window.webkit.messageHandlers.payDataCollectionComplete.postMessage({...}) | ||
| func userContentController(_ userContentController: WKUserContentController, | ||
jakubuid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| didReceive message: WKScriptMessage) { | ||
| print("💳 [PayWebView] Received message: \(message.body)") | ||
|
|
||
| guard let body = message.body as? [String: Any], | ||
| let type = body["type"] as? String else { | ||
| print("💳 [PayWebView] Invalid message format: \(message.body)") | ||
| onError("Invalid message format") | ||
| return | ||
| } | ||
|
|
||
| print("💳 [PayWebView] Message type: \(type)") | ||
|
|
||
| switch type { | ||
| case "IC_COMPLETE": | ||
| let success = body["success"] as? Bool ?? false | ||
| print("💳 [PayWebView] IC_COMPLETE received, success: \(success)") | ||
| if success { | ||
jakubuid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| DispatchQueue.main.async { [weak self] in | ||
| self?.onComplete() | ||
| } | ||
| } | ||
| case "IC_ERROR": | ||
| let error = body["error"] as? String ?? "Unknown error" | ||
| print("💳 [PayWebView] IC_ERROR received: \(error)") | ||
| DispatchQueue.main.async { [weak self] in | ||
| self?.onError(error) | ||
| } | ||
| default: | ||
| print("💳 [PayWebView] Unknown message type: \(type)") | ||
| DispatchQueue.main.async { [weak self] in | ||
| self?.onError("Unknown message type: \(type)") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Capture console.log from JavaScript | ||
| func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, | ||
| initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) { | ||
| print("💳 [PayWebView] JS Alert: \(message)") | ||
| completionHandler() | ||
| } | ||
|
|
||
| // Handle navigation errors | ||
| func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { | ||
| print("💳 [PayWebView] Navigation failed: \(error)") | ||
| DispatchQueue.main.async { [weak self] in | ||
| self?.onError("Navigation failed: \(error.localizedDescription)") | ||
| } | ||
| } | ||
|
|
||
| func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { | ||
| print("💳 [PayWebView] Failed to load: \(error)") | ||
| DispatchQueue.main.async { [weak self] in | ||
| self?.onError("Failed to load page: \(error.localizedDescription)") | ||
| } | ||
| } | ||
|
|
||
| func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { | ||
| print("💳 [PayWebView] Page loaded successfully") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #if DEBUG | ||
| struct PayDataCollectionWebView_Previews: PreviewProvider { | ||
| static var previews: some View { | ||
| PayDataCollectionWebView( | ||
| url: URL(string: "https://example.com")!, | ||
| onComplete: {}, | ||
| onError: { _ in } | ||
| ) | ||
| } | ||
| } | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.