summaryrefslogtreecommitdiff
path: root/wallet/src/main/java/net/taler/wallet/transactions/Transaction.kt
blob: 5fcabe7ffc26fcdb57359b0a68991ecea52589de (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * This file is part of GNU Taler
 * (C) 2020 Taler Systems S.A.
 *
 * GNU Taler is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3, or (at your option) any later version.
 *
 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

package net.taler.wallet.transactions

import androidx.annotation.DrawableRes
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonSubTypes.Type
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME
import com.fasterxml.jackson.annotation.JsonTypeName
import net.taler.common.Amount
import net.taler.common.Timestamp
import net.taler.wallet.R
import org.json.JSONObject

enum class ReserveType {
    /**
     * Manually created.
     */
    @JsonProperty("manual")
    MANUAL,

    /**
     * Withdrawn from a bank that has "tight" Taler integration
     */
    @JsonProperty("taler-bank-withdraw")
    @Suppress("unused")
    TALER_BANK_WITHDRAW,
}

@JsonInclude(NON_EMPTY)
class ReserveCreationDetail(val type: ReserveType, val bankUrl: String?)

enum class RefreshReason {
    @JsonProperty("manual")
    @Suppress("unused")
    MANUAL,

    @JsonProperty("pay")
    PAY,

    @JsonProperty("refund")
    @Suppress("unused")
    REFUND,

    @JsonProperty("abort-pay")
    @Suppress("unused")
    ABORT_PAY,

    @JsonProperty("recoup")
    @Suppress("unused")
    RECOUP,

    @JsonProperty("backup-restored")
    @Suppress("unused")
    BACKUP_RESTORED
}

@JsonInclude(NON_EMPTY)
class ReserveShortInfo(
    /**
     * The exchange that the reserve will be at.
     */
    val exchangeBaseUrl: String,
    /**
     * Key to query more details
     */
    val reservePub: String,
    /**
     * Detail about how the reserve has been created.
     */
    val reserveCreationDetail: ReserveCreationDetail
)

typealias Transactions = ArrayList<Transaction>

@JsonTypeInfo(
    use = NAME,
    include = PROPERTY,
    property = "type",
    defaultImpl = UnknownTransaction::class
)
/** missing:
AuditorComplaintSent = "auditor-complained-sent",
AuditorComplaintProcessed = "auditor-complaint-processed",
AuditorTrustAdded = "auditor-trust-added",
AuditorTrustRemoved = "auditor-trust-removed",
ExchangeTermsAccepted = "exchange-terms-accepted",
ExchangePolicyChanged = "exchange-policy-changed",
ExchangeTrustAdded = "exchange-trust-added",
ExchangeTrustRemoved = "exchange-trust-removed",
FundsDepositedToSelf = "funds-deposited-to-self",
FundsRecouped = "funds-recouped",
ReserveCreated = "reserve-created",
 */
@JsonSubTypes(
    Type(value = ExchangeAddedEvent::class, name = "exchange-added"),
    Type(value = ExchangeUpdatedEvent::class, name = "exchange-updated"),
    Type(value = ReserveBalanceUpdatedTransaction::class, name = "reserve-balance-updated"),
    Type(value = WithdrawTransaction::class, name = "withdrawn"),
    Type(value = OrderAcceptedTransaction::class, name = "order-accepted"),
    Type(value = OrderRefusedTransaction::class, name = "order-refused"),
    Type(value = OrderRedirectedTransaction::class, name = "order-redirected"),
    Type(value = PaymentTransaction::class, name = "payment-sent"),
    Type(value = PaymentAbortedTransaction::class, name = "payment-aborted"),
    Type(value = TipAcceptedTransaction::class, name = "tip-accepted"),
    Type(value = TipDeclinedTransaction::class, name = "tip-declined"),
    Type(value = RefundTransaction::class, name = "refund"),
    Type(value = RefreshTransaction::class, name = "refreshed")
)
@JsonIgnoreProperties(
    value = [
        "eventId"
    ]
)
abstract class Transaction(
    val timestamp: Timestamp,
    @get:LayoutRes
    open val layout: Int = R.layout.transaction_row,
    @get:LayoutRes
    open val detailPageLayout: Int = 0,
    @get:StringRes
    open val title: Int = 0,
    @get:DrawableRes
    open val icon: Int = R.drawable.ic_account_balance,
    open val showToUser: Boolean = false
) {
    open lateinit var json: JSONObject
}


class UnknownTransaction(timestamp: Timestamp) : Transaction(timestamp) {
    override val title = R.string.transaction_unknown
}

@JsonTypeName("exchange-added")
class ExchangeAddedEvent(
    timestamp: Timestamp,
    val exchangeBaseUrl: String,
    val builtIn: Boolean
) : Transaction(timestamp) {
    override val title = R.string.history_event_exchange_added
}

@JsonTypeName("exchange-updated")
class ExchangeUpdatedEvent(
    timestamp: Timestamp,
    val exchangeBaseUrl: String
) : Transaction(timestamp) {
    override val title = R.string.history_event_exchange_updated
}


@JsonTypeName("reserve-balance-updated")
class ReserveBalanceUpdatedTransaction(
    timestamp: Timestamp,
    /**
     * Condensed information about the reserve.
     */
    val reserveShortInfo: ReserveShortInfo,
    /**
     * Amount currently left in the reserve.
     */
    val reserveBalance: Amount,
    /**
     * Amount we expected to be in the reserve at that time,
     * considering ongoing withdrawals from that reserve.
     */
    val reserveAwaitedAmount: Amount,
    /**
     * Amount that hasn't been withdrawn yet.
     */
    val reserveUnclaimedAmount: Amount
) : Transaction(timestamp) {
    override val title = R.string.transaction_reserve_balance_updated
}

@JsonTypeName("withdrawn")
class WithdrawTransaction(
    timestamp: Timestamp,
    /**
     * Exchange that was withdrawn from.
     */
    val exchangeBaseUrl: String,
    /**
     * Unique identifier for the withdrawal session, can be used to
     * query more detailed information from the wallet.
     */
    val withdrawalGroupId: String,
    val withdrawalSource: WithdrawalSource,
    /**
     * Amount that has been subtracted from the reserve's balance
     * for this withdrawal.
     */
    val amountWithdrawnRaw: Amount,
    /**
     * Amount that actually was added to the wallet's balance.
     */
    val amountWithdrawnEffective: Amount
) : Transaction(timestamp) {
    override val layout = R.layout.transaction_in
    override val detailPageLayout = R.layout.fragment_event_withdraw
    override val title = R.string.transaction_withdrawal
    override val icon = R.drawable.transaction_withdrawal
    override val showToUser = true
}

@JsonTypeName("order-accepted")
class OrderAcceptedTransaction(
    timestamp: Timestamp,
    /**
     * Condensed info about the order.
     */
    val orderShortInfo: OrderShortInfo
) : Transaction(timestamp) {
    override val icon = R.drawable.ic_add_circle
    override val title = R.string.transaction_order_accepted
}

@JsonTypeName("order-refused")
class OrderRefusedTransaction(
    timestamp: Timestamp,
    /**
     * Condensed info about the order.
     */
    val orderShortInfo: OrderShortInfo
) : Transaction(timestamp) {
    override val icon = R.drawable.ic_cancel
    override val title = R.string.transaction_order_refused
}

@JsonTypeName("payment-sent")
class PaymentTransaction(
    timestamp: Timestamp,
    /**
     * Condensed info about the order that we already paid for.
     */
    val orderShortInfo: OrderShortInfo,
    /**
     * Set to true if the payment has been previously sent
     * to the merchant successfully, possibly with a different session ID.
     */
    val replay: Boolean,
    /**
     * Number of coins that were involved in the payment.
     */
    val numCoins: Int,
    /**
     * Amount that was paid, including deposit and wire fees.
     */
    val amountPaidWithFees: Amount,
    /**
     * Session ID that the payment was (re-)submitted under.
     */
    val sessionId: String?
) : Transaction(timestamp) {
    override val layout = R.layout.transaction_out
    override val detailPageLayout = R.layout.fragment_event_paid
    override val title = R.string.transaction_payment
    override val icon = R.drawable.ic_cash_usd_outline
    override val showToUser = true
}

@JsonTypeName("payment-aborted")
class PaymentAbortedTransaction(
    timestamp: Timestamp,
    /**
     * Condensed info about the order that we already paid for.
     */
    val orderShortInfo: OrderShortInfo,
    /**
     * Amount that was lost due to refund and refreshing fees.
     */
    val amountLost: Amount
) : Transaction(timestamp) {
    override val layout = R.layout.transaction_out
    override val title = R.string.transaction_payment_aborted
    override val icon = R.drawable.transaction_payment_aborted
    override val showToUser = true
}

@JsonTypeName("refreshed")
class RefreshTransaction(
    timestamp: Timestamp,
    /**
     * Amount that is now available again because it has
     * been refreshed.
     */
    val amountRefreshedEffective: Amount,
    /**
     * Amount that we spent for refreshing.
     */
    val amountRefreshedRaw: Amount,
    /**
     * Why was the refreshing done?
     */
    val refreshReason: RefreshReason,
    val numInputCoins: Int,
    val numRefreshedInputCoins: Int,
    val numOutputCoins: Int,
    /**
     * Identifier for a refresh group, contains one or
     * more refresh session IDs.
     */
    val refreshGroupId: String
) : Transaction(timestamp) {
    override val layout = R.layout.transaction_out
    override val icon = R.drawable.transaction_refresh
    override val title = R.string.transaction_refresh
    override val showToUser = !(amountRefreshedRaw - amountRefreshedEffective).isZero()
}

@JsonTypeName("order-redirected")
class OrderRedirectedTransaction(
    timestamp: Timestamp,
    /**
     * Condensed info about the new order that contains a
     * product (identified by the fulfillment URL) that we've already paid for.
     */
    val newOrderShortInfo: OrderShortInfo,
    /**
     * Condensed info about the order that we already paid for.
     */
    val alreadyPaidOrderShortInfo: OrderShortInfo
) : Transaction(timestamp) {
    override val icon = R.drawable.ic_directions
    override val title = R.string.transaction_order_redirected
}

@JsonTypeName("tip-accepted")
class TipAcceptedTransaction(
    timestamp: Timestamp,
    /**
     * Unique identifier for the tip to query more information.
     */
    val tipId: String,
    /**
     * Raw amount of the tip, without extra fees that apply.
     */
    val tipRaw: Amount
) : Transaction(timestamp) {
    override val icon = R.drawable.transaction_tip_accepted
    override val title = R.string.transaction_tip_accepted
    override val layout = R.layout.transaction_in
    override val showToUser = true
}

@JsonTypeName("tip-declined")
class TipDeclinedTransaction(
    timestamp: Timestamp,
    /**
     * Unique identifier for the tip to query more information.
     */
    val tipId: String,
    /**
     * Raw amount of the tip, without extra fees that apply.
     */
    val tipAmount: Amount
) : Transaction(timestamp) {
    override val icon = R.drawable.transaction_tip_declined
    override val title = R.string.transaction_tip_declined
    override val layout = R.layout.transaction_in
    override val showToUser = true
}

@JsonTypeName("refund")
class RefundTransaction(
    timestamp: Timestamp,
    val orderShortInfo: OrderShortInfo,
    /**
     * Unique identifier for this refund.
     * (Identifies multiple refund permissions that were obtained at once.)
     */
    val refundGroupId: String,
    /**
     * Part of the refund that couldn't be applied because
     * the refund permissions were expired.
     */
    val amountRefundedInvalid: Amount,
    /**
     * Amount that has been refunded by the merchant.
     */
    val amountRefundedRaw: Amount,
    /**
     * Amount will be added to the wallet's balance after fees and refreshing.
     */
    val amountRefundedEffective: Amount
) : Transaction(timestamp) {
    override val icon = R.drawable.transaction_refund
    override val title = R.string.transaction_refund
    override val layout = R.layout.transaction_in
    override val detailPageLayout = R.layout.fragment_event_paid
    override val showToUser = true
}

@JsonTypeInfo(
    use = NAME,
    include = PROPERTY,
    property = "type"
)
@JsonSubTypes(
    Type(value = WithdrawalSourceReserve::class, name = "reserve")
)
abstract class WithdrawalSource

@Suppress("unused")
@JsonTypeName("tip")
class WithdrawalSourceTip(
    val tipId: String
) : WithdrawalSource()

@JsonTypeName("reserve")
class WithdrawalSourceReserve(
    val reservePub: String
) : WithdrawalSource()

data class OrderShortInfo(
    /**
     * Wallet-internal identifier of the proposal.
     */
    val proposalId: String,
    /**
     * Order ID, uniquely identifies the order within a merchant instance.
     */
    val orderId: String,
    /**
     * Base URL of the merchant.
     */
    val merchantBaseUrl: String,
    /**
     * Amount that must be paid for the contract.
     */
    val amount: Amount,
    /**
     * Summary of the proposal, given by the merchant.
     */
    val summary: String
)