summaryrefslogtreecommitdiff
path: root/TalerWallet1/Views/Sheets/Payment/PaymentView.swift
blob: dd3581fe57666fe4f9eacf6c4292afc6754ea93a (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import SwiftUI
import taler_swift
import SymLog

// Will be called either by the user scanning a QR code or tapping the provided link,
// both from the shop's website. We show the payment details in a sheet.
struct PaymentView: View {
    private let symLog = SymLogV(0)
    let stack: CallStack
    let navTitle = String(localized: "Confirm Payment", comment:"pay merchant")

    // the scanned URL
    let url: URL
    let template: Bool
    @Binding var amountToTransfer: Amount
    @Binding var summary: String

    @EnvironmentObject private var model: WalletModel
    @EnvironmentObject private var controller: Controller
    @AppStorage("myListStyle") var myListStyle: MyListStyle = .automatic

    @State var preparePayResult: PreparePayResult? = nil

    var body: some View {
      Group {
        if let preparePayResult {
            let effective = preparePayResult.amountEffective
            let terms = preparePayResult.contractTerms
            List {
                // TODO: show balanceDetails.balanceAvailable
                let baseURL = preparePayResult.contractTerms.exchanges.first?.url
                let raw = preparePayResult.amountRaw
                let currency = raw.currencyStr
                let topTitle = String(localized: "Amount to pay:")
                let topAbbrev = String(localized: "Pay:", comment: "mini")
                if let effective {
                    // TODO: already paid
                    let fee = try! Amount.diff(raw, effective)      // TODO: different currencies
                    ThreeAmountsV(stack: stack.push(),
                                  topTitle: topTitle,
                                 topAbbrev: topAbbrev,
                                 topAmount: raw, fee: fee,
                               bottomTitle: String(localized: "Amount to spend:"),
                              bottomAbbrev: String(localized: "Effective:", comment: "mini"),
                              bottomAmount: effective,
                                     large: false, pending: false, incoming: false,
                                   baseURL: baseURL,
                                    noFees: nil,        // TODO: check baseURL for fees
                                    status: nil,
                                   summary: terms.summary,
                                  merchant: terms.merchant.name)
                    // TODO: payment: popup with all possible exchanges, check fees
                } else if let balanceDetails = preparePayResult.balanceDetails {    // Insufficient
                    Text("You don't have enough \(currency).")
                        .talerFont(.headline)
                    ThreeAmountsV(stack: stack.push(),
                                  topTitle: topTitle,
                                 topAbbrev: topAbbrev,
                                 topAmount: raw, fee: nil,
                               bottomTitle: String(localized: "Amount available:"),
                              bottomAbbrev: String(localized: "Available:", comment: "mini"),
                              bottomAmount: balanceDetails.balanceAvailable,
                                     large: false, pending: false, incoming: false,
                                   baseURL: baseURL,
                                    noFees: nil,        // TODO: check baseURL for fees
                                    status: nil,
                                   summary: terms.summary,
                                  merchant: terms.merchant.name)
                } else {
                    // TODO: Error - neither effective nor balanceDetails
                    Text("Error")
                        .talerFont(.body)
                }
            }
            .listStyle(myListStyle.style).anyView
            .safeAreaInset(edge: .bottom) {
                if let effective {
                    NavigationLink(destination: LazyView {
                        PaymentDone(stack: stack.push(),
                            transactionId: preparePayResult.transactionId)
                    }) {
                        Text(navTitle)      // Confirm Payment
                    }
                    .buttonStyle(TalerButtonStyle(type: .prominent))
                    .padding(.horizontal)
                } else {
                    Button("Cancel", action: {
                        dismissTop()
                    })
                        .buttonStyle(TalerButtonStyle(type: .bordered))
                        .padding(.horizontal)
                }
            }
            .navigationTitle(navTitle)
        } else {
            LoadingView(scopeInfo: nil, message: url.host)
            .task { // this runs only once
                symLog.log(".task")
                if template {
                    if let result = try? await model.preparePayForTemplateM(url.absoluteString,
                                                                 amount: amountToTransfer,
                                                                            summary: summary) {
                        preparePayResult = result
                    }
                } else {
                    if let result = try? await model.preparePayForUriM(url.absoluteString) {
                        preparePayResult = result
                    }
                }
            }
        }
      }.onAppear() {
          symLog.log("onAppear")
          DebugViewC.shared.setSheetID(SHEET_PAYMENT)
      }
    }
}
// MARK: -
#if false
struct PaymentURIView_Previews: PreviewProvider {
    static var previews: some View {
        let merchant = Merchant(name: "Merchant")
        let extra = Extra(articleName: "articleName")
        let product = Product(description: "description")
        let terms = MerchantContractTerms(hWire: "hWire",
                                     wireMethod: "wireMethod",
                                        summary: "summary",
                                    summaryI18n: nil,
                                          nonce: "nonce",
                                         amount: Amount(currency: LONGCURRENCY, cent: 220),
                                    payDeadline: Timestamp.tomorrow(),
                                         maxFee: Amount(currency: LONGCURRENCY, cent: 20),
                                       merchant: merchant,
                                    merchantPub: "merchantPub",
                                   deliveryDate: nil,
                               deliveryLocation: nil,
                                      exchanges: [],
                                       products: [product],
                                 refundDeadline: Timestamp.tomorrow(),
                           wireTransferDeadline: Timestamp.tomorrow(),
                                      timestamp: Timestamp.now(),
                                        orderID: "orderID",
                                merchantBaseURL: "merchantBaseURL",
                                 fulfillmentURL: "fulfillmentURL",
                               publicReorderURL: "publicReorderURL",
                             fulfillmentMessage: nil,
                         fulfillmentMessageI18n: nil,
                            wireFeeAmortization: 0,
                                     maxWireFee: Amount(currency: LONGCURRENCY, cent: 20),
                                     minimumAge: nil
//                                        extra: extra,
//                                     auditors: []
                                  )
        let details = PreparePayResult(status: PreparePayResultType.paymentPossible,
                                transactionId: "txn:payment:012345",
                                contractTerms: terms,
                            contractTermsHash: "termsHash",
                                    amountRaw: Amount(currency: LONGCURRENCY, cent: 220),
                              amountEffective: Amount(currency: LONGCURRENCY, cent: 240),
                               balanceDetails: nil,
                                         paid: nil
//                               ,   talerUri: "talerURI"
        )
        let url = URL(string: "taler://pay/some_amount")!
        
//        @State private var amount: Amount? = nil        // templateParam
//        @State private var summary: String? = nil       // templateParam

        PaymentView(stack: CallStack("Preview"),
                    url: url, template: false, amountToTransfer: nil, summary: nil,
                    preparePayResult: details)
    }
}
#endif