KMEmbeddedPaymentPopWC.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // KMEmbeddedPaymentPopWC.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2024/9/4.
  6. //
  7. import Cocoa
  8. import WebKit
  9. class KMEmbeddedPaymentPopWC: NSWindowController, WKNavigationDelegate {
  10. @IBOutlet weak var mainView: NSView!
  11. @IBOutlet weak var webView: WKWebView!
  12. var urlPath: String?
  13. var countryCode: String?
  14. var postCode: String?
  15. var isPaddle: Bool = false
  16. var callback: ((_ isClose: Bool) -> Void)?
  17. static var currentWindowController: KMEmbeddedPaymentPopWC!
  18. @objc static func currentFirstTrialWC(_ urlPath: String) -> KMEmbeddedPaymentPopWC {
  19. if currentWindowController != nil {
  20. currentWindowController.urlPath = urlPath
  21. return currentWindowController
  22. } else {
  23. let configWC: KMEmbeddedPaymentPopWC = KMEmbeddedPaymentPopWC.init(windowNibName: "KMEmbeddedPaymentPopWC")
  24. currentWindowController = configWC;
  25. currentWindowController.urlPath = urlPath
  26. return currentWindowController
  27. }
  28. }
  29. override func windowDidLoad() {
  30. super.windowDidLoad()
  31. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  32. if isPaddle {
  33. let blankHTML = """
  34. <html>
  35. <head></head>
  36. <body style="background-color:white;"></body>
  37. </html>
  38. """
  39. webView.navigationDelegate = self
  40. webView.loadHTMLString(blankHTML, baseURL: URL(string: "https://cdn.paddle.com/paddle/paddle.js"))
  41. } else {
  42. if urlPath != nil {
  43. if let url = URL(string: urlPath!) {
  44. let request = URLRequest(url: url)
  45. webView.load(request)
  46. }
  47. }
  48. }
  49. self.window?.delegate = self;
  50. }
  51. func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  52. print("WebView load error: \(error.localizedDescription)")
  53. }
  54. func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  55. if isPaddle {
  56. if let url = navigationAction.request.url {
  57. print("Navigating to URL: \(url)")
  58. // 你可以根据 URL 判断是否是重定向页面,执行特定逻辑
  59. }
  60. }
  61. decisionHandler(.allow)
  62. }
  63. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  64. if isPaddle {
  65. if let localPaddleJsPath = Bundle.main.path(forResource: "paddle", ofType: "js") {
  66. let localPaddleJsURL = URL(fileURLWithPath: localPaddleJsPath)
  67. let paddleScript = generatePaddleScript(overrideUrl: localPaddleJsURL.absoluteString, countryCode: countryCode!, postCode: postCode!)
  68. webView.evaluateJavaScript(paddleScript) { (result, error) in
  69. if let error = error {
  70. print("Error evaluating JavaScript: \(error.localizedDescription)")
  71. } else {
  72. print("JavaScript executed successfully")
  73. }
  74. }
  75. } else {
  76. print("Paddle.js file not found in bundle")
  77. }
  78. }
  79. }
  80. func generatePaddleScript(overrideUrl: String, countryCode: String, postCode: String) -> String {
  81. let script = """
  82. var script = document.createElement('script');
  83. script.src = 'https://cdn.paddle.com/paddle/paddle.js';
  84. script.onload = function() {
  85. try {
  86. Paddle.Setup({ vendor: 134050 });
  87. var overrideUrl = '\(urlPath!)';
  88. Paddle.Checkout.open({
  89. method: 'sdk', //隐藏弹窗的关闭按钮
  90. override: overrideUrl,
  91. country: '\(countryCode)',
  92. postcode: '\(postCode)',
  93. });
  94. } catch (error) {
  95. console.log('Paddle setup or checkout error:', error);
  96. }
  97. };
  98. script.onerror = function() {
  99. console.log('Failed to load Paddle.js');
  100. };
  101. document.head.appendChild(script);
  102. """
  103. return script
  104. }
  105. }
  106. extension KMEmbeddedPaymentPopWC: NSWindowDelegate {
  107. func windowWillClose(_ notification: Notification) {
  108. if let callback = self.callback {
  109. callback(true)
  110. }
  111. KMEmbeddedPaymentPopWC.currentWindowController = nil
  112. }
  113. }