-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMobileMoneyProcessingViewModel.swift
More file actions
67 lines (57 loc) · 2.22 KB
/
MobileMoneyProcessingViewModel.swift
File metadata and controls
67 lines (57 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Foundation
import PaystackCore
class MobileMoneyProcessingViewModel: ObservableObject {
var container: MobileMoneyContainer
var repository: ChargeMobileMoneyRepository
let mobileMoneyTransaction: MobileMoneyTransaction
@Published
var counter = 0
init(container: MobileMoneyContainer,
mobileMoneyTransaction: MobileMoneyTransaction,
repository: ChargeMobileMoneyRepository = ChargeMobileMoneyRepositoryImplementation()) {
self.container = container
self.repository = repository
self.mobileMoneyTransaction = mobileMoneyTransaction
}
var transactionDetails: VerifyAccessCode {
container.transactionDetails
}
var authorizationPromptText: String {
if !mobileMoneyTransaction.message.isEmpty {
return mobileMoneyTransaction.message
}
return "Please authorize the payment with \(container.provider.value) on your phone"
}
func checkTransactionStatus() {
Task {
await checkPendingCharge()
}
}
@MainActor
func initializeMobileMoneyAuthorization() async {
do {
let authenticationResult = try await repository.listenForMobileMoneyResponse(
for: Int(mobileMoneyTransaction.transaction) ?? 0)
await container.processTransactionResponse(authenticationResult)
} catch {
Logger.error("Listening for mobile money transaction failed with error: %@",
arguments: error.localizedDescription)
container.displayTransactionError(ChargeError(error: error))
}
}
@MainActor
private func checkPendingCharge() async {
do {
let authenticationResult = try await repository.checkPendingCharge(
with: transactionDetails.accessCode)
await container.processTransactionResponse(authenticationResult)
} catch {
Logger.error("Checking pending mobile money charge failed with error: %@",
arguments: error.localizedDescription)
container.displayTransactionError(ChargeError(error: error))
}
}
func cancelTransaction() {
container.restartMobileMoneyPayment()
}
}