diff --git a/Sources/PandaSDK/ConfiguredPanda.swift b/Sources/PandaSDK/ConfiguredPanda.swift index b16872b..375419f 100644 --- a/Sources/PandaSDK/ConfiguredPanda.swift +++ b/Sources/PandaSDK/ConfiguredPanda.swift @@ -35,6 +35,7 @@ final public class Panda: PandaProtocol, ObserverSupport { private let settingsStorage: Storage = CodableStorageFactory.userDefaults() private let deviceStorage: Storage = CodableStorageFactory.userDefaults() private var viewControllers: Set> = [] + private var viewControllerForApplePayPurchase: WebViewController? private var payload: PandaPayload? private var entryPoint: String? { return payload?.entryPoint @@ -99,12 +100,16 @@ final public class Panda: PandaProtocol, ObserverSupport { else { let error = ApplePayVerificationError.init(message: "Payment finished unsuccessfully") - onError?(error) - send(event: .purchaseError(error: error, source: entryPoint)) + viewControllerForApplePayPurchase?.dismiss(animated: true) { [weak self] in + self?.viewControllers.forEach { $0.value?.tryAutoDismiss() } + self?.onError?(error) + self?.send(event: .purchaseError(error: error, source: self?.entryPoint)) + } + return } - viewControllers.forEach({ $0.value?.onStartLoad() }) + viewControllers.forEach { $0.value?.onStartLoad() } verificationClient.verifyApplePayRequest( user: user, @@ -119,22 +124,34 @@ final public class Panda: PandaProtocol, ObserverSupport { return } + self.viewControllers.forEach { $0.value?.onFinishLoad() } + switch result { case let .success(result): if let transactionStatus = result.transactionStatus, transactionStatus == .fail { let error = ApplePayVerificationError.init(message: "Payment transaction failed") - self.send(event: .purchaseError(error: error, source: self.entryPoint)) - self.onError?(error) + + self.viewControllerForApplePayPurchase?.dismiss(animated: true) { [weak self] in + self?.viewControllers.forEach { $0.value?.tryAutoDismiss() } + self?.send(event: .purchaseError(error: error, source: self?.entryPoint)) + self?.onError?(error) + } + return } - self.viewControllers.forEach { $0.value?.tryAutoDismiss() } - self.send(event: .onApplePaySuccessfulPurchase(productID: productID)) + self.viewControllerForApplePayPurchase?.dismiss(animated: true) { [weak self] in + self?.viewControllers.forEach { $0.value?.tryAutoDismiss() } + self?.send(event: .onApplePaySuccessfulPurchase(productID: productID)) + } + case let .failure(error): - self.viewControllers.forEach { $0.value?.tryAutoDismiss() } - self.onError?(error) - self.send(event: .purchaseError(error: error, source: self.entryPoint)) + self.viewControllerForApplePayPurchase?.dismiss(animated: true) { [weak self] in + self?.viewControllers.forEach { $0.value?.tryAutoDismiss() } + self?.onError?(error) + self?.send(event: .purchaseError(error: error, source: self?.entryPoint)) + } } } } @@ -335,13 +352,14 @@ final public class Panda: PandaProtocol, ObserverSupport { } } - public func getSubscriptionStatus(statusCallback: ((Result) -> Void)?) { - DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in + public func getSubscriptionStatus(withDelay: Double, statusCallback: ((Result) -> Void)?) { + DispatchQueue.main.asyncAfter(deadline: .now() + withDelay) { [weak self] in guard let self = self else { return } + self.networkClient.getSubscriptionStatus(user: self.user) { (result) in switch result { case .failure(let error): @@ -449,13 +467,15 @@ final public class Panda: PandaProtocol, ObserverSupport { self.send(feedback: text, at: screenId) } } - viewModel.onApplePayPurchase = { [applePayPaymentHandler, weak self] pandaID, source, screenId, screenName, _ in + viewModel.onApplePayPurchase = { [applePayPaymentHandler, weak self] pandaID, source, screenId, screenName, view in guard let pandaID = pandaID else { pandaLog("Missing productId with source: \(source)") return } + self?.viewControllerForApplePayPurchase = view + pandaLog("purchaseStarted: \(pandaID) \(screenName) \(screenId)") self?.send(event: .purchaseStarted(screenId: screenId, screenName: screenName, productId: pandaID, source: entryPoint)) @@ -552,7 +572,7 @@ final public class Panda: PandaProtocol, ObserverSupport { } func onApplicationDidBecomeActive() { - getSubscriptionStatus { [weak self, settingsStorage] (result) in + getSubscriptionStatus(withDelay: 3.0) { [weak self, settingsStorage] (result) in let status: SubscriptionState switch result { case .failure(let error): diff --git a/Sources/PandaSDK/Panda.swift b/Sources/PandaSDK/Panda.swift index 27ea18d..a78ca2b 100644 --- a/Sources/PandaSDK/Panda.swift +++ b/Sources/PandaSDK/Panda.swift @@ -125,7 +125,7 @@ public protocol PandaProtocol: AnyObject { /** You can call to check subscription status of User */ - func getSubscriptionStatus(statusCallback: ((Result) -> Void)?) + func getSubscriptionStatus(withDelay: Double, statusCallback: ((Result) -> Void)?) /** Handle deeplinks diff --git a/Sources/PandaSDK/SubsriptionStatus.swift b/Sources/PandaSDK/SubsriptionStatus.swift index cd2d652..6290035 100644 --- a/Sources/PandaSDK/SubsriptionStatus.swift +++ b/Sources/PandaSDK/SubsriptionStatus.swift @@ -54,7 +54,7 @@ public struct SubscriptionInfo: Codable { public let price: Double? public let state: SubscriptionAPIStatus public let paymentType: PaymentType - public let paymentStatus: PaymentStatus + public let paymentStatus: PaymentStatus? public var stateDescription: String { state.rawValue @@ -93,5 +93,6 @@ public enum PaymentStatus: String, Codable { case fail case processing case success + case ok case unknown } diff --git a/Sources/PandaSDK/UnconfiguredPanda.swift b/Sources/PandaSDK/UnconfiguredPanda.swift index dd50473..3896d4c 100644 --- a/Sources/PandaSDK/UnconfiguredPanda.swift +++ b/Sources/PandaSDK/UnconfiguredPanda.swift @@ -121,7 +121,7 @@ final class UnconfiguredPanda: PandaProtocol, ObserverSupport { onShow?(.failure(Errors.notConfigured)) } - func getSubscriptionStatus(statusCallback: ((Result) -> Void)?) { + func getSubscriptionStatus(withDelay: Double, statusCallback: ((Result) -> Void)?) { pandaLog(UnconfiguredPanda.configError) statusCallback?(.failure(Errors.notConfigured)) }