summaryrefslogtreecommitdiff
path: root/TalerWallet1/Backend/WalletBackendRequest.swift
blob: 20827962c1da8f4105dd6674c3dfafbb3f3d09e2 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * This file is part of GNU Taler, ©2022-23 Taler Systems S.A.
 * See LICENSE.md
 */
import Foundation
import AnyCodable
import taler_swift

/// A request sent to the wallet backend.
struct WalletBackendRequest: Encodable {
    /// The operation name of the request.
    var operation: String
    
    /// The body of the request as JSON.
    var args: AnyEncodable
}

protocol WalletBackendFormattedRequest {
    associatedtype Args: Encodable
    associatedtype Response: Decodable
    
    func operation() -> String
    func args() -> Args
}
// MARK: -
/// The scope of a currency: either global or a dedicated exchange
struct ScopeInfo: Codable, Hashable {
    enum ScopeInfoType: String, Codable {
        case global
        case exchange
        case auditor
    }
    var type: ScopeInfoType
    var url: String?    // only for "exchange"
    var currency: String

    public static func == (lhs: ScopeInfo, rhs: ScopeInfo) -> Bool {
        if let lhsBaseURL = lhs.url {
            if let rhsBaseURL = rhs.url {
                if lhsBaseURL != rhsBaseURL { return false }    // different exchanges
                                                                // else fall thru and check type & currency
            } else { return false }                             // left but not right
        } else if rhs.url != nil {
            return false                                        // right but not left
        }
        return lhs.type == rhs.type &&
        lhs.currency == rhs.currency
    }
    public func hash(into hasher: inout Hasher) {
        hasher.combine(type)
        if let url {
            hasher.combine(url)
        }
        hasher.combine(currency)
    }
}
// MARK: -
/// A billing or mailing location.
struct Location: Codable {
    var country: String?
    var country_subdivision: String?
    var district: String?
    var town: String?
    var town_location: String?
    var post_code: String?
    var street: String?
    var building_name: String?
    var building_number: String?
    var address_lines: [String]?
}

/// Information identifying a merchant.
struct Merchant: Codable {
    var name: String
    var address: Location?
    var jurisdiction: Location?
}

/// A tax made on a payment.
struct Tax: Codable {
    var name: String
    var tax: Amount
}

/// A product being purchased from a merchant.
/// https://docs.taler.net/core/api-merchant.html#the-contract-terms
struct Product: Codable {
    var product_id: String?
    var description: String
//    var description_i18n:   ?
    var quantity: Int?
    var unit: String?
    var price: Amount?
    var image: String? // URL to a product image
    var taxes: [Tax]?
    var delivery_date: Timestamp?
}

/// Brief information about an order.
struct OrderShortInfo: Codable {
    var orderId: String
    var merchant: Merchant
    var summary: String
//    var summary_i18n:   ?
    var products: [Product]?
    var fulfillmentUrl: String?
    var fulfillmentMessage: String?
//    var fulfillmentMessage_i18n:    ?
    var contractTermsHash: String?
}
// MARK: -
/// A request to process a refund.
struct WalletBackendApplyRefundRequest: WalletBackendFormattedRequest {
    func operation() -> String { "applyRefund" }
    func args() -> Args { Args(talerRefundUri: talerRefundUri) }

    var talerRefundUri: String

    struct Args: Encodable {
        var talerRefundUri: String
    }

    struct Response: Decodable {
        var contractTermsHash: String
        var amountEffectivePaid: Amount
        var amountRefundGranted: Amount
        var amountRefundGone: Amount
        var pendingAtExchange: Bool
        var info: OrderShortInfo
    }
}


/// A request to force update an exchange.
struct WalletBackendForceUpdateRequest: WalletBackendFormattedRequest {
    func operation() -> String { "addRequest" }
    func args() -> Args { Args(exchangeBaseUrl: exchangeBaseUrl) }

    var exchangeBaseUrl: String
    struct Args: Encodable {
        var exchangeBaseUrl: String
    }

    struct Response: Decodable {}
}

/// A request to deposit funds.
struct WalletBackendCreateDepositGroupRequest: WalletBackendFormattedRequest {
    func operation() -> String { "createDepositGroup" }
    func args() -> Args { Args(depositPayToUri: depositePayToUri, amount: amount) }

    var depositePayToUri: String
    var amount: Amount
    
    struct Args: Encodable {
        var depositPayToUri: String
        var amount: Amount
    }
    
    struct Response: Decodable {
        var depositGroupId: String
    }
}

/// A request to get information about a payment request.
struct WalletBackendPreparePayRequest: WalletBackendFormattedRequest {
    func operation() -> String { "preparePay" }
    func args() -> Args { Args(talerPayUri: talerPayUri) }
    var talerPayUri: String

