KMEmbeddedPaymentPopWC.swift 5.0 KB

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