|
| 1 | +import SwiftUI |
| 2 | +import WebKit |
| 3 | + |
| 4 | +struct PayDataCollectionWebView: View { |
| 5 | + let url: URL |
| 6 | + let onClose: () -> Void |
| 7 | + let onComplete: () -> Void |
| 8 | + let onError: (String) -> Void |
| 9 | + let onFormDataChanged: (_ fullName: String?, _ dob: String?, _ pobAddress: String?) -> Void |
| 10 | + |
| 11 | + @State private var isLoading = true |
| 12 | + |
| 13 | + var body: some View { |
| 14 | + ZStack(alignment: .topTrailing) { |
| 15 | + PayWebViewRepresentable( |
| 16 | + url: url, |
| 17 | + isLoading: $isLoading, |
| 18 | + onComplete: onComplete, |
| 19 | + onError: onError, |
| 20 | + onFormDataChanged: onFormDataChanged |
| 21 | + ) |
| 22 | + |
| 23 | + // Close button |
| 24 | + Button(action: onClose) { |
| 25 | + Image(systemName: "xmark") |
| 26 | + .font(.system(size: 14, weight: .semibold)) |
| 27 | + .foregroundColor(.gray) |
| 28 | + .frame(width: 28, height: 28) |
| 29 | + .background(Color.white.opacity(0.9)) |
| 30 | + .clipShape(Circle()) |
| 31 | + .shadow(color: .black.opacity(0.1), radius: 2, x: 0, y: 1) |
| 32 | + } |
| 33 | + .padding(.top, 56) |
| 34 | + .padding(.trailing, 20) |
| 35 | + |
| 36 | + if isLoading { |
| 37 | + VStack(spacing: 16) { |
| 38 | + ProgressView() |
| 39 | + .scaleEffect(1.2) |
| 40 | + Text("Loading...") |
| 41 | + .font(.system(size: 14, design: .rounded)) |
| 42 | + .foregroundColor(.secondary) |
| 43 | + } |
| 44 | + .frame(maxWidth: .infinity, maxHeight: .infinity) |
| 45 | + .background(Color.white) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +private struct PayWebViewRepresentable: UIViewRepresentable { |
| 52 | + let url: URL |
| 53 | + @Binding var isLoading: Bool |
| 54 | + let onComplete: () -> Void |
| 55 | + let onError: (String) -> Void |
| 56 | + let onFormDataChanged: (_ fullName: String?, _ dob: String?, _ pobAddress: String?) -> Void |
| 57 | + |
| 58 | + func makeCoordinator() -> Coordinator { |
| 59 | + Coordinator(isLoading: $isLoading, onComplete: onComplete, onError: onError, onFormDataChanged: onFormDataChanged) |
| 60 | + } |
| 61 | + |
| 62 | + func makeUIView(context: Context) -> WKWebView { |
| 63 | + let config = WKWebViewConfiguration() |
| 64 | + |
| 65 | + // Register message handler matching the IC page's expected name |
| 66 | + config.userContentController.add(context.coordinator, name: "payDataCollectionComplete") |
| 67 | + |
| 68 | + let webView = WKWebView(frame: .zero, configuration: config) |
| 69 | + webView.navigationDelegate = context.coordinator |
| 70 | + webView.uiDelegate = context.coordinator |
| 71 | + webView.backgroundColor = .white |
| 72 | + webView.scrollView.backgroundColor = .white |
| 73 | + webView.isOpaque = false |
| 74 | + |
| 75 | + print("💳 [PayWebView] Loading URL: \(url)") |
| 76 | + webView.load(URLRequest(url: url)) |
| 77 | + return webView |
| 78 | + } |
| 79 | + |
| 80 | + func updateUIView(_ uiView: WKWebView, context: Context) {} |
| 81 | + |
| 82 | + class Coordinator: NSObject, WKScriptMessageHandler, WKNavigationDelegate, WKUIDelegate { |
| 83 | + @Binding var isLoading: Bool |
| 84 | + let onComplete: () -> Void |
| 85 | + let onError: (String) -> Void |
| 86 | + let onFormDataChanged: (_ fullName: String?, _ dob: String?, _ pobAddress: String?) -> Void |
| 87 | + |
| 88 | + init(isLoading: Binding<Bool>, onComplete: @escaping () -> Void, onError: @escaping (String) -> Void, onFormDataChanged: @escaping (_ fullName: String?, _ dob: String?, _ pobAddress: String?) -> Void) { |
| 89 | + self._isLoading = isLoading |
| 90 | + self.onComplete = onComplete |
| 91 | + self.onError = onError |
| 92 | + self.onFormDataChanged = onFormDataChanged |
| 93 | + } |
| 94 | + |
| 95 | + // JavaScript calls: window.webkit.messageHandlers.payDataCollectionComplete.postMessage({...}) |
| 96 | + func userContentController(_ userContentController: WKUserContentController, |
| 97 | + didReceive message: WKScriptMessage) { |
| 98 | + print("💳 [PayWebView] Received message: \(message.body)") |
| 99 | + |
| 100 | + guard let body = message.body as? [String: Any], |
| 101 | + let type = body["type"] as? String else { |
| 102 | + print("💳 [PayWebView] Invalid message format: \(message.body)") |
| 103 | + onError("Invalid message format") |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + print("💳 [PayWebView] Message type: \(type)") |
| 108 | + |
| 109 | + switch type { |
| 110 | + case "IC_COMPLETE": |
| 111 | + let success = body["success"] as? Bool ?? false |
| 112 | + // Extract form data from completion message (same fields as prefill) |
| 113 | + let fullName = body["fullName"] as? String |
| 114 | + let dob = body["dob"] as? String |
| 115 | + let pobAddress = body["pobAddress"] as? String |
| 116 | + print("💳 [PayWebView] IC_COMPLETE received, success: \(success), fullName: \(fullName ?? "nil"), dob: \(dob ?? "nil"), pobAddress: \(pobAddress ?? "nil")") |
| 117 | + if success { |
| 118 | + DispatchQueue.main.async { [weak self] in |
| 119 | + self?.onFormDataChanged(fullName, dob, pobAddress) |
| 120 | + self?.onComplete() |
| 121 | + } |
| 122 | + } |
| 123 | + case "IC_ERROR": |
| 124 | + let error = body["error"] as? String ?? "Unknown error" |
| 125 | + print("💳 [PayWebView] IC_ERROR received: \(error)") |
| 126 | + DispatchQueue.main.async { [weak self] in |
| 127 | + self?.onError(error) |
| 128 | + } |
| 129 | + case "IC_FORM_DATA": |
| 130 | + let fullName = body["fullName"] as? String |
| 131 | + let dob = body["dob"] as? String |
| 132 | + let pobAddress = body["pobAddress"] as? String |
| 133 | + print("💳 [PayWebView] IC_FORM_DATA received - fullName: \(fullName ?? "nil"), dob: \(dob ?? "nil"), pobAddress: \(pobAddress ?? "nil")") |
| 134 | + DispatchQueue.main.async { [weak self] in |
| 135 | + self?.onFormDataChanged(fullName, dob, pobAddress) |
| 136 | + } |
| 137 | + default: |
| 138 | + // Ignore unknown message types silently (don't treat as error) |
| 139 | + print("💳 [PayWebView] Unknown message type: \(type)") |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Capture console.log from JavaScript |
| 144 | + func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, |
| 145 | + initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) { |
| 146 | + print("💳 [PayWebView] JS Alert: \(message)") |
| 147 | + completionHandler() |
| 148 | + } |
| 149 | + |
| 150 | + func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { |
| 151 | + DispatchQueue.main.async { [weak self] in |
| 152 | + self?.isLoading = true |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // Handle navigation errors |
| 157 | + func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) { |
| 158 | + print("💳 [PayWebView] Navigation failed: \(error)") |
| 159 | + DispatchQueue.main.async { [weak self] in |
| 160 | + self?.isLoading = false |
| 161 | + self?.onError("Navigation failed: \(error.localizedDescription)") |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) { |
| 166 | + print("💳 [PayWebView] Failed to load: \(error)") |
| 167 | + DispatchQueue.main.async { [weak self] in |
| 168 | + self?.isLoading = false |
| 169 | + self?.onError("Failed to load page: \(error.localizedDescription)") |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { |
| 174 | + print("💳 [PayWebView] Page loaded successfully") |
| 175 | + DispatchQueue.main.async { [weak self] in |
| 176 | + self?.isLoading = false |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + // Handle link clicks - open external links in Safari |
| 181 | + func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { |
| 182 | + guard let url = navigationAction.request.url else { |
| 183 | + decisionHandler(.allow) |
| 184 | + return |
| 185 | + } |
| 186 | + |
| 187 | + // Allow initial page load and same-origin navigations |
| 188 | + if navigationAction.navigationType == .other { |
| 189 | + decisionHandler(.allow) |
| 190 | + return |
| 191 | + } |
| 192 | + |
| 193 | + // For link clicks, open in Safari |
| 194 | + if navigationAction.navigationType == .linkActivated { |
| 195 | + print("💳 [PayWebView] Opening external link in Safari: \(url)") |
| 196 | + UIApplication.shared.open(url) |
| 197 | + decisionHandler(.cancel) |
| 198 | + return |
| 199 | + } |
| 200 | + |
| 201 | + decisionHandler(.allow) |
| 202 | + } |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +#if DEBUG |
| 207 | +struct PayDataCollectionWebView_Previews: PreviewProvider { |
| 208 | + static var previews: some View { |
| 209 | + PayDataCollectionWebView( |
| 210 | + url: URL(string: "https://example.com")!, |
| 211 | + onClose: {}, |
| 212 | + onComplete: {}, |
| 213 | + onError: { _ in }, |
| 214 | + onFormDataChanged: { _, _, _ in } |
| 215 | + ) |
| 216 | + } |
| 217 | +} |
| 218 | +#endif |
0 commit comments