123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- //
- // KMEmbeddedPaymentPopWC.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2024/9/4.
- //
- import Cocoa
- import WebKit
- class KMEmbeddedPaymentPopWC: NSWindowController, WKNavigationDelegate {
- @IBOutlet weak var mainView: NSView!
- @IBOutlet weak var webView: WKWebView!
- var urlPath: String?
- var countryCode: String?
- var postCode: String?
- var isPaddle: Bool = false
- var callback: ((_ isClose: Bool) -> Void)?
-
- static var currentWindowController: KMEmbeddedPaymentPopWC!
-
- @objc static func currentFirstTrialWC(_ urlPath: String) -> KMEmbeddedPaymentPopWC {
- if currentWindowController != nil {
- currentWindowController.urlPath = urlPath
- return currentWindowController
- } else {
- let configWC: KMEmbeddedPaymentPopWC = KMEmbeddedPaymentPopWC.init(windowNibName: "KMEmbeddedPaymentPopWC")
- currentWindowController = configWC;
- currentWindowController.urlPath = urlPath
- return currentWindowController
- }
- }
-
- override func windowDidLoad() {
- super.windowDidLoad()
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
-
- window?.title = ""
- if isPaddle {
- let blankHTML = """
- <html>
- <head></head>
- <body style="background-color:white;"></body>
- </html>
- """
- webView.navigationDelegate = self
- webView.loadHTMLString(blankHTML, baseURL: URL(string: "https://cdn.paddle.com/paddle/paddle.js"))
- } else {
- if urlPath != nil {
- if let url = URL(string: urlPath!) {
- let request = URLRequest(url: url)
- webView.load(request)
- }
- }
- }
- self.window?.delegate = self;
- }
- func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
- print("WebView load error: \(error.localizedDescription)")
- }
-
- func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
- if isPaddle {
- if let url = navigationAction.request.url {
- print("Navigating to URL: \(url)")
- // 你可以根据 URL 判断是否是重定向页面,执行特定逻辑
- }
- }
- decisionHandler(.allow)
- }
- func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
- if isPaddle {
- if let localPaddleJsPath = Bundle.main.path(forResource: "paddle", ofType: "js") {
- let localPaddleJsURL = URL(fileURLWithPath: localPaddleJsPath)
- let paddleScript = generatePaddleScript(overrideUrl: localPaddleJsURL.absoluteString, countryCode: countryCode!, postCode: postCode!)
- webView.evaluateJavaScript(paddleScript) { (result, error) in
- if let error = error {
- print("Error evaluating JavaScript: \(error.localizedDescription)")
- } else {
- print("JavaScript executed successfully")
- }
- }
- } else {
- print("Paddle.js file not found in bundle")
- }
- }
- }
- func generatePaddleScript(overrideUrl: String, countryCode: String, postCode: String) -> String {
- let script = """
- var script = document.createElement('script');
- script.src = 'https://cdn.paddle.com/paddle/paddle.js';
- script.onload = function() {
- try {
- Paddle.Setup({ vendor: 134050 });
- var overrideUrl = '\(urlPath!)';
- Paddle.Checkout.open({
- method: 'sdk', //隐藏弹窗的关闭按钮
- override: overrideUrl,
- country: '\(countryCode)',
- postcode: '\(postCode)',
- });
- } catch (error) {
- console.log('Paddle setup or checkout error:', error);
- }
- };
- script.onerror = function() {
- console.log('Failed to load Paddle.js');
- };
- document.head.appendChild(script);
- """
- return script
- }
- }
- extension KMEmbeddedPaymentPopWC: NSWindowDelegate {
- func windowWillClose(_ notification: Notification) {
- if let callback = self.callback {
- callback(true)
- }
- KMEmbeddedPaymentPopWC.currentWindowController = nil
- }
- }
|