summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Stibane <marc@taler.net>2023-07-10 13:27:59 +0200
committerMarc Stibane <marc@taler.net>2023-07-10 13:27:59 +0200
commit407d4a7cd017e66c830bb49752d49fdd5c7169d7 (patch)
tree471dfac352c63e96edcd06ff2cf5bd2b53e99765
parentd8b2c28f8ea39ecbfc4c5582206af40670c5aa33 (diff)
downloadtaler-ios-407d4a7cd017e66c830bb49752d49fdd5c7169d7.tar.gz
taler-ios-407d4a7cd017e66c830bb49752d49fdd5c7169d7.tar.bz2
taler-ios-407d4a7cd017e66c830bb49752d49fdd5c7169d7.zip
insufficientBalance
-rw-r--r--TalerWallet1/Model/Model+Payment.swift8
-rw-r--r--TalerWallet1/Views/Payment/PaymentURIView.swift71
-rw-r--r--TalerWallet1/Views/Transactions/ThreeAmounts.swift12
3 files changed, 58 insertions, 33 deletions
diff --git a/TalerWallet1/Model/Model+Payment.swift b/TalerWallet1/Model/Model+Payment.swift
index ac1d421..51e8d57 100644
--- a/TalerWallet1/Model/Model+Payment.swift
+++ b/TalerWallet1/Model/Model+Payment.swift
@@ -125,11 +125,11 @@ struct PayMerchantInsufficientBalanceDetails: Codable {
}
/// The result from PreparePayForUri
-struct PaymentDetailsForUri: Codable {
+struct PreparePayResult: Codable {
let status: PreparePayResultType
let transactionId: String
let contractTerms: MerchantContractTerms
- let contractTermsHash: String
+ let contractTermsHash: String? // only if status != insufficientBalance
let amountRaw: Amount
let amountEffective: Amount? // only if status != insufficientBalance
let balanceDetails: PayMerchantInsufficientBalanceDetails? // only if status == insufficientBalance
@@ -138,7 +138,7 @@ struct PaymentDetailsForUri: Codable {
}
/// A request to get an exchange's payment contract terms.
fileprivate struct PreparePayForUri: WalletBackendFormattedRequest {
- typealias Response = PaymentDetailsForUri
+ typealias Response = PreparePayResult
func operation() -> String { return "preparePayForUri" }
func args() -> Args { return Args(talerPayUri: talerPayUri) }
@@ -170,7 +170,7 @@ extension WalletModel {
/// load payment details. Networking involved
@MainActor
func preparePayForUriM(_ talerPayUri: String) // M for MainActor
- async throws -> PaymentDetailsForUri {
+ async throws -> PreparePayResult {
let request = PreparePayForUri(talerPayUri: talerPayUri)
let response = try await sendRequest(request, ASYNCDELAY)
return response
diff --git a/TalerWallet1/Views/Payment/PaymentURIView.swift b/TalerWallet1/Views/Payment/PaymentURIView.swift
index a42ddc2..b1fb0bf 100644
--- a/TalerWallet1/Views/Payment/PaymentURIView.swift
+++ b/TalerWallet1/Views/Payment/PaymentURIView.swift
@@ -20,11 +20,11 @@ struct PaymentURIView: View {
@EnvironmentObject private var model: WalletModel
- func acceptAction(detailsForUri: PaymentDetailsForUri) {
+ func acceptAction(preparePayResult: PreparePayResult) {
Task {
do {
- let confirmPayResult = try await model.confirmPayM(detailsForUri.transactionId)
- symLog.log(confirmPayResult as Any)
+ let confirmPayResult = try await model.confirmPayM(preparePayResult.transactionId)
+// symLog.log(confirmPayResult as Any)
if confirmPayResult.type != "done" {
controller.playSound(0)
// TODO: show error
@@ -38,30 +38,51 @@ struct PaymentURIView: View {
}
}
- @State var detailsForUri: PaymentDetailsForUri? = nil
+ @State var preparePayResult: PreparePayResult? = nil
var body: some View {
- if let detailsForUri {
+ if let preparePayResult {
ScrollViewReader { scrollView in
+ let effective = preparePayResult.amountEffective
List {
- let baseURL = detailsForUri.contractTerms.exchanges.first?.url
- let raw = detailsForUri.amountRaw
- let effective = detailsForUri.amountEffective
+ let baseURL = preparePayResult.contractTerms.exchanges.first?.url
+ let raw = preparePayResult.amountRaw
let currency = raw.currencyStr
- let fee = try! Amount.diff(raw, effective) // TODO: different currencies
- ThreeAmountsView(topTitle: String(localized: "Amount to pay:"),
- topAmount: raw, fee: fee,
- bottomTitle: String(localized: "\(currency) to be spent:"),
- bottomAmount: effective,
- large: false, pending: false, incoming: false,
- baseURL: baseURL)
- // TODO: payment: popup with all possible exchanges, check fees
+ let topTitle = String(localized: "Amount to pay:")
+ if let effective {
+ // TODO: already paid
+ let fee = try! Amount.diff(raw, effective) // TODO: different currencies
+ ThreeAmountsView(topTitle: topTitle,
+ topAmount: raw, fee: fee,
+ bottomTitle: String(localized: "\(currency) to be spent:"),
+ bottomAmount: effective,
+ large: false, pending: false, incoming: false,
+ baseURL: baseURL)
+ // TODO: payment: popup with all possible exchanges, check fees
+ } else if let balanceDetails = preparePayResult.balanceDetails { // Insufficient
+ Text("You don't have enough \(currency)")
+ ThreeAmountsView(topTitle: topTitle,
+ topAmount: raw, fee: nil,
+ bottomTitle: String(localized: "\(currency) available:"),
+ bottomAmount: balanceDetails.balanceAvailable,
+ large: false, pending: false, incoming: false,
+ baseURL: baseURL)
+ } else {
+ // TODO: Error - neither effective nor balanceDetails
+ Text("Error")
+ }
}
.listStyle(myListStyle.style).anyView
.safeAreaInset(edge: .bottom) {
- Button(navTitle, action: { acceptAction(detailsForUri: detailsForUri) })
- .buttonStyle(TalerButtonStyle(type: .prominent))
- .padding(.horizontal)
+ if let effective {
+ Button(navTitle, action: { acceptAction(preparePayResult: preparePayResult) })
+ .buttonStyle(TalerButtonStyle(type: .prominent))
+ .padding(.horizontal)
+ } else {
+ Button("Cancel", action: { dismissTop() })
+ .buttonStyle(TalerButtonStyle(type: .bordered))
+ .padding(.horizontal)
+ }
}
.navigationTitle(navTitle)
} // ScrollViewReader
@@ -72,8 +93,8 @@ struct PaymentURIView: View {
.task {
do {
symLog.log(".task")
- let details = try await model.preparePayForUriM(url.absoluteString)
- detailsForUri = details
+ let result = try await model.preparePayForUriM(url.absoluteString)
+ preparePayResult = result
} catch { // TODO: error
symLog.log(error.localizedDescription)
}
@@ -116,17 +137,19 @@ struct PaymentURIView_Previews: PreviewProvider {
extra: extra
// auditors: [],
)
- let details = PaymentDetailsForUri(
+ let details = PreparePayResult(
status: PreparePayResultType.paymentPossible,
transactionId: "txn:payment:012345",
contractTerms: terms,
contractTermsHash: "termsHash",
amountRaw: try! Amount(fromString: LONGCURRENCY + ":2.2"),
amountEffective: try! Amount(fromString: LONGCURRENCY + ":2.4"),
- talerUri: "talerURI"
+ balanceDetails: nil,
+ paid: nil
+// , talerUri: "talerURI"
)
let url = URL(string: "taler://pay/some_amount")!
- PaymentURIView(url: url, detailsForUri: details)
+ PaymentURIView(url: url, preparePayResult: details)
}
}
diff --git a/TalerWallet1/Views/Transactions/ThreeAmounts.swift b/TalerWallet1/Views/Transactions/ThreeAmounts.swift
index 8d85c04..2c9e94f 100644
--- a/TalerWallet1/Views/Transactions/ThreeAmounts.swift
+++ b/TalerWallet1/Views/Transactions/ThreeAmounts.swift
@@ -32,7 +32,7 @@ struct ThreeAmountsSheet: View {
struct ThreeAmountsView: View {
var topTitle: String
var topAmount: Amount
- var fee: Amount
+ var fee: Amount?
var bottomTitle: String
var bottomAmount: Amount
let large: Bool
@@ -54,11 +54,13 @@ struct ThreeAmountsView: View {
color: labelColor,
large: large)
.padding(.bottom, 4)
- AmountView(title: "Exchange fee:",
- value: feeSign + fee.readableDescription,
- color: fee.isZero ? labelColor : feeColor,
- large: false)
+ if let fee {
+ AmountView(title: "Exchange fee:",
+ value: feeSign + fee.readableDescription,
+ color: fee.isZero ? labelColor : feeColor,
+ large: false)
.padding(.bottom, 4)
+ }
AmountView(title: bottomTitle,
value: bottomAmount.readableDescription,
color: foreColor,