import UIKit
import WebKit

class CheckoutPaymentViewController: UIViewController, WKNavigationDelegate {

var checkoutUrl: String? // Razorpay checkout URL

var wkWebView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

// Initialize WebView
wkWebView = WKWebView(frame: self.view.frame)
wkWebView.navigationDelegate = self
self.view.addSubview(wkWebView)

self.loadWebPage()
}

func loadWebPage() {
guard let urlString = self.checkoutUrl else { return }
guard let url = URL(string: urlString) else { return }
self.wkWebView.load(URLRequest(url: url))
}

// Handle Payment Success or Failure (Optional)
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url?.absoluteString {
if url.contains("success") {
print("Payment Successful")
self.dismiss(animated: true, completion: nil) // Dismiss ViewController on Success
} else if url.contains("failure") {
print("Payment Failed")
}
}
decisionHandler(.allow)
}
}
