-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMPesaChrageViewModel.swift
More file actions
92 lines (80 loc) · 3.08 KB
/
Copy pathMPesaChrageViewModel.swift
File metadata and controls
92 lines (80 loc) · 3.08 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Foundation
import PaystackCore
protocol MPesaContainer {
var transactionDetails: VerifyAccessCode { get }
func processTransactionResponse(_ response: ChargeCardTransaction) async
func displayTransactionError(_ error: ChargeError)
func restartMPesaPayment()
}
class MPesaChrageViewModel: ObservableObject, @MainActor MPesaContainer {
var chargeCardContainer: ChargeContainer
var repository: ChargeMobileMoneyRepository
var transactionDetails: VerifyAccessCode
let provider: MobileMoneyChannel
@Published
var phoneNumber: String = ""
@Published
var transactionState: ChargeMobileMoneyState = .countdown
init(chargeCardContainer: ChargeContainer,
transactionDetails: VerifyAccessCode,
provider: MobileMoneyChannel,
repository: ChargeMobileMoneyRepository = ChargeMobileMoneyRepositoryImplementation()) {
self.chargeCardContainer = chargeCardContainer
self.repository = repository
self.transactionDetails = transactionDetails
self.provider = provider
}
var isValid: Bool {
phoneNumber.count >= 10
}
@MainActor
func submitPhoneNumber() async {
do {
let authenticationResult = try await repository.chargeMobileMoney(
phone: phoneNumber.formattedKenyanPhoneNumber,
transactionId: "\(transactionDetails.transactionId ?? 0)",
provider: provider.key)
transactionState = .processTransaction(transaction: authenticationResult)
} catch {
displayTransactionError(ChargeError(error: error))
}
}
func restartMPesaPayment() {
transactionState = .countdown
}
@MainActor
func processTransactionResponse(_ response: ChargeCardTransaction) async {
switch response.status {
case .success:
chargeCardContainer.processSuccessfulTransaction(details: transactionDetails)
case .failed:
let message = response.message ?? response.displayText ?? "Declined"
transactionState = .error(ChargeError(message: message))
case .timeout:
let message = response.displayText ?? "Payment timed out"
transactionState = .fatalError(error: .init(message: message))
case .pending:
break
default:
Logger.error("Unexpected M-Pesa transaction status: %@",
arguments: response.status.rawValue)
transactionState = .fatalError(
error: .generic(withCause: "Unexpected transaction status: \(response.status.rawValue)"))
}
}
@MainActor
func displayTransactionError(_ error: ChargeError) {
Logger.error("Displaying error: %@", arguments: error.localizedDescription)
transactionState = .error(error)
}
func cancelTransaction() {
restartMPesaPayment()
}
}
enum ChargeMobileMoneyState {
case loading(message: String? = nil)
case countdown
case error(ChargeError)
case fatalError(error: ChargeError)
case processTransaction(transaction: MobileMoneyTransaction)
}