Skip to content

Commit

Permalink
minor fixes for ApplePay feature
Browse files Browse the repository at this point in the history
  • Loading branch information
YehorKyrylov committed Oct 17, 2022
1 parent 38b2fcb commit 826cac4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
48 changes: 34 additions & 14 deletions Sources/PandaSDK/ConfiguredPanda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final public class Panda: PandaProtocol, ObserverSupport {
private let settingsStorage: Storage<Settings> = CodableStorageFactory.userDefaults()
private let deviceStorage: Storage<DeviceSettings> = CodableStorageFactory.userDefaults()
private var viewControllers: Set<WeakObject<WebViewController>> = []
private var viewControllerForApplePayPurchase: WebViewController?
private var payload: PandaPayload?
private var entryPoint: String? {
return payload?.entryPoint
Expand Down Expand Up @@ -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,
Expand All @@ -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))
}
}
}
}
Expand Down Expand Up @@ -335,13 +352,14 @@ final public class Panda: PandaProtocol, ObserverSupport {
}
}

public func getSubscriptionStatus(statusCallback: ((Result<SubscriptionStatus, Error>) -> Void)?) {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in
public func getSubscriptionStatus(withDelay: Double, statusCallback: ((Result<SubscriptionStatus, Error>) -> 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):
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion Sources/PandaSDK/Panda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public protocol PandaProtocol: AnyObject {
/**
You can call to check subscription status of User
*/
func getSubscriptionStatus(statusCallback: ((Result<SubscriptionStatus, Error>) -> Void)?)
func getSubscriptionStatus(withDelay: Double, statusCallback: ((Result<SubscriptionStatus, Error>) -> Void)?)

/**
Handle deeplinks
Expand Down
3 changes: 2 additions & 1 deletion Sources/PandaSDK/SubsriptionStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,5 +93,6 @@ public enum PaymentStatus: String, Codable {
case fail
case processing
case success
case ok
case unknown
}
2 changes: 1 addition & 1 deletion Sources/PandaSDK/UnconfiguredPanda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ final class UnconfiguredPanda: PandaProtocol, ObserverSupport {
onShow?(.failure(Errors.notConfigured))
}

func getSubscriptionStatus(statusCallback: ((Result<SubscriptionStatus, Error>) -> Void)?) {
func getSubscriptionStatus(withDelay: Double, statusCallback: ((Result<SubscriptionStatus, Error>) -> Void)?) {
pandaLog(UnconfiguredPanda.configError)
statusCallback?(.failure(Errors.notConfigured))
}
Expand Down

0 comments on commit 826cac4

Please sign in to comment.