    struct Args: Encodable {
        var talerPayUri: String
    }

    struct Response: Decodable {}
}

/// A request to confirm a payment.
struct WalletBackendConfirmPayRequest: WalletBackendFormattedRequest {
    func operation() -> String { "confirmPay" }
    func args() -> Args { Args(proposalId: proposalId) }
    var proposalId: String

    struct Args: Encodable {
        var proposalId: String
    }

    struct Response: Decodable {}
}

// MARK: -
/// The result from PrepareReward
struct PrepareRewardResponse: Decodable {
    var walletRewardId: String
    var accepted: Bool
    var rewardAmountRaw: Amount
    var rewardAmountEffective: Amount
    var exchangeBaseUrl: String
    var expirationTimestamp: Timestamp
}
/// A request to prepare a reward.
struct PrepareRewardRequest: WalletBackendFormattedRequest {
    typealias Response = PrepareRewardResponse
    func operation() -> String { "prepareReward" }
    func args() -> Args { Args(talerRewardUri: talerRewardUri) }

    var talerRewardUri: String
    struct Args: Encodable {
        var talerRewardUri: String
    }
}
// MARK: -
/// A request to accept a reward.
struct AcceptRewardRequest: WalletBackendFormattedRequest {
    struct Response: Decodable {}
    func operation() -> String { "acceptReward" }
    func args() -> Args { Args(walletRewardId: walletRewardId) }

    var walletRewardId: String
    struct Args: Encodable {
        var walletRewardId: String
    }
}
// MARK: -
/// A request to abort a failed payment.
struct WalletBackendAbortFailedPaymentRequest: WalletBackendFormattedRequest {
    func operation() -> String { "abortFailedPayWithRefund" }
    func args() -> Args { Args(proposalId: proposalId) }

    var proposalId: String
    struct Args: Encodable {
        var proposalId: String
    }
    
    struct Response: Decodable {}
    
}
// MARK: -
struct IntegrationTestArgs: Codable {
    var exchangeBaseUrl: String
    var bankBaseUrl: String
    var merchantBaseUrl: String
    var merchantApiKey: String
    var amountToWithdraw: String
    var amountToSpend: String
}

/// A request to run a basic integration test.
struct WalletBackendRunIntegrationTestRequest: WalletBackendFormattedRequest {
    func operation() -> String { "runIntegrationTest" }
    func args() -> Args { integrationTestArgs }
    var integrationTestArgs: IntegrationTestArgs
    
    typealias Args = IntegrationTestArgs
    
    struct Response: Decodable {}
}

struct TestPayArgs: Codable {
    var merchantBaseUrl: String
    var merchantApiKey: String
    var amount: String
    var summary: String
}

/// A request to make a test payment.
struct WalletBackendTestPayRequest: WalletBackendFormattedRequest {
    func operation() -> String { "testPay" }
    func args() -> Args { testPayArgs }

    var testPayArgs: TestPayArgs
    typealias Args = TestPayArgs
    
    struct Response: Decodable {}
}

struct Coin: Codable {
    var denom_pub: String
    var denom_pub_hash: String
    var denom_value: String
    var coin_pub: String
    var exchange_base_url: String
    var remaining_value: String
    var refresh_parent_coin_pub: String
    var withdrawal_reserve_pub: String
    var coin_suspended: Bool
}

/// A request to dump all coins to JSON.
struct WalletBackendDumpCoinsRequest: WalletBackendFormattedRequest {
    func operation() -> String { "dumpCoins" }
    func args() -> Args { Args() }
    struct Args: Encodable { }

    struct Response: Decodable {
        var coins: [Coin]
    }
    
}

/// A request to suspend or unsuspend a coin.
struct WalletBackendSuspendCoinRequest: WalletBackendFormattedRequest {
    struct Response: Decodable {}
    func operation() -> String { "setCoinSuspended" }
    func args() -> Args { Args(coinPub: coinPub, suspended: suspended) }

    var coinPub: String
    var suspended: Bool
    
    struct Args: Encodable {
        var coinPub: String
        var suspended: Bool
    }
}

struct PrepareRefundRequest: WalletBackendFormattedRequest {
    func operation() -> String { "startRefundQueryForUri" }
    func args() -> Args { Args(talerRefundUri: talerRefundUri) }

    var talerRefundUri: String

    struct Args: Encodable {
        var talerRefundUri: String
    }

    struct Response: Decodable {
        var transactionId: String
    }
}

struct StartRefundQueryRequest: WalletBackendFormattedRequest {
    struct Response: Decodable {}
    func operation() -> String { "startRefundQuery" }
    func args() -> Args { Args(transactionId: transactionId) }

    var transactionId: String

    struct Args: Encodable {
        var transactionId: String
    }
}