KMEmbeddedPaymentPopWC.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. window?.title = ""
  33. if isPaddle {
  34. let blankHTML = """
  35. <html>
  36. <head></head>
  37. <body style="background-color:white;"></body>
  38. </html>
  39. """
  40. webView.navigationDelegate = self
  41. webView.loadHTMLString(blankHTML, baseURL: URL(string: "https://cdn.paddle.com/paddle/paddle.js"))
  42. } else {
  43. if urlPath != nil {
  44. if let url = URL(string: urlPath!) {
  45. let request = URLRequest(url: url)
  46. webView.load(request)
  47. }
  48. }
  49. }
  50. self.window?.delegate = self;
  51. }
  52. func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  53. print("WebView load error: \(error.localizedDescription)")
  54. }
  55. func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  56. if isPaddle {
  57. if let url = navigationAction.request.url {
  58. print("Navigating to URL: \(url)")
  59. // 你可以根据 URL 判断是否是重定向页面,执行特定逻辑
  60. }
  61. }
  62. decisionHandler(.allow)
  63. }
  64. func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  65. if isPaddle {
  66. if let localPaddleJsPath = Bundle.main.path(forResource: "paddle", ofType: "js") {
  67. let localPaddleJsURL = URL(fileURLWithPath: localPaddleJsPath)
  68. let paddleScript = generatePaddleScript(overrideUrl: localPaddleJsURL.absoluteString, countryCode: countryCode!, postCode: postCode!)
  69. webView.evaluateJavaScript(paddleScript) { (result, error) in
  70. if let error = error {
  71. print("Error evaluating JavaScript: \(error.localizedDescription)")
  72. } else {
  73. print("JavaScript executed successfully")
  74. }
  75. }
  76. } else {
  77. print("Paddle.js file not found in bundle")
  78. }
  79. }
  80. }
  81. func generatePaddleScript(overrideUrl: String, countryCode: String, postCode: String) -> String {
  82. let script = """
  83. var script = document.createElement('script');
  84. script.src = 'https://cdn.paddle.com/paddle/paddle.js';
  85. script.onload = function() {
  86. try {
  87. Paddle.Setup({ vendor: 134050 });
  88. var overrideUrl = '\(urlPath!)';
  89. Paddle.Checkout.open({
  90. method: 'sdk', //隐藏弹窗的关闭按钮
  91. override: overrideUrl,
  92. country: '\(countryCode)',
  93. postcode: '\(postCode)',
  94. });
  95. } catch (error) {
  96. console.log('Paddle setup or checkout error:', error);
  97. }
  98. };
  99. script.onerror = function() {
  100. console.log('Failed to load Paddle.js');
  101. };
  102. document.head.appendChild(script);
  103. """
  104. return script
  105. }
  106. }
  107. extension KMEmbeddedPaymentPopWC: NSWindowDelegate {
  108. func windowWillClose(_ notification: Notification) {
  109. if let callback = self.callback {
  110. callback(true)
  111. }
  112. KMEmbeddedPaymentPopWC.currentWindowController = nil
  113. }
  114. }