payments

 avatar
unknown
swift
2 years ago
3.0 kB
4
Indexable
import Foundation
import WebKit

final class PaymentWebViewViewController: UIViewController, HasCloseAndLogoItemsOnLeft,WKNavigationDelegate {
    var paymentURL: String?
    var didFinishPayment: ((_ didUserQuit: Bool) -> Void)?
    private var didFinishByUser = true
    private var shouldTriggerUpdateBalance = false
    private var bgTaskId: UIBackgroundTaskIdentifier?
    @IBOutlet weak var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        guard let url = URL(string: paymentURL ?? "") else { return }
        let urlRequest = URLRequest(url: url)
        webView.load(urlRequest)
        webView.navigationDelegate = self
        addDidEnterBackgroundNotification()
        addWillEnterForegroundNotification()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        if didFinishByUser {
            didFinishPayment?(true)
        }
    }

    deinit {
        removeWillEnterForegroundNotification()
        removeDidEnterBackgroundNotification()
        if let bgTaskId = bgTaskId, bgTaskId != .invalid {
            UIApplication.shared.endBackgroundTask(bgTaskId)
        }
    }

    private func addWillEnterForegroundNotification() {
        let notificationCenter = NotificationCenter.default
        notificationCenter.addObserver(self, selector: #selector(appWillMoveToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
    }

    private func removeWillEnterForegroundNotification() {
        NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
    }

    private func addDidEnterBackgroundNotification() {
        let notificationCenter = NotificationCenter.default
        notificationCenter.addObserver(self, selector: #selector(appDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
    }

    private func removeDidEnterBackgroundNotification() {
        NotificationCenter.default.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if let url = navigationAction.request.url, url.description.contains("prywatny-description") {
            didFinishByUser = false
            didFinishPayment?(false)
            return decisionHandler(.cancel)
        }
        return decisionHandler(.allow)
    }

    @objc func appWillMoveToForeground() {
        if let bgTaskId = bgTaskId, bgTaskId != .invalid {
            UIApplication.shared.endBackgroundTask(bgTaskId)
        }
    }

    @objc func appDidEnterBackground() {
        bgTaskId = UIApplication.shared.beginBackgroundTask(expirationHandler: { [weak self] in
            guard let bgTaskId = self?.bgTaskId else { return }
            UIApplication.shared.endBackgroundTask(bgTaskId)
        })
    }
}
Editor is loading...